写在前面
今天继续讲JavaIO流的知识!
Java 文件与流操作
File 类
File
类用于表示文件和文件夹的抽象。
构造方法
public File(String pathname)
: 使用路径名创建 File
对象。
public File(String parent, String child)
: 使用父目录和子目录名称创建 File
对象。
public File(File parent, String child)
: 使用父 File
对象和子目录名称创建 File
对象。
成员方法
创建功能
public boolean createNewFile()
: 创建一个新文件。
public boolean mkdir()
: 创建单个目录。
public boolean mkdirs()
: 创建目录,包括必要但不存在的父目录。
删除功能
public boolean delete()
: 删除文件或目录。
重命名功能
public boolean renameTo(File dest)
: 重命名文件或目录。
判断功能
public boolean isDirectory()
: 判断是否为目录。
public boolean isFile()
: 判断是否为文件。
public boolean exists()
: 判断文件或目录是否存在。
public boolean canRead()
: 判断是否可读。
public boolean canWrite()
: 判断是否可写。
public boolean isHidden()
: 判断是否为隐藏文件。
基本获取功能
public String getAbsolutePath()
: 获取绝对路径。
public String getPath()
: 获取路径。
public String getName()
: 获取文件名。
public long length()
: 获取文件长度。
public long lastModified()
: 获取最后修改时间。
高级获取功能
public String[] list()
: 获取目录中的文件和目录名称。
public File[] listFiles()
: 获取目录中的 File
对象数组。
文件名称过滤器
public String[] list(FilenameFilter filter)
: 使用文件名过滤器获取文件和目录名称。
public File[] listFiles(FilenameFilter filter)
: 使用文件名过滤器获取 File
对象数组。
字节流
字节输入流
- 抽象父类:
InputStream
- 实现子类:
FileInputStream
BufferedInputStream
字节输出流
- 抽象父类:
OutputStream
- 实现子类:
-
FileOutputStream
| #创建文件输出流以写入由指定的 File对象表示的文件。 |
| FileOutputStream(File file) |
| |
| |
| #创建文件输出流以写入由指定的 File对象表示的文件。 |
| FileOutputStream(File file, boolean append) |
| |
| |
| #创建文件输出流以指定的名称写入文件。 |
| FileOutputStream(String name) |
| |
| |
| #创建文件输出流以指定的名称写入文件。 |
| FileOutputStream(String name, boolean append) |
- 写数据的方式:
- 写一个字节:
- 写一个字节数组:
| byte[] bytes = {97, 98, 99}; |
| fos.write(bytes); |
- 写字节数组的一部分:
| byte[] bytes = {97, 98, 99}; |
| fos.write(bytes, 1, 2); |
-
BufferedOutputStream
字符流
字符流是基于字节流的,并加上了编码表的支持。
字符输入流
- 抽象父类:
Reader
- 实现子类:
InputStreamReader
FileReader
BufferedReader
- 创建对象的方式:
- 读取数据的方式:
- 一次读取一个字符:
| int i = 0; |
| while ((i = br.read()) != -1) { |
| System.out.print((char) i); |
| } |
- 一次读取一个字符数组:
| char[] chars = new char[1024]; |
| int length = 0; |
| while ((length = br.read(chars)) != -1) { |
| String s = new String(chars, 0, length); |
| System.out.print(s); |
| } |
- 一次读取一个字符数组的一部分。
| char[] chars = new char[1024]; |
| int length = 0; |
| while ((length = br.read(chars)) != -1) { |
| String s = new String(chars, 开始索引, 具体长度); |
| System.out.print(s); |
| } |
- 一次读取一行数据(不包括换行符):
| String line = null; |
| while ((line = br.readLine()) != null) { |
| System.out.print(line); |
| } |
字符输出流
- 抽象父类:
Writer
- 实现子类:
-
OutputStreamWriter
-
FileWriter
-
BufferedWriter
- 创建对象的方式:
- 写数据的方式:
- 一次写一个字符:
- 一次写一个字符数组:
| char[] chars = {'a', 'b', 'c'}; |
| bw.write(chars); |
- 一次写一个字符数组的一部分:
| char[] chars = {'a', 'b', 'c'}; |
| bw.write(chars, 1, 2); |
- 一次写一个字符串:
- 一次写一个字符串的一部分:
| bw.write("example", 1, 4); |
- 特殊功能:自动生成换行符:
文件的读写复制
字节流
| FileInputStream inputPath = new FileInputStream("inputpath"); |
| FileOutputStream outputPath = new FileOutputStream("outpath"); |
| |
| int i = 0; |
| while ((i = inputPath.read()) != -1) { |
| outputPath.write((char) i); |
| } |
| inputPath.close(); |
| outputPath.close(); |
| BufferedInputStream inputpath = new BufferedInputStream(new FileInputStream("inputpath")); |
| BufferedOutputStream outputpath = new BufferedOutputStream(new FileOutputStream("outpath")); |
| |
| int i = 0; |
| |
| while ((i = inputpath.read()) != -1) { |
| outputpath.write((char) i); |
| outputpath.flush; |
| } |
| |
| inputpath.close(); |
| outputpath.close(); |
| |
| FileInputStream inputPath = new FileInputStream("inputpath"); |
| FileOutputStream outputPath = new FileOutputStream("outpath"); |
| |
| byte[] bytes = new byte[1024]; |
| int length = 0; |
| while ((length = inputPath.read(bytes)) != -1) { |
| outputPath.write(bytes, 0, length); |
| outputPath.flush(); |
| } |
| inputPath.close(); |
| outputPath.close(); |
- 带缓冲的字节输入输出流:一次读写一个字节数组(此方式的效率最高)。
| BufferedInputStream inputpath = new BufferedInputStream(new FileInputStream("inputpath")); |
| BufferedOutputStream outputpath = new BufferedOutputStream(new FileOutputStream("outpath")); |
| |
| byte[] bytes = new byte[1024]; |
| int length = 0; |
| while ((length = inputpath.read(bytes)) != -1) { |
| outputpath.write(bytes, 0, length); |
| outputpath.flush(); |
| } |
| |
| inputpath.close(); |
| outputpath.close(); |
字符流
| InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("inputpath")); |
| OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("outputpath")); |
| |
| int i = 0; |
| while ((i = inputStreamReader.read()) != -1) { |
| outputStreamWriter.write(i); |
| outputStreamWriter.flush(); |
| } |
| |
| inputStreamReader.close(); |
| outputStreamWriter.close(); |
| InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("inputpath")); |
| OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("outputpath")); |
| |
| char[] chars = new char[1024]; |
| int length = 0; |
| while ((length = inputStreamReader.read(chars)) != -1) { |
| outputStreamWriter.write(chars,0,length); |
| outputStreamWriter.flush(); |
| } |
| |
| inputStreamReader.close(); |
| outputStreamWriter.close(); |
| FileReader fileReader = new FileReader("inputpath"); |
| FileWriter fileWriter = new FileWriter("outputpath"); |
| |
| int i = 0; |
| while ((i = fileReader.read()) != -1) { |
| fileWriter.write(i); |
| fileWriter.flush(); |
| } |
| |
| fileWriter.close(); |
| fileReader.close(); |
| FileReader fileReader = new FileReader("inputpath"); |
| FileWriter fileWriter = new FileWriter("src/com/shujia/data/b.txt"); |
| |
| char[] chars = new char[1024]; |
| int length = 0; |
| while ((length = fileReader.read(chars)) != -1) { |
| fileWriter.write(chars,0,length); |
| fileWriter.flush(); |
| } |
| |
| fileWriter.close(); |
| fileReader.close(); |
| BufferedReader bufferedReader = new BufferedReader(new FileReader("inputpath")); |
| BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outpath")); |
| |
| int i = 0; |
| while ((i = bufferedReader.read()) != -1) { |
| bufferedWriter.write(i); |
| bufferedWriter.flush(); |
| } |
| |
| bufferedWriter.close(); |
| bufferedReader.close(); |
| BufferedReader bufferedReader = new BufferedReader(new FileReader("inputpath")); |
| BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outpath")); |
| |
| char[] chars = new char[1024]; |
| int length = 0; |
| while ((length = bufferedReader.read(chars)) != -1) { |
| bufferedWriter.write(chars,0,length); |
| bufferedWriter.flush(); |
| } |
| |
| bufferedWriter.close(); |
| bufferedReader.close(); |
| BufferedReader bufferedReader = new BufferedReader(new FileReader("inputpath")); |
| BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outpath")); |
| |
| String i = null; |
| while ((i = bufferedReader.readLine()) != null) { |
| bufferedWriter.write(i); |
| bufferedWriter.newLine(); |
| bufferedWriter.flush(); |
| } |
| |
| bufferedWriter.close(); |
| bufferedReader.close(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)