写在前面
今天继续讲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
- 创建对象的方式:
- FileInputStream fis = new FileInputStream("路径");
复制代码 - 读取数据的方式:
- 一次读取一个字节:
- int i = 0;
- while ((i = fis.read()) != -1) {
- System.out.print((char) i);
- }
复制代码 - 一次读取一个字节数组:
- byte[] bytes = new byte[1024];
- int length = 0;
- while ((length = fis.read(bytes)) != -1) {
- String s = new String(bytes, 0, length);
- System.out.print(s);
- }
复制代码 - 一次读取一个字节数组的一部分。
- byte[] bytes = new byte[1024];
- int length = 0;
- while ((length = fis.read(bytes)) != -1) {
- String s = new String(bytes, 这里写从哪个索引开始截取, 这里写自己想要截取的长度);
- System.out.print(s);
- }
复制代码
- BufferedInputStream
- 创建对象的方式:
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream("路径"));
复制代码 - 读取数据的方式:
- 一次读取一个字节:
- int i = 0;
- while ((i = bis.read()) != -1) {
- System.out.print((char) i);
- }
复制代码 - 一次读取一个字节数组:
- byte[] bytes = new byte[1024];
- int length = 0;
- while ((length = bis.read(bytes)) != -1) {
- String s = new String(bytes, 0, length);
- System.out.print(s);
- }
复制代码 - 一次读取一个字节数组的一部分。
- byte[] bytes = new byte[1024];
- int length = 0;
- while ((length = bis.read(bytes)) != -1) {
- String s = new String(bytes, 这里写从哪个索引开始截取, 这里写自己想要截取的长度);
- System.out.print(s);
- }
复制代码
字节输出流
- 抽象父类: 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
- 创建对象的方式:
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("路径"));
复制代码 - 写数据的方式(注:这种方式写文件在写完之后需要flush一下,不然不会成功写入,后面说的以file对象进行传入参数的输入流基本上都要写完刷一下才有内容写入):
- 写一个字节:
- 写一个字节数组:
- byte[] bytes = {97, 98, 99};
- bos.write(bytes);
复制代码 - 写字节数组的一部分:
- byte[] bytes = {97, 98, 99};
- bos.write(bytes, 1, 2);
复制代码
字符流
字符流是基于字节流的,并加上了编码表的支持。
字符输入流
- 抽象父类: Reader
- 实现子类:
- InputStreamReader
- 创建对象的方式:
- InputStreamReader isr = new InputStreamReader(new FileInputStream("路径"));
复制代码 - 读取数据的方式:
- 一次读取一个字符:
- int i = 0;
- while ((i = isr.read()) != -1) {
- System.out.print((char) i);
- }
复制代码 - 一次读取一个字符数组:
- char[] chars = new char[1024];
- int length = 0;
- while ((length = isr.read(chars)) != -1) {
- String s = new String(chars, 0, length);
- System.out.print(s);
- }
复制代码 - 一次读取一个字符数组的一部分。
- char[] chars = new char[1024];
- int length = 0;
- while ((length = isr.read(chars)) != -1) {
- String s = new String(chars, 开始索引, 要读的具体长度);
- System.out.print(s);
- }
复制代码
- FileReader
- 创建对象的方式:
- FileReader fr = new FileReader("路径");
复制代码 - 读取数据的方式:
- 一次读取一个字符:
- int i = 0;
- while ((i = fr.read()) != -1) {
- System.out.print((char) i);
- }
复制代码 - 一次读取一个字符数组:
- char[] chars = new char[1024];
- int length = 0;
- while ((length = fr.read(chars)) != -1) {
- String s = new String(chars, 0, length);
- System.out.print(s);
- }
复制代码 - 一次读取一个字符数组的一部分。
- char[] chars = new char[1024];
- int length = 0;
- while ((length = fr.read(chars)) != -1) {
- String s = new String(chars, 开始索引, 要读的具体长度);
- System.out.print(s);
- }
复制代码
- BufferedReader
- 创建对象的方式:
- 方式1:
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("路径")));
复制代码 - 方式2(简化版):
- BufferedReader br = new BufferedReader(new FileReader("路径"));
复制代码
- 读取数据的方式:
- 一次读取一个字符:
- 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
- 创建对象的方式:
- OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("路径"));
复制代码 - 写数据的方式:
- 一次写一个字符。
- 一次写一个字符数组。
- char[] chars = {'a', 'b', 'c'};
- osw.write(chars);
复制代码 - 一次写一个字符数组的一部分。
- char[] chars = {'a', 'b', 'c'};
- osw.write(chars, 1, 2);
复制代码 - 一次写一个字符串。
- 一次写一个字符串的一部分。
- osw.write("example", 1, 4);
复制代码
- FileWriter
- 创建对象的方式:
- FileWriter fw = new FileWriter("路径");
复制代码 - 写数据的方式:
- 一次写一个字符。
- 一次写一个字符数组。
- char[] chars = {'a', 'b', 'c'};
- fw.write(chars);
复制代码 - 一次写一个字符数组的一部分。
- char[] chars = {'a', 'b', 'c'};
- fw.write(chars, 1, 2);
复制代码 - 一次写一个字符串。
- 一次写一个字符串的一部分。
- fw.write("example", 1, 4);
复制代码
- BufferedWriter
- 创建对象的方式:
- 方式1:
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("路径")));
复制代码 - 方式2(简化版):
- BufferedWriter bw = new BufferedWriter(new FileWriter("路径"));
复制代码
- 写数据的方式:
- 一次写一个字符:
- 一次写一个字符数组:
- 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("src/com/shujia/data/a.txt");
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("src/com/shujia/data/a.txt"));
- BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/com/shujia/data/b.txt"));
- int i = 0;
- while ((i = bufferedReader.read()) != -1) {
- bufferedWriter.write(i);
- bufferedWriter.flush();
- }
- bufferedWriter.close();
- bufferedReader.close();
复制代码 - 字符缓冲输入输出流:一次读写一个字符数组。
- BufferedReader bufferedReader = new BufferedReader(new FileReader("src/com/shujia/data/a.txt"));
- BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/com/shujia/data/b.txt"));
- 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();
复制代码 - 字符缓冲输入输出流:一次读写一行数据。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |