概念
IO流 传入传出流
I: input 输入
o : output 输出
分类
1.按照数据类型分类 分为 字节流byte 和字符流 char
- 字节流byte
- 字符流 char 出现大篇的汉子 用字符流
2.按照数据流向(输入输出的对象是Java 的内存)
- 输入流
- 输出流
3.按照功能分
- 节点流:直接操作的数据源
- 处理流:对节点流进行处理
节点流
inputStream 字节输入流
只用FileInputStram 字节输入流
FileInputstream fileInputstream=new FileInputstream(写绝对路径或者相对路径);
字节输入流
输入流的遍历 read 方法
read ()返回的是字符的ASCII ,返回-1就是读到了末尾
intread=0; 初始化 read read为-1时 程序结束
while ((read=fileInputstream.read())!=-1){ read 不等于-1的时候进入循环
System.out.print(read); 遍历 read
}
read (byte[] 缓冲区)
byte[]b=new byte[900];
int read fileInputstream.read(b);
System.out.println(new String(b,0,read));
System.out.println(read);
read fileInputstream.read(b);
System.out.println(new string(b,0,read))
System.out.println(read);
byte 数组返回的是本次读取的个数 返回-1 时结束
循环遍历 写法
int read=0;
while ((read=fileInputstream.read(b))!=-1){
System.out.println(new String(b,offset:0,read));
Reader 字符传输流
FileReader fileReader=new FileReader()
FileoutputStream 字节输出流 输出流 找不到文件会自动创建文件,但是不能创建文件夹。
write(int): 写ACSII值 传到文件中的字母 如果想追加 在构造方法中加truen 覆盖是false 默认是覆盖。
try(FileOutputstream fileOutputstream=new Fileoutputstream(name:"D:/b.txt",append:false)){
fileoutputstream.write(b:97)
fileOutputstream.write(b:98);
fileoutputstream.write(b:99);
String a="你好啊";
byte[]bytes a.getBytes();//获取字节数组
Wirte 字符输出流
FileWirte
处理流
缓冲流
分类 | 字节流 | 字符流 |
文件输入流 | BufferedInputStream | BufferedReader |
文件输出流 | BufferedOutputStream | BufferedWriter |
字符流
readLine()方法:一次性读取一行数据,当返回nu几L时,说明读取到末尾
String s =""
while ((s=bufferedReader.readLine())!=nUll){
System.out.println(s)
newLine() : 换行、