目录
File类
专门创建文件对象,因为java是面向对象的,所以我们想操作一个文件的时候,就需要将文件转换成对象
文件和文件夹都可以变成File对象
路径分隔符
- windows和DOS系统默认使用"\"来表示
- UNIX和URL使用"/"来表示
Java程序支持跨平台运行,因此路径分隔符要慎用。 为了解决这个隐患,File类提供了一个常量: public static final String separator:根据操作系统,动态的提供分隔符。
Filefile1 = new File("d:\\atguigu\\info.txt");
File file2 = new File("d:" + File.separator + "atguigu" +File.separator + "info.txt");
File file3 = new File("d:/atguigu");
File类常用方法
import java.io.File;
import java.io.IOException;
public class TestFile {
public static void main(String[] args) throws IOException {
File file = new File("D:\\测试文件.txt");
System.out.println( file.exists() );//exists():判断文件是否存在
System.out.println( file.getName() );//getName(): 获取文件名称
System.out.println( file.length() );//length(): 文件长度,字节为单位
System.out.println( file.getPath() );//getPath():获取完整路径
System.out.println( file.getParent() );//getParent():获取上级路径
File parentFile = file.getParentFile();//将父级文件夹 变成File对象
System.out.println( parentFile.isFile() );//isFile(): 判断是不是文件
System.out.println( parentFile.isDirectory() );//isDirectory(): 判断是不是文件夹
//createNewFile():创建一个新文件,如果已经存在文件 则不会覆盖
File file2 = new File("D:\\测试文件2.txt");
System.out.println("文件创建成功?"+file2.createNewFile());
//file.renameTo():改名
File file3 = new File("D:\\测试文件-新.txt");
System.out.println("文件改名成功?"+file.renameTo(file3));
//delete()删除文件
System.out.println("删除文件成功?"+file2.delete());
}
}
IO流分类
- 按数据流的方向:输入流、输出流
输入与输出是相对于应用程序而言的,比如文件读写,读取文件是输入流,写文件是输出流,这点很容易搞反。
记住:以程序为第一视角
- 按处理数据单位:字节流、字符流 字节流和字符流的用法几乎完成全一样,区别在于字节流和字符流所操作的数据单元不同 字节流操作的单元是数据单元是8位的字节,字符流操作的是数据单元为16位的字符。
- 按功能:节点流、处理流
直接操作数据读写的流类,比如FileInputStream
对一个已存在的流的链接和封装,通过对数据进行处理为程序提供功能强大、灵活的读写功能,例如BufferedInputStream(缓冲字节流)
处理流和节点流应用了Java的装饰者设计模式。
下图就很形象地描绘了节点流和处理流,处理流是对节点流的封装,最终的数据处理还是由节点流完成的。
在诸多处理流中,有一个非常重要,那就是缓冲流。
我们知道,程序与磁盘的交互相对于内存运算是很慢的,容易成为程序的性能瓶颈。减少程序与磁盘的交互,是提升程序效率一种有效手段。缓冲流,就应用这种思路:普通流每次读写一个字节,而缓冲流在内存中设置一个缓存区,缓冲区先存储足够的待操作数据后,再与内存或磁盘进行交互。这样,在总数据量不变的情况下,通过提高每次交互的数据量,减少了交互次数。
字节流、字符流、字符缓冲流
在Java中,有两种主要的流类型:字节流(Byte Stream)和字符流(Character Stream)。
字节流以字节为单位进行读写,用于处理二进制数据或文本文件的原始字节。字节流通常用于处理图像、音频、视频等二进制数据。在字节流中,最基本的输入流是InputStream,最基本的输出流是OutputStream。
字符流以字符为单位进行读写,用于处理文本数据。字符流可以将字节转换为字符,使得处理文本文件更加方便。在字符流中,最基本的输入流是Reader,最基本的输出流是Writer。
为了提高性能和效率,Java还提供了字符缓冲流(Buffered Stream)。字符缓冲流是字符流的一种装饰器(Decorator),它通过在内存中创建一个缓冲区,减少与底层设备的直接交互次数,从而提高读写的效率。字符缓冲流包装了底层的字符流,提供了更高级别的读写方法,如readLine()和write()。最基本的字符缓冲输入流是BufferedReader,最基本的字符缓冲输出流是BufferedWriter。
字节流-读写代码
字节流的方式效率较低,不建议使用
package com.IO流;
import java.io.*;
/**
* 字节流:字节流的方式效率较低,不建议使用
*
* @author 康有为
* @date 2023/07/17
*/
public class IOTest1 {
public static void main(String[] args) throws IOException {
File file = new File("D:\\测试文件.txt");
write(file);
System.out.println(read(file));
}
//写操作,输出字节流 FileOutputStream ,这里的写操作是添加字符串,不是将文件的内容全部修改成字符串
public static void write(File file) throws IOException {
//第二个参数 true代表在源文件上添加字符串, false代表将源文件的内容直接修改成字符串
OutputStream os = new FileOutputStream(file, true);
// 要写入的字符串
String string = "松下问童子,言师采药去。只在此山中,云深不知处。";
// 写入文件
os.write(string.getBytes());
// 关闭流
os.close();
}
//读操作,输入字符流 FileInputStream
public static String read(File file) throws IOException {
InputStream in = new FileInputStream(file);
// 一次性取多少个字节
byte[] bytes = new byte[1024];
// 用来接收读取的字节数组
StringBuilder sb = new StringBuilder();
// 读取到的字节数组长度,为-1时表示没有数据
int length = 0;
// 循环取数据
while ((length = in.read(bytes)) != -1) {
// 将读取的内容转换成字符串
sb.append(new String(bytes, 0, length));
}
// 关闭流
in.close();
return sb.toString();
}
}
字符流-读写代码
字符流适用于文本文件的读写,OutputStreamWriter类其实也是借助FileOutputStream类实现的,故其构造方法是FileOutputStream的对象
package com.IO流;
import java.io.*;
/**
* 字符流:适用于文本文件的读写
*
* @author 康有为
* @date 2023/07/17
*///字符流
public class IOTest2 {
public static void main(String[] args) throws IOException {
File file = new File("D:\\测试文件.txt");
//OutputStreamWriter 和 InputStreamReader
write(file);
System.out.println(read(file));
}
//写操作,输入字符流OutputStreamWriter,
public static void write(File file) throws IOException {
// OutputStreamWriter可以显示指定字符集,否则使用默认字符集
//第二个参数 true代表在源文件上添加字符串, false代表将源文件的内容直接修改成字符串
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8");
// 要写入的字符串
String string = "时间不在于你拥有多少,而在于你怎样使用。";
osw.write(string);
osw.close();
}
//读操作,输出字符流InputStreamReader
public static String read(File file) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
// 字符数组:一次读取多少个字符
char[] chars = new char[1024];
// 每次读取的字符数组先append到StringBuilder中
StringBuilder sb = new StringBuilder();
// 读取到的字符数组长度,为-1时表示没有数据
int length;
// 循环取数据
while ((length = isr.read(chars)) != -1) {
// 将读取的内容转换成字符串
sb.append(chars, 0, length);
}
// 关闭流
isr.close();
return sb.toString();
}
}
也可以用便捷式的方法
//便捷式写FileWriter
public static void write(File file) throws IOException {
//第二个参数 true代表在源文件上添加字符串, false代表将源文件的内容直接修改成字符串
FileWriter fw = new FileWriter(file, true);
// 要写入的字符串
String string = "666666666666666666666";
fw.write(string);
fw.close();
}
//便捷式读FileReader
public static String read(File file) throws IOException {
FileReader fr = new FileReader(file);
// 一次性取多少个字节
char[] chars = new char[1024];
// 用来接收读取的字节数组
StringBuilder sb = new StringBuilder();
// 读取到的字节数组长度,为-1时表示没有数据
int length;
// 循环取数据
while ((length = fr.read(chars)) != -1) {
// 将读取的内容转换成字符串
sb.append(chars, 0, length);
}
// 关闭流
fr.close();
return sb.toString();
}
字符缓冲流-读写代码
高性能和效率,最推荐的一个
package com.IO流;
import java.io.*;
/**
* BufferedReader、BufferedWriter(字符缓冲流)
* 高性能和效率
*
* @author 康有为
* @date 2023/07/17
*/
public class IOTest3 {
public static void main(String[] args) throws IOException {
File file = new File("D:\\测试文件.txt");
write(file);
System.out.println(read(file));
}
//写操作,输出缓冲流BufferedWriter
public static void write(File file) throws IOException {
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
//用 FileWriter可以大幅度简化代码
BufferedWriter bw = new BufferedWriter(new FileWriter(file, false));//第二个参数 true代表在源文件上添加字符串, false代表将源文件的内容直接修改成字符串
// 要写入的字符串
String string = "你好你好你好你好你好你好好呢好呢好呢好呢好呢你好你好你好你好你好你好";
bw.write(string);
bw.close();
}
//读操作,输入缓冲流BufferedReader
public static String read(File file) throws IOException {
//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
//用 FileReader可以大幅度简化代码
BufferedReader br = new BufferedReader(new FileReader(file));
// 用来接收读取的字节数组
StringBuilder sb = new StringBuilder();
// 按行读数据
String line;
// 循环取数据
while ((line = br.readLine()) != null) {
// 将读取的内容转换成字符串
sb.append(line);
}
// 关闭流
br.close();
return sb.toString();
}
}