IO流
File类
File(File parent, String child)
从父抽象路径名和子路径名字符串创建新的 File实例。
File(String pathname)
通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
File(String parent, String child)
从父路径名字符串和子路径名字符串创建新的 File实例。
public static final String separator
与系统相关的默认名称 - 分隔符字符,以方便的方式表示为字符串。 该字符串包含一个字符,即separatorChar 。
File常用方法(File类中的方法只是对文件/文件夹进行操作,并没对文件内容进行操作!!!)
Files. exists():检测文件路径是否存在。
Files. createFile():创建文件。
Files. createDirectory():创建文件夹。
Files. delete():删除一个文件或目录。
Files. copy():复制文件。
Files. move():移动文件。
Files. size():查看文件个数。
Files. read():读取文件。
Files. write():写入文件。
BIO,NIO,AIO 有什么区别?
BIO:Block IO 同步阻塞式 IO,就是我们平常使用的传统 IO,它的特点是模式简单使用方便,并发处理能力低。
NIO:Non IO 同步非阻塞 IO,是传统 IO 的升级,客户端和服务器端通过 Channel(通道)通讯,实现了多路复用。
AIO:Asynchronous IO 是 NIO 的升级,也叫 NIO2,实现了异步非堵塞 IO ,异步 IO 的操作基于事件和回调机制
IO流分类
1.按流向
输入流:InputStream
输出流:OutputStream
2.数据单位
字节流(byte 8bit)
字符流(char 16bit)
3.按流的角色
节点流
处理流
抽象基类 字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer
/**
* 从文件中读入数据到内存操作时:
* 1.read()方法读入到末尾返回-1
* 2.为了保证流一定能正常关闭,采取try-catch-finally进行处理
* 3.读入文件一定要存在,否则会报FileNorFoundException
*/
@Test
public void test1() {
File file1 = null;
FileReader fileReader = null;
try {
file1 = new File("Hello.txt");
fileReader = new FileReader(file1);
int i = fileReader.read();
while (i != -1) {
System.out.print((char) i);
i = fileReader.read();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test2() {
File file = null;
FileReader fileReader = null;
try {
file = new File("Hello.txt");
fileReader = new FileReader(file);
char[] chars = new char[5];
int len;
while ((len = fileReader.read(chars)) != -1) {
//方式1:
//错误的
// for (int i = 0; i < chars.length; i++) {
// System.out.print(chars[i]);
// }
//正确的
// for (int i = 0; i < len; i++) {
// System.out.print(chars[i]);
// }
//方式2:
//错误的
// String string = new String(chars);
// System.out.println(string);
//正确的
String string = new String(chars,0,len);
System.out.println(string);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 从内存中写出数据到硬盘文件
* 1.写出过程中,File("xxx")指定文件可以不存在,会自动创建
* 2.File("xxx")存在:
* 2.1:如果使用File(file)/File(file,false),会进行原有文件的覆盖
* 2.2:如果使用File(file,true),则会在原有文件的基础上进行追加内容
*/
@Test
public void test1() {
File file = null;
FileWriter fileWriter = null;
try {
file = new File("Hello1.txt");
fileWriter = new FileWriter(file);
fileWriter.write("我是好人!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 将文件中的数据读入内存,然后写出到硬盘中的文件
*/
@Test
public void test1() {
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
File rfile = new File("Hello.txt");
File wfile = new File("Hello2.txt");
fileReader = new FileReader(rfile);
fileWriter = new FileWriter(wfile);
char[] chars = new char[5];
int len;
while ((len = fileReader.read(chars)) != -1) {
fileWriter.write(chars, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileWriter!=null){
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fileReader!=null){
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test1(){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
File rfile = new File("C:\\Users\\xxx\\Desktop\\Screenshot.png");
File wfile = new File("C:\\Users\\xxx\\Desktop\\shot.png");
fileInputStream = new FileInputStream(rfile);
fileOutputStream = new FileOutputStream(wfile);
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read(bytes)) != -1){
fileOutputStream.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
缓冲流作用在已有流的基础上的(不仅节点流),加快读写速度,代码和节点流的一样。
转换流是讲字节流转成字符流,带内存,再将字符流转成字节流到硬盘文件(FileInputStream----->FileReader----->内存----->FileWirter----->FileOutputStream)
对象流
@Test
//序列化
//将内存中的对象保存到磁盘中
public void test1(){
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("hi.txt")));
objectOutputStream.writeObject(new String("我爱中国"));
objectOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(objectOutputStream != null){
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
//反序列化
//从磁盘中将对象还原到内存
public void test2(){
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(new File("hi.txt")));
Object readObject = objectInputStream.readObject();
System.out.println(readObject);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if(objectInputStream != null){
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
序列化机制:自定义类需要实现Serializable接口,并显示定义全局常量serialVersionUID,属性要保证是可序列化的,被static/transient修饰的属性是不能被序列化