1.IO Flow Overview
2. The OutputStream
output stream of bytes
OutputStream
Class is abstract and can not be used directly, the need to create subclasses
2.1 FileOutputStream
file output stream of bytes
- Role: theRAMThe databyteThe way to writeHard disk filein
- Construction method:
effect:
- Create an
FileOutputStream
Object - The method of construction of the file / path, creating an empty file
- We will
FileOutputStream
object points to good empty file creation
FileOutputStream(String name) 写入数据的目的地是一个文件的路径
FileOutputStream(File file) 写入数据的目的地是一个文件
- Step output stream of bytes used
- Create a create a
FileOutputStream
object constructor to fill in the destination of write data
FileOutputStream fos01 = new FileOutputStream("G:\\Java\\测试文件夹\\a.txt");
- Create a call
FileOutputStream
objectwrite
methods, writes the data to a file
fos01.write(12);
- Release resources
fos01.close();
- Time to write more bytes methods
Method a: write the entire byte array
byte[] b = {97,98,99,100};
fos01.write(b);
If the first byte is a positive number (0-127), then the time will display the ASCII table query
if the first byte is negative, then the first byte and the second byte will form a Chinese, when will query the system displays the default code table
Method two: writing a portion of a byte array
byte[] b = {97,98,99,100,121};
fos01.write(byte[] b,int off,int leng); 写入字节数组b的从off开始,长度为leng的数据
package FileOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo01 {
public static void main(String[] args) throws IOException {
FileOutputStream fos01 = new FileOutputStream("G:\\Java\\测试文件夹\\b.txt");
byte[] b = {97,98,99,100,121};
fos01.write(b);
fos01.write(b,1,3);
fos01.close();
}
}
Act III:
Using the
getBytes()
method, the string into a byte array
package FileOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo02 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("G:\\Java\\测试文件夹\\c.txt");
byte[] b = "你好".getBytes();
fos.write(b);
fos.close();
}
}
-
Additional writing data (writing) and line feed
-
Writing
the construction method:
FileOutputStream(String name,boolean append) 写入数据的目的地是一个文件的路径,append为true则不会覆盖原文件,继续在原文件中续写,append为false则覆盖原文件
FileOutputStream(File file,boolean append) 写入数据的目的地是一个文件,append为true则不会覆盖原文件,继续在原文件中续写,append为false则覆盖原文件
- Wrap: Write a newline
windows:\r\n
Linux:/n
Mac:/r
3. The InputStream
input stream of bytes
InputStream
Class is abstract and can not be used directly, the need to create subclasses
3.1 FileInputStream
File input stream of bytes
- Role: theHard disk fileThe databyteThe way to readRAMin
- Construction method:
effect:
- Create an
FileInputStream
Object - Will
FileInputStream
object to a file read
FileInputStream(String name) 读取文件的数据源是一个文件的路径
FileInputStream(File file) 读取文件的数据源是一个文件
- Byte input stream using the steps
- Create a create a
FileInputStream
object constructor to fill in the data source to read the file
FileInputStream fos = new FileInputStream("G:\\Java\\测试文件夹\\c.txt");
- Create a call
FileInputStream
objectread()
methods, read the file
fos.read(); 读取文件中的一个字节并返回,并且将指针后移一位,读取到文件的末尾返回-1
- Release resources
fos.close();
import java.io.FileInputStream;
import java.io.IOException;
public class Demo02 {
public static void main(String[] args) throws IOException {
FileInputStream fos = new FileInputStream("G:\\Java\\测试文件夹\\c.txt");
fos.read();
System.out.println(fos.read());
fos.read();
System.out.println(fos.read());
fos.read();
System.out.println(fos.read());
fos.read();
System.out.println(fos.read());
fos.close();
}
}
- Input stream of bytes read cycle all bytes
FileInputStream fos = new FileInputStream("G:\\Java\\测试文件夹\\a.txt");
int len = 0;
while ((len = fos.read()) != -1) {
System.out.println(len);
}
Not
the while (fos.read () = -. 1!) {
System.out.println (fos.read ());
}
since each execution fos.read (), the pointer will move after a
- The method of reading a plurality of bytes
FileInputStream file = new FileInputStream("G:\\Java\\测试文件夹\\a.txt");
byte[] bytes = new byte[2]; 创建一个字节数组,用于盛放多个字节
int len = file.read(bytes); 读取字节数组容量的字节个数,返回值为字节长度
System.out.println(len); 打印字节长度
System.out.println(Arrays.toString(bytes)); 将数组按照默认格式变成字符串
System.out.println(new String(bytes)); 创建字符串三种构造方法之一,根据字节数组的内容,来创建字符串
example:
package FileInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) throws IOException {
FileInputStream file = new FileInputStream("G:\\Java\\测试文件夹\\a.txt");
byte[] bytes1 = new byte[2];
int len1 = file.read(bytes1);
System.out.println(len1);
System.out.println(new String(bytes1));
System.out.println("============================");
byte[] bytes2 = new byte[2];
int len2 = file.read(bytes2);
System.out.println(len2);
System.out.println(new String(bytes2));
System.out.println("============================");
byte[] bytes3 = new byte[2];
int len3 = file.read(bytes3);
System.out.println(len3);
System.out.println(new String(bytes3));
System.out.println("============================");
byte[] bytes4 = new byte[2];
int len4 = file.read(bytes4);
System.out.println(len4);
System.out.println(new String(bytes4));
}
}
-
principle:
-
All bytes read cycle optimization:
byte[] bytes = new byte[1024];
int len = 0;
while ((len=file.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
package FileInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) throws IOException {
FileInputStream file = new FileInputStream("G:\\Java\\测试文件夹\\a.txt");
/* byte[] bytes1 = new byte[2];
int len1 = file.read(bytes1);
System.out.println(len1);
System.out.println(new String(bytes1));
System.out.println("============================");
byte[] bytes2 = new byte[2];
int len2 = file.read(bytes2);
System.out.println(len2);
System.out.println(new String(bytes2));
System.out.println("============================");
byte[] bytes3 = new byte[2];
int len3 = file.read(bytes3);
System.out.println(len3);
System.out.println(new String(bytes3));
System.out.println("============================");
byte[] bytes4 = new byte[2];
int len4 = file.read(bytes4);
System.out.println(len4);
System.out.println(new String(bytes4));*/
byte[] bytes = new byte[1024];
int len = 0;
while ((len=file.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
}
}