1.IO
1.1概述
流的本质:数据传输,方便直观的进行数据操作。
I:input 输入
O:output 输出
1.2分类
处理类型:字节流和字符流
数据流向:输入流和输出流
功能:节点流和处理流
节点流:直接处理数据
处理流:处理节点流
1.3四大抽象类
字节输入流:InputStream
字节输出流:OutputStream
字符输入流:Reader
字符输出流:Writer
1.4 文件流
1.4.1 FileInputStream
1.4.1.1 概述
打开文件,读取文件中的数据(位置)。
1.绝对位置:以系统根目录为准,如D:/xx/xxx/xx/a.txt
FileInputStream fis=new FileInputStream(“D:\a.txt”);
2.相对位置:
./ 表示当前目录
…/ 表示上级目录
…/…/ 上上级目录
FileInputStream fis=new FileInputStream("./src/b.txt");
字节输入流,按照字节的方式进行数据读取
1.4.1.2 常用方法
1.4.1.3 Read使用
read : 读取一个字节,并返回对应的ASCII码值,返回为int类型,如果到达文件末尾(读完了) 则返回-1
public static void main(String[] args){
//自动关闭资源
//创建字节输入流
try(FileInputStream fis=new FileInputStream("./src/b.txt")){
// read : 读取一个字节,并返回,返回为int类型,如果到达文件末尾(读完了) 则返回-1
int data=0;
while((data=fis.read())!=-1){
System.out.print((char) data);
}
}catch(IOException e){
e.printStackTrace();
}
}
1.4.1.4 Read重载使用
read方法重载:可以传递一个数组,一次读取会把该数组读满/读完,然后一次性返回,返回int类型,为当前读取的个数,如果达到文件末尾,返回-1.
数组相当于一个缓冲区,效率会有所提升。
public static void main(String args[]){
try(FileInputStream fis=new FileInputStream("./src/b.txt")){
//available:可读取的字节数
System.out.println(fis.available());
//数组容量并不是越大效率就越高,容量和数据大小刚好一致的时候,效率最高
byte[] bytes=new byte[fis.available()];
int temp=0;
while((temp=fis.read(bytes))!=-1){
// 把字节数组转换为字符串,可能出现数据重复问题
// System.out.print(new String(bytes));
//read返回的是当前次读取的元素个数,所以我们可以指定读多少,转换多少
// 数组 , 起始位置(包含) , 个数
System.out.print(new String(bytes,0,temp));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
1.4.2 FileReader
1.4.2.1 概述
FileReader:一次读取一个字符,两个字节,主要用于读取纯文本文件,解决乱码问题.
read():一次读取一个字符,返回ASCII,达到文件末尾返回-1
read(char[]):一次读取一个字符数数组,提高读取效率,返回本地读取的字符个数,到达文件末尾返回-1
1.4.2.2 使用方式
public static void main(String[] args){
try(FileReader fr=new FileReader("./src/b.txt");){
int temp=0;
while((temp=fr.read())!=-1){
System.out.print((char) temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
try(FileReader fr=new FileReader("./src/b.txt"); ){
int temp=0;
char[] chars =new char[10];
while((temp=fr.read(chars))!=-1){
System.out.print(new String(chars,0,temp));
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
1.4.3 FileOutputStream
1.4.3.1 概述
字节输出流,用于把数据写到指定文件中
如果指定文件不存在,会自动创建该文件,但是 不会创建目录(不会创建文件夹)
1.4.3.2 常用方法
1.4.3.3 构造方法
public static void main(String[] args) throws IOException{
//覆盖写入,当创建该文件的输出流对象的时候,就会把该文件中内全部内容清除
//FileOutputStream fos1=new FileOutputStream(“D:/a.txt”);
//追加写入,创建对象时,传入第二个参数true,不会清空该文件
FileOutputStream fos1 = new FileOutputStream(“D:/a.txt”,true);
fos1.write(98);
}
1.4.3.4 使用方式
public static void main(String[] args){
try(FileOutputStream fos=new FileOutputStream(“D:/a.txt”)😉{
//写出对应的ASCII码
fos.write(97);
byte[] bytes={97,98,99};
fos.write(bytes);
//不能直接写字符串
String str=“你好吗”;
//getBytes : 把该字符串转换为字节数组
fos.write(str.getBytes());
//刷新缓存,强制持久化
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
1.4.4 FileWriter
1.4.4.1使用方式
//用法和字节输出一致,只不过新增了可以写出字符串的方法重载
try(FileWriter fw=new FileWriter(“D:/a.txt”)){
//写出字符数组
char[] chars={‘a’,‘c’};
fw.write(chars);
//可直接写字符串
fw.write(“你好吗?”);
fw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
1.5 缓冲流
特点:
1.主要是为了提高效率而存在的,减少物理读取次数
2.提供readLine()、newLine()这样的便捷的方法(针对缓冲字符流)
3.在读取和写入时,会有缓存部分,调用flush为刷新缓存,将内存数据写入到磁盘
1.5.1 BufferedReader
字符输入缓冲流,为了解决效率问题
try( //创建字符输入流
FileReader fr=new FileReader("./src/day_01_IO/Test_01.java");
// 创建字符输入缓冲流对象
BufferedReader br=new BufferedReader(fr)😉{
//readLine :读取一行数据,返回读到的这一行的数据的内容,String,到达文件末尾返回null
String temp=null;
while((temp=br.readLine())!=null){
System.out.println(temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
1.5.2 BufferedWriter
字符输出缓冲流
try( // 创建字符输出流
FileWriter fw = new FileWriter(“D:/a.txt”);
//创建字符输出缓冲流
BufferedWriter bw =new BufferedWriter(fw)😉{
//写出
bw.write(“你好吗”);
//换行
bw.newLine();
bw.write(“你管得着吗”);
//刷缓存
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
1.6 转换流
try(FileInputStream fis = getInputStream(“D:/a.txt”);
//使用转换流把字节输入流转换为字符输入流
InputStreamReader isr=new InputStreamReader(fis)😉{
int temp=0;
while ((temp = isr.read()) != -1) {
System.out.print((char) temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//需求:根据文件路径,获取文件的输入流对象
public static FileInputStream getInputStream(String filePath) throws IOException{
return new FileInputStream(filePath);
}
1.7 打印流
1.7.1 概述
1.7.2 使用方式
PrintStream
PrintWriter
try ( //创建输出流
FileOutputStream fos=new FileOutputStream(“D:/a.txt”);
//封装为打印流
PrintStream ps=new PrintStream(fos)😉{
ps.println(123);
ps.println(“asdasd阿萨德”);
//System中的打印流,默认打印到控制台
System.out.println(“xxxx”);
//更改System中的打印流
System.setOut(ps);
// 以下所有打印操作,不再显示在控制台中,而是打印到指定文件中
System.out.println(“xxxxxxxxx”);
System.out.println(“你好吗”);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
1.8 数据流
1.8.1 概述
数据流 是为了不同平台之间数据读取的统一性,Linux,Windows等操作系统对数据进行存储的时候方式是不同的
为了解决之间的差异性,java提供了两个轻量级的方法,只要每个平台有java环境,就能保证数据的一致性
比如在进行网络协议传输的时候,是非常适用的
DataOutputStream
//创建对象
try {
DataOutputStream dos=new DataOutputStream(new FileOutputStream(“D:/a.txt”));
//写出数据
dos.writeByte(1);
dos.writeShort(12);
dos.writeInt(51);
dos.writeUTF(“阿萨德吉安市”);
dos.flush();
dos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataInputStream
public static void main(String[] args) throws Exception {
DataInputStream dis =new DataInputStream(new FileInputStream(“D:/a.txt”));
//必须保证顺序和类型一致
System.out.println(dis.readByte());
System.out.println(dis.readShort());
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
}