IO
IO流
- 目标:如何操作文件内部的内容?读取文件中的内容,往文件中写内容?
- 解决:通过流
- 流: 一连串流动的数据,先入先出的顺序进行流动,管道,可以通过管道传输数据
- 数据源 -----数据---- 目的地
- 数据源: 数据来的地方
- 目的地: 数据去的地方
- 流的分类:
- 按照功能分:
- 节点流(真实在做读入写出)
- 功能流 (增强节点流的功能,提高节点流的性能)
- 流向分: (以程序为中心)
- 输入流:读入
- 输出流:输出
- 操作单元:
- 字节流:万能流
- 字符流
- 分类是相辅相成的
- io包下的内容都是与io相关的内容
- 字节输入流: InputStream 抽象类
- FileInputStream 文件字节输入流,专门用来读入文件中数据内容的
功能流(节点流): 基本数据类型流(Data)|数据处理流–>读写基本数据类型的数据+String - 是字节流的功能流
- DataInputStream 基本数据类型输入流 新增功能: readXXX()
- DataOutputStream 基本数据类型输出流 新增功能: writeXXX()
- 不能发生多态使用,因为有新增方法
- 先写出后读入
- 读入与写出的过程顺序要保持一致
- 文件存在,但是内容读不到,会出现异常EOFException
public class IODemo01 {
public static void main(String[] args) throws IOException {
//1.创建流
//FileInputStream(String name)
InputStream is = new FileInputStream("E:/test.txt");
//FileInputStream(File name)
InputStream is2 = new FileInputStream(new File("E:/test.txt"));
//2.读入 read() 如果读到末尾,返回-1
/*
* System.out.println((char)is.read()); System.out.println((char)is.read());
* System.out.println((char)is.read());
*/
//循环读入 但是每次读入一个字节内容,效率较低,可以一卡车一卡车读入
/*
* int num = -1; //存储当前这一次读入的数据 while((num =is.read())!=-1) {
* //先做读入,读到的数据赋值给num,然后再判断num的值是否为-1 System.out.println((char)num); }
*/
//一卡一卡读入,先创建一个卡车(字节),字节数组
int len = -1; //读入到car中的字节个数
byte[] car = new byte[2]; //1024进制,1024的整数倍可以
/*
* len = is.read(car); System.out.println(new String(car,0,len));
*/
//循环读入数据,每次读取一卡车
while((len=is.read(car))!=-1) {
System.out.println(new String(car,0,len));
}
//3.关闭
is.close();
}
}
public class DataDemo01 {
public static void main(String[] args) throws IOException {
//write("E://haha.txt");
read("E://haha2.txt");
}
//读入
public static void read(String path) throws IOException {
//1.创建流
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
//2.读入
int i = in.readInt();
boolean flag = in.readBoolean();
String s = in.readUTF();
System.out.println(i+"-->"+flag+"-->"+s);
//3.关闭
in.close();
}
//写出
public static void write(String path) throws IOException {
//1.创建流
DataOutputStream out = new DataOutputStream
(new BufferedOutputStream(new FileOutputStream(path)));
//2.写出
int i = 100;
boolean flag = false;
String str = "张三";
out.writeInt(i);
out.writeBoolean(flag);
out.writeUTF(str);
//刷出
out.flush();
//关闭
out.close();
}
}
字节输出流 节点流
- OutputStream 抽象类
- FileOutputStream 文件字节输出流
- 注意: 默认不追加覆盖,如果想要追加,构建流的构造器添加第二个参数boolean append, true为追加,默认false
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class IODemo02 {
public static void main(String[] args) throws IOException {
/*
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String name, boolean append)
创建一个向具有指定 name 的文件中写入数据的输出文件流。
FileOutputStream(File file)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(File file, boolean append)
*/
//1.选择流
OutputStream os=new FileOutputStream("E:/AAA.txt",true); //输出流指向的文件,不存在,系统会自动创建,但是不会创建文件夹
//2.写出 write(int) 写出一个字节
//os.write(97);
//write(byte[])
byte[] arr = "今天是周二,开会不要忘记了!!!".getBytes();
os.write(arr);
//3.刷出
os.flush();
//4.关闭
os.close();
}
}
文件拷贝:
数据源------> 程序-------> 目的地
import java.io.*;
public class CopyFile03 {
public static void main(String[] args) {
//1.选择流
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream("E:/AAA.txt");
os = new FileOutputStream("D:/AAA.txt",true);
//2.输入 输出
byte[] car = new byte[1024];
int len = -1;
while(-1!=(len=is.read(car))) {
os.write(car,0,len);
}
//3.刷出
os.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//4.关闭 后打开的先关闭
if(os!=null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is!=null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
字符流
只能读写纯文本内容
- 字符输入流 Reader FileReader
- 字符输出流 Writer FileWriter
import java.io.*;
public class IODemo04 {
public static void main(String[] args) throws IOException {
//1.创建流
Reader rd = new FileReader("E:/AAA.txt");
Writer rt = new FileWriter("E:/BBB.txt");
//2.读入写出
char[] car = new char[1024];
int len = -1;
while((len = rd.read(car))!=-1) {
rt.write(car);
}
//3.刷出
rt.flush();
//4.关闭
rt.close();
rd.close();
}
}
功能:
- 节点流: FileInputStream…FileReader…
- 功能流: 增强节点流的功能,提高节点流的性能
- 缓冲流: 提高读写效率…
- 使用: 缓冲流(节点流)
- 字节输入流: BufferedInputStream
- 字节输出流: BufferedOutputStream
- 字节缓冲流没有新增功能,可以发生多态
- 字符输入流: BufferedReader
- 新增功能 : readLine() 返回字符串
- 字符输出流: BufferedWriter
- 新增功能 : newLine() 换行符
- 因为字符缓冲流存在新增功能,不能发生多态
import java.io.*;
public class BufferedDemo05 {
public static void main(String[] args) throws IOException {
//testBufferedInputStream();
testBufferedReader();
}
//字符流缓冲流
static void testBufferedReader() throws IOException {
BufferedReader rd = new BufferedReader(new FileReader("E:/AAA.txt"));
BufferedWriter rt = new BufferedWriter(new FileWriter("E:/F.txt",true));
//2.读入写出
String str = null; //存储每行读入的内容
while((str = rd.readLine())!=null) {
rt.write(str);
rt.newLine(); //换行
}
//3.刷出
rt.flush();
//4.关闭
rt.close();
rd.close();
}
//字节流缓冲流
static void testBufferedInputStream() throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream("E:/AAA.txt"));
OutputStream os = new BufferedOutputStream(new FileOutputStream("E:/CCC.txt",true));
//2.输入 输出
byte[] car = new byte[1024];
int len = -1;
while(-1!=(len=is.read(car))) {
os.write(car,0,len);
}
//3.刷出
os.flush();
//4.关闭
os.close();
is.close();
}
}
功能流(节点流)
基本数据类型流(Data)|数据处理流–>读写基本数据类型的数据+String
- 是字节流的功能流
- DataInputStream 基本数据类型输入流 新增功能: readXXX()
- DataOutputStream 基本数据类型输出流 新增功能: writeXXX()
- 不能发生多态使用,因为有新增方法
- 先写出后读入
- 读入与写出的过程顺序要保持一致
- 文件存在,但是内容读不到,会出现异常EOFException
public class DataDemo01 {
public static void main(String[] args) throws IOException {
//write("E://haha.txt");
read("E://haha2.txt");
}
//读入
public static void read(String path) throws IOException {
//1.创建流
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
//2.读入
int i = in.readInt();
boolean flag = in.readBoolean();
String s = in.readUTF();
System.out.println(i+"-->"+flag+"-->"+s);
//3.关闭
in.close();
}
//写出
public static void write(String path) throws IOException {
//1.创建流
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
//2.写出
int i = 100;
boolean flag = false;
String str = "张三";
out.writeInt(i);
out.writeBoolean(flag);
out.writeUTF(str);
//刷出
out.flush();
//关闭
out.close();
}
}
Object对象流: 读写对象类型的数据
- ObjectInputStream 反序列化输入流 新增功能:readObject()
- ObjectOutputStream 序列化输出流 新增功能:void writeObject(Object obj)
- 不能发生多态,因为存在新增功能
- 序列化: 把对象数据转为可存储或者可传输的状态,这个过程称之为序列化
- 反序列化的顺序要与序列化的顺序保持一致
- 不是所有的类都能够序列化 ,需要实现空接口 java.io.Serializable
- 不是所有的属性都需要序列化 transient
- 如果父类有实现序列化,子类没有,子类内容可以进行序列化
- 如果子类实现序列化,父类没有,子类只能序列化子类内容,父类内容为默认值
import java.io.*;
import java.util.Arrays;
import com.shsxt.entity.Person;
public class ObjectDemo02 {
public static void main(String[] args) throws Exception {
out("heihei.txt");
in("heihei.txt");
}
//读入
public static void in(String path) throws FileNotFoundException, IOException, ClassNotFoundException {
//1.创建流
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));
//2.读入对象数据
Object obj = in.readObject();
int[] arr = (int[]) in.readObject();
System.out.println(obj);
System.out.println(Arrays.toString(arr));
//3.关闭
in.close();
}
//写出
public static void out(String path) throws FileNotFoundException, IOException {
//1.创建流
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
//2.写出对象数据
Person p = new Person("胡歌",30);
out.writeObject(p);
out.writeObject(new int[] {1,2,3});
//3.刷出
out.flush();
//4.关闭
out.close();
}
}
COMMOS IO
项目下新建文件夹lib,把jar包资源放入其中
- 选中资源右键build path->add to build path
- build path: 项目管理其他资源文件
import org.apache.commons.io.FilenameUtils;
public class CommonsIODemo01 {
public static void main(String[] args) {
/*FilenameUtils
getName():获取文件名
getExtension(String path):获取文件的扩展名
isExtension(String fileName,String ext):判断fileName是否是ext后缀名
*/
System.out.println(FilenameUtils.getName("D://haha.txt")); //haha.txt
System.out.println(FilenameUtils.getName("D://AAA")); //AAA
System.out.println(FilenameUtils.getExtension("D://haha.txt")); //txt
System.out.println(FilenameUtils.getExtension("D://AAA"));
System.out.println(FilenameUtils.isExtension("D://haha.txt","jpg"));
//获取一个目录下所有字内容,拿到所有的java文件
}
}
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class CommonsIODemo02 {
public static void main(String[] args) throws IOException {
/*
readFileToString(File file,String charset):读取文件内容,并返回一个String
writeStringToFile(File file,String content, String charset):将内容content写入到file中
copyDirectoryToDirectory(File srcDir,File destDir):文件夹复制
copyFile(File srcFile,File destFile):文件复制
*/
File file = new File("E:/a.txt");
File dest = new File("E:/aaa.txt");
System.out.println(FileUtils.readFileToString(file,"utf-8"));
System.out.println(FileUtils.readLines(file,"utf-8"));
FileUtils.writeStringToFile(file, "张三的歌儿", "utf-8", true);
FileUtils.copyFile(file, dest);
File srcDir = new File("E:/AAA");
File destDir = new File("E:/BBB");
FileUtils.copyDirectoryToDirectory(srcDir,destDir);
}
}