IO流
FIle类的使用
java.io.File类:文件和文件目录路径的抽象表示形式,与平台无关
File能新建、删除、重命名文件和目录,但File不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。
想要在Java程序中表示一个真实存在的文件和目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。
File对象可以作为参数传递给流的构造器。
构造器
常用方法
IO流原理及流的分类
文件流
操作步骤:
1、File类的实例化
2、FileReader流的实例化
3、读入的操作
4、资源的关闭
字符流:
//输入
@Test
public void Test1(){
FileReader fr = null;
try {
File file = new File("hello.txt");
fr = new FileReader(file);
int data;
while((data = fr.read()) != -1){
System.out.print((char)data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//输出:
@Test
public void Test2(){
FileWriter fw = null;
try {
File file = new File("hello1.txt");
fw = new FileWriter(file);
fw.write("Hello bro!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
字节流:
//图片复制:
@Test
public void Test3(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcFile = new File("12.jpg");
File destFile = new File("34.jpg");
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲流
提升流的读取和写入的效率。
通过内部定义了一个缓冲区(8192字节),先将内容读到缓冲区,再给read,这样减少了对系统IO资源的使用量。
装饰者模式,
File srcFile = new File("12.jpg");
File destFile = new File("34.jpg");
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
关闭时只需关闭处理流,因为他会自动关闭内部的节点流。
BufferedReader 有一个方法 readLine()
转换流
转换流提供了在字节流和字符流之间的转换。属于字符流。
InputStreamReader:将一个字节的输入流转换为字符的输入流
OutputStreamWriter:将一个字符的输出流转换成字节的输出流
InputStreamReader isr = new InputStreamReader(new FileInputStream("hello.txt"),"UTF-8");
第二个参数选择编码集
标准输入、输出流
打印流
数据流
对象流
//序列化
@Test
public void testObjectOutputStream(){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("一个String"));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//反序列化
@Test
public void testObjectInputStream(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String)obj;
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if(ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
自定义类要实现序列化需:
1、实现接口:Serializable
2、当前类提供一个全局常量:serialVersionUID
3、除了当前类需要实现Serializable接口之外,还必须保证其内部所有属性也必须是可序列化的(默认情况下,基本数据都是可序列化的)
随机存取文件流
NIO.2中Path、Paths、Files类的使用
Path 可以看成是File类的升级版本
Paths是Path的工具类
以上内容为尚硅谷网课的笔记。
网课链接附上