Java I/O system (input/output stream) Flow characteristics 1. Contains liquid (data) 2. It has the classification of direction (read or write) Flow: input stream and output stream input stream: input stream in IO packet inherits from abstract class InputStream or reader output stream: input stream in IO package inherits from abstract class OutputStream or writer byte stream and character stream
Note: 1 bytes represents the data size stored in 1 English words, and a Chinese character accounts for two bytes
1. Byte stream: Transmitted as the smallest unit in bytes, inherited from the abstract class InputStream or outputstream, used to process the binary file, InputStream the parent class to read the byte stream, OutputStream to the parent class that writes the byte stream. Low-level byte stream: divided into 3 categories
FileInputStream and FileOutputStream for read and write operations on binary files. Bytearrayinputstream and Bytearrayoutputstream for read and write operations to byte arrays of memory buffers PipedInputStream and PipedOutputStream of Read and write operations to thread pipelines
Advanced byte stream: Divided into 3 major categories
Filter Stream class: Pre-preprocess the output before the data is actually written to the output stream, or post-process the input after reading the data. are subclasses of FileInputStream and FileOutputStream.
Object flow: Includes ObjectInputStream and ObjectOutputStream
Student stu=new Student(); FileOutputStream fos=new FileOutputStream(new File("f:\\user.txt")); ObjectOutputStream os=new ObjectOutputStream(fos); os.writeObject(stu); os.close(); 将实例化的Student类的对象stu写出
Merge: The Scquenceinputstream class can implement merge operations for two files.
2. Character stream: In the smallest unit of char transmission, inherited from the abstract class reader or writer
A character stream is used to process a text file, and all character streams inherit from the abstract class reader and writer two parent classes. are performed in a character unit.
3. Node stream: A stream that can read and write data directly from a specific data source. The read () method derived from InputStream or reader. The writ () method derived from OutputStream or writer. Bufferedinputstream class: The data in the memory buffer is processed. 4. Object Flow: Java provides an object serialization mechanism that enables ObjectInputStream and ObjectOutputStream to complete object-based reading and writing.
Write data using the input buffered stream Bufferediuputstream:
File file=new File("f:\\test.txt");//找到文件,再覆盖 byte[] b=new byte[(int) file.length()]; BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file)); int i=0; while((i=bis.read(b))!=-1){ System.err.println(new String(b,"gbk"));
To read a file using BufferedReader:
File file=new File("f:\\test3.txt"); BufferedReader brd=new BufferedReader(new FileReader(file)); System.out.println(brd.readLine());//读一行 int i=0; while((i=brd.read())!=-1){ System.err.print((char)i);
To read a file using FileInputStream:
File file=new File("f:","int.java"); FileInputStream fis=new FileInputStream(file); //读取文件 System.out.println(file.length()); int n=0; byte[] b=new byte[(int) file.length()]; while((n=fis.read(b))!=-1){//读取完毕 System.out.println(new String(b,0,n)); System.out.println();
To read a file using FileReader:
File file=new File("f:\\test3.txt"); FileReader fr=new FileReader(file); int n=0; while((n=fr.read())!=-1){ System.out.print((char)n);
Write Data using FileWrite:
//File file=new File("f:\\test3.txt");//直接在f盘中创建一个test.txt FileWriter fw=new FileWriter(new File("f:\\test3.txt"),true);//true代表可以继续追加,不覆盖 String s="明天星期3\r\n"; //s.getBytes();//将字符形式转换成Byte形式 fw.write(s);//写入 fw.close();
Copy of the file input and output stream:
File file=new File("f:\\1.jpg");//找到文件,否则就新建一个文件 File file2=new File("f:\\test2.jpg");//移到test2.jpg中 FileInputStream fis=new FileInputStream(file);//将文件1.jpg输入流导入 FileOutputStream fos=new FileOutputStream(file2);//将文件test2.jpg输出流导出 int n=0; while((n=fis.read())!=-1){//开始读取 fos.write(n);//开始写入 } fis.close(); fos.flush(); fos.close();
Upload Avatar
File file=new File(student.getHeadPortrait()); FileInputStream fis=new FileInputStream(file); File file2=new File("f:\\demo\\"+student.name+".jpg");//f:\\文件夹名称为:固定格式,进入哪个文件夹 if(file2.exists()){//存在,看Api file2.createNewFile();//创建文件} FileOutputStream fos=new FileOutputStream(file2); int n=0; while((n=fis.read())!=-1){//开始读取 fos.write(n);//开始写入 } fis.close(); fos.flush(); fos.close();
Io operation steps:
1. Build flow 2. Operation Flow 3. Close the Stream
The file class has 4 constructor method 1.public file (String pathname): Creates one file object associated with the specified path name.
File file=new File("f:\\test3.txt");
2.public file (String parent,string child): Creates a file object with the specified parameters.
File file=new File("f:","test3.txt");
3.public file (String parent,string child): The same as the previous one, except that the directory is a File object instead of a character. 4.public file (Uri uri): Creates a file object with the given Java.net.URI object. Method
1.file.mkdir (); Create directory File.getcanonicalpath () gets the current path
File file=new File("f://demo");//在f盘中创建demo的文件 file.mkdir();//创建目录 System.out.println(file.getCanonicalPath());//获取当前路径
2.boolean createnewfile (): Create file; Boolean exists (): Determines whether a file or directory exists
File file2=new File("f:\\demo\\"+student.name+".jpg");//f:\\文件夹名称为:固定格式,进入哪个文件夹 if(file2.exists()){//存在,看Api file2.createNewFile();}
Java I/O system (input/output stream)