Basic concepts of IO streams
IO streams are used to process data transmission between devices
Java operates data through streams
The objects used by Java to operate streams are divided into two types of streams on IO packets: byte streams and character streams: input streams and output streams.
Abstract base class for byte streams: InputStream, OutputStream
Abstract base class for character streams: Reader, Writer
Note: The subclass names derived from these 4 classes are all suffixes of the subclass names with their parent class names.
For example: Subclass of InputStream: FileInputStream
For example: the subclass of Reader FileReader
If you create a FileWriter object, once the object is initialized, the file to be operated must be clearly defined, and the file will be created to the specified directory. If there is a file with the same name in the directory, it will be overwritten.
Demo:
package javase.day18; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { FileWriter fw=null; try { fw = new FileWriter("C://java_test//FileWriterTest.txt"); //Call the write method and write the string to the stream fw.write("alex test23"); //Fresh the data in the buffer in the stream object fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally{ try { if(fw!=null){ //Close the stream resource, but the internal buffer will be refreshed once before closing fw.close(); } } } catch (IOException e) { e.printStackTrace(); } } } } package javase.day18; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { FileWriter fw=null; try { fw = new FileWriter("C://java_test//FileWriterTest.txt"); //Call the write method to write the string to the stream fw.write("alex test23"); //Fresh the data in the buffer in the stream object fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally{ try { if(fw!=null){ //Close the stream resource, but the data in the internal buffer will be refreshed once before closing fw.close(); } } } catch (IOException e) { e.printStackTrace(); } } } }Print the source code of Java file Demo code:
package javase.day18; import java.io.FileReader; import java.io.IOException; public class FileReaderTest { public static void main(String[] args) { try { FileReader fr=new FileReader("C://java_test//SystemDemo.java"); char[] buf=new char[1024]; int num=0; while((num=fr.read(buf))!=-1){ System.out.println(new String(buf,0,num)); } } catch (IOException e) { e.printStackTrace(); } } } package javase.day18; import java.io.FileReader; import java.io.IOException; public class FileReaderTest { public static void main(String[] args) { try { FileReader fr=new FileReader("C://java_test//SystemDemo.java"); char[] buf=new char[1024]; int num=0; while((num=fr.read(buf))!=-1) { System.out.println(new String(buf,0,num)); } } catch (IOException e) { e.printStackTrace(); } } } Copy the file Demo code:
The method used for copy_1() is to read a character and write a character.
The method used by copy_2() is to read the characters into a character array at one time and finally write them to the target file again.
package javase.day18; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyText { public static void main(String[] args) { try { copy_1(); } catch (IOException e) { e.printStackTrace(); } } public static void copy_1() throws IOException{ FileWriter fw = new FileWriter("C://java_test//Copy_SystemDemo.java"); FileReader fr = new FileReader("C://java_test//SystemDemo.java"); int num=0; while((num=fr.read())!=-1){ fw.write(num); } fw.close(); fr.close(); } public static void copy_2() throws IOException{ FileWriter fw = new FileWriter("C://java_test//Copy_SystemDemo.java"); FileReader fr = new FileReader("C://java_test//SystemDemo.java"); int num=0; char[] buf=new char[1024]; while((num=fr.read(buf))!=-1){ fw.write(buf,0,num); } fw.close(); fr.close(); } } package javase.day18; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyText { public static void main(String[] args) { try { copy_1(); } catch (IOException e) { e.printStackTrace(); } } public static void copy_1() throws IOException{ FileWriter fw = new FileWriter("C://java_test//Copy_SystemDemo.java"); FileReader fr = new FileReader("C://java_test//SystemDemo.java"); int num=0; while((num=fr.read())!=-1){ fw.write(num); } fw.close(); fr.close(); } public static void copy_2() throws IOException{ FileWriter fw = new FileWriter("C://java_test//Copy_SystemDemo.java"); FileReader fr = new FileReader("C://java_test//SystemDemo.java"); int num=0; char[] buf=new char[1024]; while((num=fr.read(buf))!=-1){ fw.write(buf,0,num); } fw.close(); fr.close(); } } Buffer for character streams:
The emergence of buffers improves the read and write efficiency of data.
Corresponding classes: BufferedWriter, BufferedReader.
The buffer must be combined with the stream before it can be used.
The function of flow is enhanced based on the flow.
Basic rules of IO stream operation:
1. Clear the source and purpose:
Source: Input Stream, Reader
Purpose: OutputStream, Writer
2. Whether the data of the operation is plain text:
Yes: Character Stream No: Byte Stream is: (1) When using Reader for input character stream
(2) Use InputStream for input byte stream
(3) Use Writer for output character stream
(4) When using OutputStream for output byte stream
3. When the system is clear, then clarify which specific object to use:
Source device: memory, hard disk, keyboard destination device: memory, hard disk, console
IO operation tool class
[1] String fileReaderStringHandle(String fileName)
Read the file (specified by fileName) into a string;
[2] byte[] fileReaderByteHandle(String fileName)
Read the file (specified by fileName) into a byte array;
[3] void fileWriterHandle(String fileName, String text)
Writes a string (specified by text) to a file (specified by fileName).
IOUtil.java
import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.PrintWriter;public class IOUtil { /** * Read the file into a String and use FileReader+BufferedReader (providing readLine method) * * @param fileName * @return String */ public static String fileReaderStringHandle(String fileName) { StringBuilder sb = new StringBuilder(); try { BufferedReader in = new BufferedReader(new FileReader(new File( fileName).getAbsoluteFile())); try { String s; while ((s = in.readLine()) != null) { sb.append(s); sb.append("/n"); } } finally { in.close(); } } catch (IOException e) { throw new RuntimeException(e); } return sb.toString(); } /** * Use FileInputStream+BufferedInputStream to process files in a byte way* * @param fileName * @return byte[] */ public static byte[] fileReaderByteHandle(String fileName) { byte[] data = null; try { BufferedInputStream bf = new BufferedInputStream( new FileInputStream(fileName)); try { data = new byte[bf.available()]; bf.read(data); } finally { bf.close(); } } catch (IOException e) { throw new RuntimeException(e); } return data == null ? new byte[] {} : data; } /** * Write the specified text to a file named fileName* * @param fileName * @param text */ public static void fileWriterHandle(String fileName, String text) { try { PrintWriter out = new PrintWriter(new File(fileName) .getAbsoluteFile()); try { out.print(text); } finally { out.close(); } } catch (IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) throws IOException { System.out.print(fileReaderStringHandle("src/IOUtil.java")); for (byte b : fileReaderByteHandle("src/IOUtil.java")) System.out.print(b); fileWriterHandle("zj.txt", fileReaderStringHandle("src/IOUtil.java")); }}