目录
IO流
1、File类
1.1、创建File类对象(三种常用)
new File(String pathname)
- pathname参数:要创建的文件存放的路径名
new File(String parent,String child)
- parent参数:指定要创建的文件放在哪个路径下(child的父路径)
- child参数:要创建的文件的文件名
new File(File file,String child)
- 和第二种方式一个道理
1.2、常用方法
//getAbsolutePath:获取绝对路径
//getPath:将抽象路径转换为字符串--相对路径
//getParent:返回抽象路径名的父路径,并将父路径转换为字符串
//getName:返回抽象路径最后一个文件或者文件夹名称
//length:返回具体文件的长度--long
//lastModified:上次修改时间--时间戳long
//list:获取指定目录下的文件或者文件目录名称数组
//listFiles:获取指定目录下的文件或者文件目录名称file数组
String[] list = file.list();
File[] files = file.listFiles();
//renameTo:就是把前面的文件移动到后面指定的位置并重名为文件名
File file1 = new File("hello.txt");
File file2 = new File("D:\\java自学资料\\Java基础\\IO流\\hi.txt");
boolean renameTo = file2.renameTo(file1);
//exists:判断文件或者文件目录是否存在
//isDirectory:判断是否是一个文件目录
//isFile:判断是否是一个文件
//canWrite:是否能写
//canRead:是否能读
//isHidden:判断该文件是否被隐藏
//在硬盘中文件或者文件目录的创建,而不是File构造器对象的创建
// createNewFile:创建文件
// delete:删除文件(不经过回收站)
// mkdir:创建文件目录
// mkdirs:创建文件目录包括上级不存在的目录
2、IO流
2.1、IO流的分类
标准 | ||
---|---|---|
数据流向 | 输入流 | 输出流 |
操作单位 | 字符流 | 字节流 |
流的角色 | 文件流 | 缓冲流 |
2.2、流的体系结构
抽象基类 | 文件流 | 缓冲流 |
---|---|---|
Reader | FileReader | BufferedReader |
Writer | FileWriter | BufferedWriter |
InputStream | FileInputStream | BufferedInputStream |
OutputStream | FileOutputStream | BufferedOutputStream |
2.3、文本文件操作
1、文件流
读文件
-
根据指定路径获取并创建File对象
File file = new File("hello.txt");
-
创建管道(文件流)
FileReader fileReader = new FileReader(file);
-
读文件(调用文件流的
read
方法)int data=0; while ((data=fileReader.read())!=-1){ System.out.println((char) data); }
-
关闭流资源
fileReader.close();
-
改进
File file = new File("hello.txt"); fileReader = new FileReader(file); //定义一个具有容量的容器 char[] cbuff=new char[5]; int len; while ((len=fileReader.read(cbuff))!=-1){ //方式一:abcde hello 123 lo多输出了“lo” //原因:定义了长度为5的字符数组,一次从文件中读5个字符; // 第三次读文件,读取剩下的“123”,不够5个字符,因此第二次读的“hello”只覆盖了“hel”还剩“lo”没有被覆盖 // 总结:它读文件时,只会将原来字符数组的数据覆盖掉,而不是全部清空!! /*for (int i=0;i<cbuff.length;i++){ System.out.print(cbuff[i]); }*/ //方式二: for (int i = 0; i < len; i++) { System.out.print(cbuff[i]); }
写文件(说明)
不管创建File类对象的文件名在物理磁盘上存在与否
- 都可以写数据并创建该文件(文件名不存在对应的物理磁盘中)
- 或者在原存在的文件中直接覆盖(续写)数据(文件存在对应的物理磁盘中)
而写数据时的覆盖或者续写取决于“创建流对象的构造器
- 覆盖:false(默认)
- 续写:true
-
创建一个文件File对象(磁盘上存不存在都可以)
- 不存在:在磁盘上创建该文件来写
- 存在:覆盖或者续写
-
创建管道
fileWriter = new FileWriter(file,true);
-
调用管道对象的
write
方法fileWriter.write("You need to have a dream!");
-
关闭流资源
2、缓冲流
复制文本文件
-
创建File对象、管道、缓冲管道
bufferedReader = new BufferedReader(new FileReader(new File("hello.txt"))); bufferedWriter = new BufferedWriter(new FileWriter(new File("hello3.txt")));
-
复制文件
- 一个字符一个字符读
- 整行来读
//方式一 int len; char[] chars = new char[10]; while ((len=bufferedReader.read(chars))!=-1){ bufferedWriter.write(chars,0,len); } //方式二 String data; while ((data=bufferedReader.readLine())!=null){ bufferedWriter.write(data); }
-
关闭流资源(只需要关外层即可,关外层时已自动帮助我们关闭内存了)
2.4、非文本文件操作
1、文件流
使用字节流来操作非文本文件,也可以操作文本文件(但是可能会出现乱码)
复制图片
-
创建要复制和复制后保存的File对象
File fileR = new File("屏幕截图 2022-05-19 120736.png"); File fileW = new File("复制后的图片.png");
-
创建管道
fileInputStream = new FileInputStream(fileR); fileOutputStream = new FileOutputStream(fileW);
-
复制图片(先读入,再写出)
int len; byte[] bytes = new byte[5]; while ((len=fileInputStream.read(bytes))!=-1){ fileOutputStream.write(bytes,0,len); }
-
关闭流资源
2、缓冲流
- 创建文件File对象、造管道、造缓冲流
- 复制文件
- 关闭流
3、其他流
3.1、转换流
1、介绍
实现字节流与字符流之间的转换
- InputStreamReader:将字节流转换成字符流
- OutputStreamWriter:将字符流转换成字节流
2、操作
public void test02(){
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
try {
File file1 = new File("hello.txt");
File file2 = new File("hello_gbk.txt");
FileInputStream fileInputStream = new FileInputStream(file1);
FileOutputStream fileOutputStream = new FileOutputStream(file2);
inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
outputStreamWriter = new OutputStreamWriter(fileOutputStream,"gbk");
int len;
char[] chars = new char[10];
while ((len=inputStreamReader.read(chars))!=-1){
outputStreamWriter.write(chars,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStreamReader!=null)
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (outputStreamWriter!=null)
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.2、标准输入、输出流(控制台)
System.in:标准的输入流,默认在控制台键盘输入,
System.out:标准的输出流,默认在控制台输出注意:
- 可以通过setOut方法来更改控制台的输出
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
bufferedReader = new BufferedReader(inputStreamReader);
while (true){
System.out.println("请输入:");
String data=bufferedReader.readLine();
if (data.equalsIgnoreCase("e")||data.equalsIgnoreCase("exit")){
System.out.println("程序结束!");
break;
}
public void test01(){
PrintStream printStream = null;
try {
File file = new File("D:\\java自学资料\\Java基础\\IO流\\ascii.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
printStream = new PrintStream(fileOutputStream, true);
if (printStream!=null){
System.setOut(printStream);//把标准的控制台输出更改为文件输出
}
//输出ASCII码
for (int i = 0; i <= 255; i++) {
System.out.print((char) i);
//每50个字符换一行
if (i%50==0){
System.out.println();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (printStream!=null)
printStream.close();
}
}
3.3、数据流
数据流:使用文件读写基本数据类型
dataOutputStream = new DataOutputStream(new FileOutputStream("data.txt"));
dataOutputStream.writeInt(1);
dataOutputStream.writeUTF("麻腾飞");
dataOutputStream.writeBoolean(true);
3.4、对象流
ObjectInputStream和ObjectOutputStream
- 作用:用来存储和读取基本数据类型和对象
什么叫序列化机制:
- 在Java中,将内存中的Java对象转换成与平台无关二进制流,从而运行该对象可以持久的保存在电脑的磁盘或硬盘,随时随地可以拿来使用(序列化)
- 也可以通过网络将转换的二进制流文件传输到相应的网络节点,其他程序可以将获取的二进制流转换为原来的Java对象(反序列化)
/*序列化*/
@Test
public void test04(){
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream("object.bat"));
objectOutputStream.writeObject(new String("我爱天安门!"));
objectOutputStream.writeObject(new Person("mtf",22));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutputStream!=null){
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*反序列化*/
@Test
public void test05(){
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream("object.bat"));
Object obj = objectInputStream.readObject();
Object object = objectInputStream.readObject();
String s = (String) obj;
Person person = (Person) object;
System.out.println(s);
System.out.println(person);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (objectInputStream!=null){
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}