1. Properties 1. Overview
2. Common Methods
Public class PropertiesDemo1 {public static void main (String [] args) {Properties pro = new Properties ();/* setProperty (String key, String value) add key and value */pro. setProperty ("java01", "001"); pro. setProperty ("java02", "002");/* Get the value through the key: String getProperty (String key) */System. out. println (pro. getProperty ("java01");/* Set <String> stringPropertyNames () to obtain the key Set */set <String> Set = pro. stringPropertyNames (); for (String s: set) System. out. println (s + ":" + pro. getProperty (s) ;}}result: 001java02: 002java01: 001
3. Configuration File
Import java. io. fileReader; import java. io. IOException; import java. util. hashMap; import java. util. properties; import java. util. set; public class PropertiesDemo1 {public static void main (String [] args) throws IOException {BufferedReader br = new BufferedReader (new FileReader ("F: \ pro.txt ")); // read stream HashMap <String, String> map = new HashMap <String, String> (); // map set String line = null; while (line = br. ReadLine ())! = Null) {String [] s = line. split ("="); map. put (s [0], s [1]);} System. out. println (map) ;}}result: {java02 = 002, java03 = 003, java01 = 001}
Import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. util. hashMap; import java. util. properties; import java. util. set; public class PropertiesDemo1 {public static void main (String [] args) throws IOException {/* void load (Reader reader) load the read stream streams to the Collection */Properties pro = new Properties (); FileReader fr = new FileReader ("F: \ pro.txt"); pro. load (fr); // put the file read from the character reading stream into the Properties object System. out. println ("loaded set:" + pro);/* Below we modify the value */pro in the set. setProperty ("java02", "hello");/** store (Writer writer, String * comments) writes data to the stream by writing characters to update the information in the collection configuration file, comments is the watched content */FileWriter fw = new FileWriter ("F: \ pro.txt"); pro. store (fw, "java"); // update the configuration file. Note: java fr. close (); fw. close () ;}} result: loaded set: {java03 = 003, java02 = 002, java01 = 001}
4. Number of computing programs running
Importjava. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStream; import java. io. printStream; import java. util. properties; public class PropertiesDemo {publicstatic void main (String [] args) throws IOException {/** idea of the number of times the computing program runs: 1. create a configuration file and define a counter * 2. each time it is run, it is obtained, then auto-incrementing, and then stored back */Filef Ile = new File ("F: \ pro. ini"); if (! File. exists () file. createNewFile (); Propertiespro = new Properties (); pro. load (newFileInputStream (file); // load the stream Stringvalue = pro. getProperty ("count"); intcount = 0; if (value! = Null) count = Integer. parseInt (value); count ++; // auto-increment pro. setProperty ("count", count + ""); // change the value of pro. store (newFileOutputStream (file), "java for count"); // update the file System. out. println (pro) ;}} result: Run Once, count is + 1.
2. Print the stream
Import java. io. bufferedReader; import java. io. file; import java. io. IOException; import java. io. inputStreamReader; import java. io. printWriter; public class PrintStreamDemo {public static void main (String [] agrs) throws IOException {// File f = newFile ("F: \ demo.txt "); bufferedReader br = new BufferedReader (new InputStreamReader (System. in); PrintWriter pw = new PrintWriter (System. out, true); // true indicates automatic refresh, But it must be the println method String line = null; while (line = br. readLine ())! = Null) {if ("over ". equals (line) return; pw. println ("content:" + line); // pw. flush ();} br. close (); pw. close ();}}
3. Merge streams
SequenceInputStreamEnumerationInputStream
import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.Enumeration;import java.util.Vector;import java.io.FileInputStream; public class SqueDemo { public static void main(String[] agrs) throws IOException { Vector<FileInputStream> v = new Vector<FileInputStream>(); v.add(new FileInputStream("F:\\1.txt")); v.add(new FileInputStream("F:\\2.txt")); v.add(new FileInputStream("F:\\3.txt")); Enumeration<FileInputStream> en = v.elements(); SequenceInputStreamsis = new SequenceInputStream(en); FileOutputStream out = new FileOutputStream("F:\\4.txt"); byte[] b = new byte[1024]; int len = 0; while ((len = sis.read(b)) != -1) { out.write(b, 0, len); out.flush(); } sis.close(); out.close(); }}
4. stream cutting
Import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. sequenceInputStream; import java. util. enumeration; import java. util. vector; public class PhotoDemo {public static void main (String [] args) throws IOException {FenGe (); HeBing ();} private static void HeBing () throws FileNotFoundException, IOException {/* actually this You can also use loops and add them to the set. */Vector <FileInputStream> v = new Vector <FileInputStream> (); v. add (new FileInputStream ("F: \ part \ 1. part "); v. add (new FileInputStream ("F: \ part \ 2. part "); v. add (new FileInputStream ("F: \ part \ 3. part "); v. add (new FileInputStream ("F: \ part \ 4. part "); v. add (new FileInputStream ("F: \ part \ 5. part "); Enumeration <FileInputStream> en = v. elements (); SequenceInputStream sis = new Se QuenceInputStream (en); FileOutputStream out = new FileOutputStream ("F: \ part \ 1.bmp"); byte [] B = new byte [1024]; int len = 0; while (len = sis. read (B ))! =-1) {out. write (B, 0, len); out. flush ();} sis. close (); out. close ();}/* Split */private static void FenGe () throws FileNotFoundException, IOException {FileInputStream input = new FileInputStream ("F :\\ 1.png"); FileOutputStream out = null; byte [] buf = new byte [1024*100]; int count = 1; int len = 0; while (len = input. read (buf ))! =-1) {out = new FileOutputStream ("F: \ part \" + (count ++) + ". part "); // you can customize the suffix after the split out. write (buf, 0, len); out. flush (); out. close ();} input. close ();}}