I believe that Java's reading and writing file methods are of many uses in work. I used Java's reading and writing file methods to process data input and output, which is indeed very convenient. However, my memory is really worrying. Many times, since I can't remember how to write it, my Java code is really small, so I should practice more. Here is a summary and we will focus on the aspects in the future.
Java reading files
package Genius idiot dream; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.InputStreamReader;import java.io.RandomAccessFile;import java.io.Reader;public class JavaIO { /** * Adopt the default encoding method of the operating system, GBK, etc., non-UTF8 * */ /** * Read file content in bytes, often used to read binary files, such as pictures, images, sounds, etc. */ public static void readFileByBytes(String filename) { File file=new File(filename); FileInputStream in=null; try { System.out.println("Read file in bytes, read one byte at a time: "); in=new FileInputStream(file); int temp=0; while ((temp=in.read()) != -1) { System.out.println(temp); } in.close(); } catch (IOException e) { e.printStackTrace(); return ; } try { System.out.println("Read the file in bytes, read multiple bytes at a time: "); byte[] temp=new byte[100]; int byteread=0; in=new FileInputStream(file); JavaIO.showAvailableBytes(in); while ((byteread=in.read(temp))) != -1) { System.out.write(temp,0,byteread); } } catch (Exception e1) { e1.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } } } } /** * Read files in characters, often used to read files of text, numbers, etc. * */ public static void readFileByChar(String filename) { File file=new File(filename); Reader reader=null; try { System.out.println("Read file content in characters, one byte at a time:"); //InputStreamReader class: is a bridge for converting bytes to characters reader=new InputStreamReader(new FileInputStream(file)); int temp; while ((temp=reader.read()) != -1) { if (((char)temp) != '/r') { System.out.println((char)temp); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } try { System.out.println("Read file content in characters, read multiple bytes at a time: "); char[] temp=new char[30]; int charread=0; reader=new InputStreamReader(new FileInputStream(filename)); while ((charread=reader.read(temp)) != -1) { if ((charread == temp.length) && (temp[temp.length-1]!='/r')) { System.out.println(temp); } else { for (int i=0; i<charread; i++) { if (temp[i] == '/r') { break; } else { System.out.println(temp[i]); } } } } } } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } } } } /** * Read files in behavior units, often used to read formatted files with rows* */ public static void readFileByLine(String filename) { File file=new File(filename); BufferedReader reader=null; try { System.out.println("Read file content in behavior units, read one whole line at a time: "); reader=new BufferedReader(new FileReader(file)); String temp=null; int line=1; while ((temp=reader.readLine())) != null) { System.out.println("line " + line + ": " + temp); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } } } /** * Random read file content* */ public static void readFileByRandomAccess(String filename) { RandomAccessFile randomfile=null; try { System.out.println("Random reads a piece of file content"); randomfile=new RandomAccessFile(filename,"r"); long fileLength=randomfile.length(); int beginIndex=(fileLength > 4 ? 4 : 0); randomfile.seek(beginIndex); byte[] bytes=new byte[10]; int byteread=0; while ((bytead=randomfile.read(bytes)) != -1) { System.out.write(bytes,0,byteread); } } catch (IOException e) { e.printStackTrace(); } finally { if (randomfile != null) { try { randomfile.close(); } catch (IOException e) { } } } } private static void showAvailableBytes(InputStream in) { try { System.out.println("The number of bytes in the current byte input stream is: " + in.available()); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String filename="E://BaiYiShaoNian.txt"; JavaIO.readFileByBytes(filename); JavaIO.readFileByChar(filename); JavaIO.readFileByLine(filename); JavaIO.readFileByRandomAccess(filename); }}Java write files
package Genius idiot dream; import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStreamWriter;public class JavaIO2 { public static void main(String[] args) throws IOException { String Path="E://Genius idiot dream//JAVA"; File file=new File("E://Genius Idiot Dream//JAVA","BaiYiShaoNian.txt"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } /** * Three ways to write files in Java* */ FileOutputStream fos=null; BufferedWriter bw=null; FileWriter fw=null; int value=1000; try { fos=new FileOutputStream(new File(Path+"fos.txt")); long begin=System.currentTimeMillis(); for (int i=1; i<=value; i++) { fos.write(5); } long end=System.currentTimeMillis(); System.out.println("TheCostTime of FileOutputStream is : " + (end-begin)); fos.close(); bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8")); begin=System.currentTimeMillis(); for (int i=1; i<=value; i++) { bw.write(5); bw.newLine(); } bw.close(); end=System.currentTimeMillis(); System.out.println("TheCostTime of BufferedWriter is : " + (end-begin)); fw=new FileWriter(Path+"fw.txt"); begin=System.currentTimeMillis(); for (int i=1; i<=value; i++) { fw.write(5); } fw.close(); end=System.currentTimeMillis(); System.out.println("TheCostTime of FileWriter is : " + (end-begin)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fos.close(); //FileOutputStream bw.close(); //BufferedWriter fw.close(); //FileWriter } catch (Exception e) { e.printStackTrace(); } } }}The above summary of Java reading and writing file methods (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.