数据流操作
文件流操作
// 创建文件对象, 使用文件路径关联系统文件
String filePath = "D:\\idea2022\\java-top-speed\\data\\word.txt";
File file = new File(filePath);
System.out.println(file); //文件路径
// 文件对象的操作
//判断当前文件对象是否为文件
System.out.println(file.isFile());
// TODO 判断当前文件对象是否为文件夹
System.out.println(file.isDirectory());
// TODO 判断文件对象是否存在关联
System.out.println(file.exists());
if ( file.exists() ) {
// TODO 文件对象存在的情况
System.out.println("文件对象存在");
if (file.isFile()) {
System.out.println("文件对象关联的是一个文件");
System.out.println(file.getName()); //文件名 word.txt
System.out.println(file.length()); //文件大小 5
System.out.println(file.lastModified());//文件最后修改时间
System.out.println(file.getAbsolutePath()); //绝对路径 D:\\idea2022\\java-top-speed\\data\\word.txt
} else if ( file.isDirectory() ) {
System.out.println("文件对象关联的是一个文件夹");
System.out.println(file.getName());
System.out.println(file.lastModified());
System.out.println(file.getAbsolutePath());
String[] list = file.list(); //文件夹中的文件
System.out.println("文件夹中的文件:");
for (String s : list) {
System.out.println(s);
}
System.out.println("文件夹中的文件对象:");
File[] files = file.listFiles();
for (File s : files) {
System.out.println(s);
}
}
} else {
// TODO 文件对象不存在的情况
System.out.println("文件对象不存在,没有关联成功");
// TODO 创建多级文件目录
//file.mkdirs();
// TODO 创建新文件
//file.createNewFile();
}
- 文件复制
// TODO 数据源文件对象
File srcFile = new File("D:\\idea2022\\java-top-speed\\data\\word.txt");
// TODO 数据目的地文件对象(自动生成)
File destFilt = new File("D:\\idea2022\\java-top-speed\\data\\word.txt.copy");
// TODO 文件输入流(管道对象)
FileInputStream in = null;
// TODO 文件输出流(管道对象)
FileOutputStream out = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFilt);
// TODO 打开阀门,流转数据(输入)
//data = in.read();
int data = -1;
// TODO 打开阀门,流转数据(输出)
//out.write(data);
// TODO 如果文件数据已经全部读取完毕后,那么再去读取数据,读取的结果就是-1,表示无效(结尾)
while ( (data = in.read()) != -1 ) {
out.write(data);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if ( in != null ) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if ( out != null ) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
上面复制文件的方式,一个个的读取文件内容写入另一个文件,效率比较低,性能不高。
将数据依次读取到缓冲区,再写入文件
// TODO Java IO - 文件复制 - 缓冲
// TODO 数据源文件对象
File srcFile = new File("D:\\idea2022\\java-top-speed\\data\\word.txt");
// TODO 数据目的地文件对象(自动生成)
File destFilt = new File("D:\\idea2022\\java-top-speed\\data\\word.txt.copy");
// TODO 文件输入流(管道对象)
FileInputStream in = null;
// TODO 文件输出流(管道对象)
FileOutputStream out = null;
// TODO 缓冲输入流(管道对象)
BufferedInputStream buffIn = null;
// TODO 缓冲输出流(管道对象)
BufferedOutputStream buffOut = null;
// TODO 缓冲区(水桶)
byte[] cache = new byte[1024];
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFilt);
buffIn = new BufferedInputStream(in);
buffOut = new BufferedOutputStream(out);
// TODO 打开阀门,流转数据(输入)
int data = -1;
while ( (data = buffIn.read(cache)) != -1 ) {
buffOut.write(cache, 0, data);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if ( buffIn != null ) {
try {
buffIn.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if ( buffOut != null ) {
try {
buffOut.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
一行一行的读取数据
// TODO Java IO - 文件复制 - 字符串
// TODO 数据源文件对象
File srcFile = new File("D:\\idea2022\\java-top-speed\\data\\word.txt");
// TODO 数据目的地文件对象(自动生成)
File destFilt = new File("D:\\idea2022\\java-top-speed\\data\\word.txt.copy");
// TODO 字符输入流(管道对象)
BufferedReader reader = null;
// TODO 字符输出流(管道对象)
PrintWriter writer = null;
try {
reader = new BufferedReader(new FileReader(srcFile));
writer = new PrintWriter(destFilt);
// TODO 打开阀门,流转数据(输入)
// 读取文件中的一行数据(字符串)
String line = null;
while ( (line = reader.readLine()) != null ) {
System.out.println(line);
writer.println(line);
}
// 刷写数据
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if ( reader != null ) {
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if ( writer != null ) {
writer.close();
}
}
序列化
public class Java07_IO_Object {
public static void main(String[] args){
// TODO Java IO - 文件复制 - 序列化 & 反序列化
// TODO 数据文件对象
File dataFile = new File("D:\\idea2022\\java-top-speed\\data\\obj.dat");
// TODO 对象输出流(管道对象)
ObjectOutputStream objectOut = null;
FileOutputStream out = null;
// TODO 对象输入流
ObjectInputStream objectIn = null;
FileInputStream in = null;
try {
out = new FileOutputStream(dataFile);
objectOut = new ObjectOutputStream(out);
// Java中只有增加了特殊的标记的类,才能再写文件中时进行序列化操作
// 这里的标记其实就是一个接口
User user = new User();
objectOut.writeObject(user);
objectOut.flush();
// TODO 从文件中读取数据转换成对象
in = new FileInputStream(dataFile);
objectIn = new ObjectInputStream(in);
Object o = objectIn.readObject();
System.out.println(o);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if ( objectOut != null ) {
try {
objectOut.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
class User implements Serializable{
}