The description is omitted here,
Package org. rui. io. inout; import java. io. bufferedReader; import java. io. fileReader;/*** buffer input file * @ author lenovo **/public class BufferedInputFile {public static String read (String filename) throws Exception {BufferedReader br = new BufferedReader (new FileReader (filename); String s; StringBuilder sb = new StringBuilder (); while (s = br. readLine ())! = Null) sb. append (s + "\ n"); br. close (); // getClass (); return sb. toString ();} public static void main (String [] args) throws Exception {// String path = BufferedInputFile. class. getResource (". "). getFile (). toString (); // System. out. println (read (path + "BufferedInputFile. java "); String path =" D:/Users/liangrui/workspace/thinking/src/org/rui/io/inout/"; System. out. println (read (path + "BufferedInputFile. java ");}/** output: Same as above */
Package org. rui. io. inout; import java. io. *;/*** input from memory * @ author lenovo **/public class MemoryInput {public static void main (String [] args) throws Exception {String path = "D: /Users/liangrui/workspace/thinking/src/org/rui/io/inout/"; // BufferedInputFile. read (path + "MemoryInput. java ") StringReader in = new StringReader (BufferedInputFile. read (path + "MemoryInput. java "); int c = 0; while (c = in. read ())! =-1) System. out. print (char) c) ;}/ ** output: Same */
Package org. rui. io. inout; import java. io. byteArrayInputStream; import java. io. dataInputStream;/*** lattice tested memory input * @ author lenovo **/public class FormattedMemoryInput {public static void main (String [] args) {try {String path = "D :\\ Users \ liangrui \ workspace \ thinking \ src \ org \ rui \ io \ inout /"; dataInputStream in = new DataInputStream (new ByteArrayInputStream (BufferedInputFile. read (path + "FormattedMemoryInput. java "). getBytes (); while (true) System. out. print (char) in. readByte (); // The returned value cannot detect whether the input is complete} catch (Exception e) {System. out. println ("end of stream ");}}}
Package org. rui. io. inout; import java. io. bufferedInputStream; import java. io. byteArrayInputStream; import java. io. dataInputStream; import java. io. fileInputStream;/*** The following shows how to read a file in a byte location * @ author lenovo **/public class TestEOF {public static void main (String [] args) throws Exception {String path = "D :\\ Users \ liangrui \ workspace \ thinking \ src \ org \ rui \ io \ inout /"; dataInputStream in = new DataInputS Tream (new BufferedInputStream (new FileInputStream (path + "TestEOF. java "); // note that it varies with the type of the media to be read, literally, it means the number of bytes that can be read without blocking. // for a file, this means the entire file, but for different types of streams, this may not be the case, therefore, use while (in. available ()! = 0) System. out. print (char) in. readByte ());}}
Package org. rui. io. inout; import java. io. bufferedInputStream; import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. byteArrayInputStream; import java. io. dataInputStream; import java. io. fileInputStream; import java. io. fileWriter; import java. io. printWriter; import java. io. stringReader;/*** basic file output * @ author lenovo **/public class BasicFileOutput {public static void main (String [] args ) Throws Exception {String file = "D:/BasicFileOutput. out "; String path =" D :\\ Users \ liangrui \ workspace \ thinking \ src \ org \ rui \ io \ inout /"; // read the file BufferedReader in = new BufferedReader (new StringReader (BufferedInputFile. read (path + "BasicFileOutput. java "); // write PrintWriter pw = new PrintWriter (new BufferedWriter (new FileWriter (file); // int linecount = 1; String s; while (s = in. readLine ())! = Null) pw. println (linecount ++ s); pw. close (); // read the written file System. out. println (BufferedInputFile. read (file ));}}
Package org. rui. io. inout; import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. fileReader; import java. io. fileWriter; import java. io. printWriter; import java. io. stringReader;/*** text file output speed setting method * @ author lenovo **/public class FileOutputShortcut {public static void main (String [] args) throws Exception {String file = "D:/BasicFileOutput. out "; String path =" D: \ Users \ liangrui \ workspa Ce \ thinking \ src \ org \ rui \ io \ inout/"; // read the file BufferedReader in = new BufferedReader (new StringReader (BufferedInputFile. read (path + "BasicFileOutput. java "); // write java se5 add an auxiliary constructor printWriter pw = new PrintWriter (file) in PrintWriter; // int linecount = 1; string s; while (s = in. readLine ())! = Null) pw. println (linecount ++ s); pw. close (); // read the written file System. out. println (BufferedInputFile. read (file ));}}
Package org. rui. io. inout; import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. byteArrayInputStream; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. fileInputStream; import java. io. fileOutputStream;/*** store and recover data * If we use dataoutputstream to write data, java ensures that we can use datainputstream to accurately read data, * No matter how different the Data Reading and Writing platforms are * UTF-8 is a multi-byte format, and Its Encoding length varies according to the actual character set used, if we only use ASCII 7 characters * which wastes space and bandwidth, UTF-8 encodes the ASCII characters into a single byte, non-ASCII characters are encoded into two to three bytes * @ author lenovo **/public class StoringAndRecoveringData {public static void main (String [] args) throws Exception {String path = "d:/data.txt"; // output DataOutputStream out = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (path); out. writeDouble (5.55); out. writeUTF ("hello world"); out. writeDouble (6.66); out. writeUTF ("Hello World"); out. close (); // read DataInputStream in = new DataInputStream (new BufferedInputStream (new FileInputStream (path); // System. out. println (in. readDouble (); System. out. println (in. readUTF (); System. out. println (in. readDouble (); System. out. println (in. readUTF () ;}}/** output: 5.55 hello world6.66 hello world */
Package org. rui. io. inout; import java. io. bufferedInputStream; import java. io. byteArrayInputStream; import java. io. dataInputStream; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. randomAccessFile;/*** when reading and writing random access files ** using RandomAccessFile, you must know the file layout, in this way, you can operate on it correctly. * You can use various methods to read basic types and UTF-8 strings, the following example ** you may consider using a memory ing file to lend randomaccessfile * @ author lenovo **/public class UsingRandomAccessFile {static String file = "rtest. dat "; static void display () throws Exception {RandomAccessFile rf = new RandomAccessFile (file," r "); for (int I = 0; I <7; I ++) system. out. println ("value" + I + ":" + rf. readDouble (); System. out. println (rf. readUTF (); rf. close ();} public static void main (String [] args) throws Exception {RandomAccessFile rf = new RandomAccessFile (file, "rw "); // write for (int I = 0; I <7; I ++) rf. writeDouble (I * 1.414); rf. writeUTF ("the end of the file"); rf. close (); // print display (); // write to open and modify rf = new RandomAccessFile (file, "rw"); rf. seek (5*8); rf. writeDouble (1, 47.0001); rf. close (); // print display () ;}/ ** output: value 0: 0.0 value 1: 1.414 value 2: 2.828 value 3: 4.242 value 4: 5.656 value 5: 7.069999999999999 value 6: 8.484 the end of the filevalue 0: 0.0 value 1: 1.414 value 2: 2.828 value 3: 4.242 value 4: 5.656 value 5: 47.0001 value 6: 8.484 the end of the file */