One, foreword
These odd flow in understanding as well as the odd strange, is to use delicious ┗ | ` O ‘| ┛ ao ~ ~
IO flow overview
I indicates input, O indicates output indicates input and output streams.
Flow is an abstract concept and a general term for data transmission, that is, data transmission between devices is called flow
Data transmission. The essence of a stream is data transmission.
As we all know, program execution is loaded into memory.
- IO streams are used to deal with the problem of transferring data between devices
IO streams can be broadly classified into two categories:
① According to data flow
- Input stream: Read data
- Output stream: Write data
② According to data type
- Byte input stream; Byte output stream
- Character input stream; Character output stream
Generally, IO streams are divided by data type
Text files generally use character streams, other byte streams
Byte streams are universal streams that can be used for any type of file.
Byte stream write data
1) InputStream:
- This abstract class is a superclass of all the classes that represent the input byte stream.
(2) the OutputStream:
- This abstract class is the superclass of all classes that represent a byte output stream.
Their subclasses are all suffixed by their parent class:
Since it is an abstract class, we write and read data using direct subclasses.
FileOutputStream and FileInputStream, the former used to write data, the latter used to read data.
FileOutputStream(String name): Creates a file input stream and writes to the file with the specified name
Public class FileOutputDemo {public static void main(String[] args) throws IOException {// Create a byte output stream object, FileOutputStream fos=new FileOutputStream("E:\MyTest\kong.txt"); File f1=new File("E:\MyTest\kong.txt"); System.out.println(f1.createNewFile()); // Write throws IOException, so its method throws IOException. Catch with fos. Write (49); //1 fos.write(48); //0 fos.write(48); //0 byte arr[]=new byte[10]; fos.write(arr); Fos.close (); }}Copy the code
E:\MyTest\kong. TXT, this means THAT I created the kong. TXT directory under MyTest module in E disk, when the file
The existing storage will fail to create (false) and return Boolean
Creating an object does three things:
- Call system functions to create files.
- Create a byte output stream object.
- Let the byte output stream point to the created file.
Any object involved in the creation of IO operations must free resources.
Byte stream writing data in three ways:
import java.io.*; Public class FileOutputStreamDemo02 {public static void main(String[] args) throws IOException {// Create an input stream object FileOutputStream fos=new FileOutputStream("E:\MyTest\kong03.txt"); FileOutputStream fos1=new FileOutputStream(new File("E:\MyTest\kong03.txt")); // The first way is to write fos.write(97); fos.write(98); fos.write(99); Byte []bys={97,98,99,100}; //abcd fos.write(bys); Byte []bys1="abcd".getbytes (); fos.write(bys1, 1, 3); // release the resource fos.close(); }}Copy the code
Two minor problems with byte stream writing data:
Given the above operation, it is not difficult to notice that the data is written without line breaks, which looks very uncomfortable.
① Word stream input data newline problem:
- In Windows: \r\n
- Linux:\n
- mac:\r
Just add the corresponding line feed when writing data
② How to append byte stream write data
Simply add true after creating the specified path.
public FileOutputSteam(String name,boolean append)
Copy the code
import java.io.FileOutputStream; import java.io.IOException; public class FileTextZJ { public static void main(String[] args) throws IOException { FileOutputStream fos =new FileOutputStream("E:\MyTest\kong04.txt",true); for (int i=0; i<5; i++){ fos.write("hello\r\n".getBytes()); } fos.close(); }}Copy the code
The following result is executed twice:, which achieves the append effect we want.
Byte stream data
1. Read one byte at a time
FileInputStream(String name)// Write from whereCopy the code
When no data reaches the end of the file, -1 is returned, so just the return value is used
public class FileInputStreamDemo{
public static void main(String [] args) throw IoException{
FileInputStream fis=new FileInputStream("E:MyByte\kong02.txt");
int by;
while(by=fis.read()!=-1){
System.out.println((char)by);
}
fis.close();
}
}
Copy the code
2. Read one array at a time
import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamDemo01 { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("E:\MyTest\kong1.txt"); byte[]bys=new byte[1024]; int len; while((len=fis.read())! =-1){ System.out.println(new String(bys,0,len)); } fis.close(); }}Copy the code
Character buffer stream
- BufferedWriter
- BufferedReader
Buffered stream parameters require a FileWriter or FileReader object
Character buffer streams write files
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterDemo1 { public static void main(String[] args) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter("E:\MyText\KongChao.txt")); bw.write("hello\r\n"); bw.write("world\r\n"); bw.close(); }}Copy the code
The character buffer stream reads data
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("E:\MyTest\kong.txt"));
int ch;
while((ch=br.read())!=-1){
System.out.print((char)ch);
}
br.close();
}
}
Copy the code
Character buffered stream features
- BufferedWriter:
void newLine(); // line breakCopy the code
- BufferedReader:
Public String readLine(): Reads a line of file, excluding newlines, terminators, or null if the stream is at the end of the fileCopy the code
A combination of input and storage will be used
import java.io.*; Public class BufferedWriterDemo1 {public static void main(String[] args) throws IOException {// Create a character input stream BufferedWriter bw = new BufferedWriter(new FileWriter("E:\MyTest\KongChao.txt")); Br = new BufferedReader(new FileReader("E:\MyTest\copy.txt")); String line; while((line=br.readLine())! =null){ bw.write(line); bw.newLine(); bw.flush(); } // Close the resource bw.close(); br.close(); }}Copy the code
\