Article directory
1. Text files
(1) Using Scanner to read the contents of the file and output them to the program
public static void main(String[] args) throws IOException{ File file=new File("D:\\photo\\New Text Document.txt"); try( Scanner input=new Scanner(file); ){ while(input.hasNext()) { System.out.println(input.nextLine()); } } }
(2) Write data to text files using PrintWriter
public static void main(String[] args) throws IOException{ File file=new File("D:\\photo\\New Text Document.txt"); try( PrintWriter output=new PrintWriter(file); ){ output.print("hello world"); } }
Using the PrintWriter class, if the file does not exist, the PrintWriter's constructor creates a new file. If the file exists, the current content of the file will be discarded without confirming with the user and written to the data in output.
2. Binary files
(1) FileInputStream and FileOutputStream are two classes that can only store and read data of type int.
public static void main(String[] args) throws IOException{ File file=new File("D:\\photo\\New Text Document.dat"); //Write binary files try( FileOutputStream output=new FileOutputStream(file); ){ for(int i=0;i<10;i++) output.write(i); } //Read binary files try( FileInputStream input=new FileInputStream(file); ){ int value; while((value=input.read())!=-1) System.out.print(value+" "); } }
(2) Basic type values and character types can be manipulated using DataInputStream and DataOutputStream
public static void main(String[] args) throws IOException{ File file=new File("D:\\photo\\New Text Document.dat"); //Write binary files try( DataOutputStream output=new DataOutputStream(new FileOutputStream(file)); ){ output.writeByte(123); output.writeShort(123); output.writeInt(123); output.writeLong(1234567); output.writeFloat(3.14f); output.writeDouble(3.14); output.writeBoolean(true); output.writeChar('a'); output.writeChars("hello word"); output.writeUTF("George"); //Write a string in UTF format } //Read binary files try( DataInputStream input=new DataInputStream(new FileInputStream(file)); ){ System.out.println(input.readByte()); System.out.println(input.readShort()); System.out.println(input.readInt()); System.out.println(input.readLong()); System.out.println(input.readFloat()); System.out.println(input.readDouble()); System.out.println(input.readBoolean()); System.out.println(input.readChar()); System.out.println(input.readLine()); //Read a line of characters from the input stream } }
(3) BufferedInputStream and BufferedOutputStream classes can improve the speed of input and output by reducing the number of disk reads and writes.
When using Buffered Input Stream, the whole data on the disk is read into the memory buffer at one time, and then a single data is transferred to the program in the buffer.
When using Buffered Output Stream, individual data is first written to the buffer in memory, and when the buffer is full, the data in the buffer is written to disk at one time.
DataOutputStream output=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); DataInputStream input=new DataInputStream(new BufferedInputStream(new FileInputStream(file)));