This article describes the methods of reading and writing files in Java programming. Share it for your reference, as follows:
What is the role of file read and write operations in Java?
When answering this question, you should first think of Java is just a language, our use tool, so the answer is clear, that is, write various foreign data into a certain file for storage; or read out its data from the file for us to use. Just like the following movie process, download a movie from the network resources and save it on your computer (write a file), and use the player to open it (read the file) when you want to watch it.
How to read and write files in Java?
Let’s first understand. There are two types of streams in Java, byte streams and character streams. The two base classes of byte streams are InputStream and OutputStream; the two base classes of character streams are Reader and Writer. The so-called file flow means that we cannot leave the stream when we operate the file. From this we can see that we want to use a certain class that must inherit one of the four base classes as above. Everything in Java is a class and everything is an object. Naturally, you will think of what types of file operations are:
The following four directly used classes:
Byte Stream: FileInputStream and FileOutputStream
Character stream: FileReader and FileWriter
It's easy to find the class. The rest is to find a way to implement it.
There are two options here, which involves how we choose the right way to read and write files?
Differences in selection criteria:
Read files in bytes, which are often used to read binary files, such as pictures, sounds, images and other files.
Read files in units of characters, and are often used to read files of text, numbers, etc.
As for whether to use a Buffer to encapsulate the file input and output stream, it depends on the size of the file. If it is a reading and writing large files, the Buffer bucket is selected to provide file reading and writing efficiency.
The following is a simple application example:
1. Use byte stream to read and write files directly:
Note: FileOutputStream(file, true); the true parameter in FileOutputStream(file, true); indicates that the original file is not overwritten, and the content is added directly after the file.
public class FileTest{static File file = new File("d:/test.txt");public static void main(String[] args){try{FileOutputStream out = new FileOutputStream(file, true);String s = "Hello,world!/r/n";out.write(s.getBytes());out.flush();out.close();//FileInputStream in = new FileInputStream(file);//byte [] b = new byte[20];//in.read(b, 0, b.length);//System.out.println(new String(b));//in.close();} catch (FileNotFoundException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}}}2. Use character streams to read and write files directly:
public class File03{static File file = new File("d:/test.txt");public static void main(String[] args){try{FileWriter fw = new FileWriter(file,true);fw.write("Hello,world!/r/n");fw.flush();fw.close();//FileReader fr = new FileReader(file);//int i=0;//String s ="";//while( ( i = fr.read() )!= -1)//{//s = s +(char)i;//}//System.out.println(s);} catch (FileNotFoundException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}}}Use of file read and write streams after being encapsulated with Buffer:
1. Read and write files after encapsulation of byte stream:
static File file = new File("d:/test.txt");public static void main(String[] args){try{// FileOutputStream out = new FileOutputStream(file, true);// BufferedOutputStream bout = new BufferedOutputStream(out);// String s = "I have a dream!";// bout.write(s.getBytes());// bout.flush();// bout.close();FileInputStream in = new FileInputStream(file);BufferedInputStream bin = new BufferedInputStream(in);byte[] b = new byte[15];bin.read(b);bin.close();System.out.println(new String(b));} catch (FileNotFoundException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}}}2. Read and write files after encapsulating the character stream:
public class File03{static File file = new File("d:/test.txt");public static void main(String[] args){try{// FileWriter fw = new FileWriter(file, true);// BufferedWriter bw = new BufferedWriter(fw);// String nextLine = System.getProperty("line.separator");// bw.write("Hello,world!" + nextLine);// bw.flush();// bw.close();FileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);int i = 0;String s = "";String temp = null;while((temp=br.readLine())!=null){s = s+temp;}System.out.println(s);} catch (FileNotFoundException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}}}I hope this article will be helpful to everyone's Java programming.