Java Read and Write File method summary, java read and write Summary
Java File Read and Write method summary
Java's methods for reading and writing files are very useful at work. I have been using Java's methods for reading and writing files to process data input and output, which is really convenient. However, my memory is really anxious. Many times, since I cannot remember how to write it, I have a small amount of Java code, so I should exercise more. Here is a summary, which will be summarized in the future.
Java read files
1 package, genius, idiot dream; 2 3 import java. io. bufferedReader; 4 import java. io. file; 5 import java. io. fileInputStream; 6 import java. io. fileReader; 7 import java. io. IOException; 8 import java. io. inputStream; 9 import java. io. inputStreamReader; 10 import java. io. randomAccessFile; 11 import java. io. reader; 12 13 public class JavaIO {14 15/*** 16 * adopts the default encoding method at the underlying layer of the operating system, such as GBK, non-UTF8 17 **/18 19/** 20 * read files in bytes Content, which is often used to read binary files, such as video, image, and sound files 21 **/22 public static void readFileByBytes (String filename) {23 File file = new File (filename ); 24 FileInputStream in = null; 25 try {26 System. out. println ("reads a file in bytes, one byte at a time:"); 27 in = new FileInputStream (file); 28 int temp = 0; 29 while (temp = in. read ())! =-1) {30 System. out. println (temp); 31} 32 in. close (); 33} catch (IOException e) {34 e. printStackTrace (); 35 return; 36} 37 try {38 System. out. println ("read files in bytes, read multiple bytes at a time:"); 39 byte [] temp = new byte [100]; 40 int byteread = 0; 41 in = new FileInputStream (file); 42 JavaIO. showAvailableBytes (in); 43 while (byteread = in. read (temp ))! =-1) {44 System. out. write (temp, 0, byteread); 45} 46} catch (Exception e1) {47 e1.printStackTrace (); 48} finally {49 if (in! = Null) {50 try {51 in. close (); 52} catch (IOException e1) {53 54} 55} 56} 57} 58/** 59 * reads a file in character units and is often used to read text, 60 **/61 public static void readFileByChar (String filename) {62 File file = new File (filename); 63 Reader reader = null; 64 try {65 System. out. println ("Read File Content in characters, one byte at a time:"); 66 // InputStreamReader class: is the bridge between byte and character conversion 67 reader = new InputStreamReader (new FileInputStream (file); 68 Int temp; 69 while (temp = reader. read ())! =-1) {70 if (char) temp )! = '\ R') {71 System. out. println (char) temp); 72} 73} 74 reader. close (); 75} catch (Exception e) {76 e. printStackTrace (); 77} 78 try {79 System. out. println ("Read File Content in characters, read multiple bytes at a time:"); 80 char [] temp = new char [30]; 81 int charread = 0; 82 reader = new InputStreamReader (new FileInputStream (filename); 83 while (charread = reader. read (temp ))! =-1) {84 if (charread = temp. length) & (temp [temp. length-1]! = '\ R') {85 System. out. println (temp); 86} else {87 for (int I = 0; I <charread; I ++) {88 if (temp [I] = '\ R ') {89 break; 90} else {91 System. out. println (temp [I]); 92} 93} 94} 95} 96} catch (Exception e) {97 e. printStackTrace (); 98} finally {99 if (reader! = Null) {100 try {101 reader. close (); 102} catch (IOException e) {103 104} 105} 106} 107/** 108 * read files in behavior units, commonly used to read row-oriented formatting files 110 **/111 public static void readFileByLine (String filename) {112 File file File = new File (filename); 113 BufferedReader reader = null; 114 try {115 System. out. println ("reads the file content in the unit of action, one read for the whole row at a time:"); 116 reader = new BufferedReader (new FileReader (file); 117 String temp = null; 118 int line = 1; 119 While (temp = reader. readLine ())! = Null) {120 System. out. println ("line" + line + ":" + temp); 121 line ++; 122} 123 reader. close (); 124} catch (IOException e) {125 e. printStackTrace (); 126} finally {127 if (reader! = Null) {128 try {129 reader. close (); 130} catch (IOException e) {131 132} 133} 134} 135/** 136 * Randomly Read 137 */138 public static void readFileByRandomAccess (String filename) {139 RandomAccessFile randomfile = null; 141 try {142 System. out. println ("randomly reading a file content"); 143 randomfile = new RandomAccessFile (filename, "r"); 144 long fileLength = randomfile. length (); 145 int beginIndex = (fileLength> 4? 4: 0); 146 randomfile. seek (beginIndex); 147 byte [] bytes = new byte [10]; 148 int byteread = 0; 149 while (byteread = randomfile. read (bytes ))! =-1) {150 System. out. write (bytes, 0, byteread); 151} 152} catch (IOException e) {153 e. printStackTrace (); 154} finally {155 if (randomfile! = Null) {156 try {157 randomfile. close (); 158} catch (IOException e) {159 160} 161} 162} 163 private static void showAvailableBytes (InputStream in) {164 try {165 System. out. println ("number of bytes in the Current byte input stream:" + in. available (); 167} catch (IOException e) {168 e. printStackTrace (); 169} 170} 171 public static void main (String [] args) {172 String filename = "E: \ policyishaonian.txt"; 173 JavaIO. readFileByBytes (filename); 174 JavaIO. readFileByChar (filename); 175 JavaIO. readFileByLine (filename); 176 JavaIO. readFileByRandomAccess (filename); 177} 178}
Write files in Java
1 package, genius, idiot dream; 2 3 import java. io. bufferedWriter; 4 import java. io. file; 5 import java. io. fileNotFoundException; 6 import java. io. fileOutputStream; 7 import java. io. fileWriter; 8 import java. io. IOException; 9 import java. io. outputStreamWriter; 10 11 public class JavaIO2 {12 13 public static void main (String [] args) throws IOException {14 String Path = "E: \ genius idiot dream \ JAVA "; 15 File file = new File ("E: \ genius white Shumeng \ JAVA "," policyishaonian.txt "); 16 if (! File. exists () {17 try {18 file. createNewFile (); 19} catch (IOException e) {20 e. printStackTrace (); 21} 22} 23/** 24 * Three Java file Writing Methods 25 **/26 FileOutputStream fos = null; 27 BufferedWriter bw = null; 28 FileWriter fw = null; 29 int value = 1000; 30 31 try {32 fos = new FileOutputStream (new File (Path + "fos.txt"); 33 long begin = System. currentTimeMillis (); 34 for (int I = 1; I <= value; I ++) {35 fos. write (5); 36} 37 long end = System. currentTimeMillis (); 38 System. out. println ("TheCostTime of FileOutputStream is:" + (end-begin); 39 fos. close (); 40 41 bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (new File (Path + "br.txt"), "UTF8"); 42 begin = System. currentTimeMillis (); 43 for (int I = 1; I <= value; I ++) {44 bw. write (5); 45 bw. newLine (); 46} 47 bw. close (); 48 end = System. currentTimeMillis (); 49 System. out. println ("TheCostTime of BufferedWriter is:" + (end-begin); 50 51 fw = new FileWriter (Path + "fw.txt"); 52 begin = System. currentTimeMillis (); 53 for (int I = 1; I <= value; I ++) {54 fw. write (5); 55} 56 fw. close (); 57 end = System. currentTimeMillis (); 58 System. out. println ("TheCostTime of FileWriter is:" + (end-begin); 59 60 61} catch (Exception e) {62 // TODO Auto-generated catch block63 e. printStackTrace (); 64} finally {65 try {66 fos. close (); // FileOutputStream67 bw. close (); // BufferedWriter68 fw. close (); // FileWriter69} catch (Exception e) {70 e. printStackTrace (); 71} 72} 73 74} 75}