第六章 文件&IO流
6.1、File类
描述:该类是文件和目录路径名的抽象表示
构造方法:
| 方法 | 描述 |
|---|---|
| public File(String pathname) | 通过将给定的路径名字符串转换为抽象路径名来创建新的File实例 |
| public File(String parent, String child) | 从父路径名字符串和子路径名字符串创建新的File实例 |
| public File(File parent, String child) | 从父抽象路径名和子路径名字符串创建新的File实例 |
成员方法:
创建功能:
| 方法 | 描述 |
|---|---|
| public boolean createNewFile() | 当具有该名称的文件不存在时,创建一个由该抽象路径名命名的新空文件 |
| public boolean mkdir() | 创建由此抽象路径名命名的单级目录 |
| public boolean mkdirs() | 创建由此抽象路径名命名的多级目录 |
判断功能:
| 方法 | 描述 |
|---|---|
| public boolean isDirectory() | 测试此抽象路径名表示的File是否为目录 |
| public boolean isFile() | 测试此抽象路径名表示的File是否为文件 |
| public boolean exists() | 测试此抽象路径名表示的File是否存在 |
获取功能:
| 方法 | 描述 |
|---|---|
| public String getAbsolutePath() | 返回此抽象路径名的绝对路径名字符串 |
| public String getPath() | 将此抽象路径名转换为路径名字符串 |
| public String getName() | 返回由此抽象路径名表示的文件或目录的名称 |
| public String[] list() | 返回此抽象路径名表示的目录中的文件和目录的名称字符串数组 |
| public File[] listFiles() | 返回此抽象路径名表示的目录中的文件和目录的File对象数组 |
删除功能:
| 方法 | 描述 |
|---|---|
| public boolean delete() | 删除由此抽象路径名表示的文件或目录 |
6.2、IO流
概述:IO流就是用来处理设备间数据传输问题的。常见的应用:文件复制、文件上传、文件下载、文件的读取、文件的写出等等
分类:
按照数据流向来分:
输入流:读数据
输出流:写数据
按照数据类型来分:
字节流
字节输入流
字节输出流
字符流
字符输入流
字符输出流
注意:
- 如果操作的是纯文本文件,优先使用字符流
- 如果操作的是图片、视频、音频、应用等二进制文件,优先使用字节流
- 如果不确定文件类型,优先使用字节流,字节流是万能的流
6.2.1、字节流
体系:

6.2.1.1、字节流写数据的三种方式
| 方法 | 描述 |
|---|---|
| public void write(int b) | 写入一个字节 |
| public void write(byte[] b) | 写入一个字节数组 |
| public void write(byte[] b, int off, int len) | 写入一个字节数组的一部分 |
6.2.1.2、字节流读数据的三种方式
| 方法 | 描述 |
|---|---|
| public abstract int read() | 读入一个字节 |
| public int read(byte[] b) | 读入一个字节数组 |
| public int read(byte[] b, int off, int len) | 读入一个字节数组的一部分 |
6.2.1.3、字节流复制文件的四种方式
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
method4();
}
// 基本字节流一次读写一个字节
public static void method1() throws IOException {
FileInputStream fis = new FileInputStream("sFolder\\demo.txt");
FileOutputStream fos = new FileOutputStream("dFolder\\demo.txt");
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节数组
public static void method2() throws IOException {
FileInputStream fis = new FileInputStream("sFolder\\demo.txt");
FileOutputStream fos = new FileOutputStream("dFolder\\demo.txt");
byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 字节缓冲流一次读写一个字节
public static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("sFolder\\demo.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("dFolder\\demo.txt"));
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 字节缓冲流一次读写一个字节数组
public static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("sFolder\\demo.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("dFolder\\demo.txt"));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
6.2.2、字符流
体系:

6.2.2.1、字符流写数据的五种方式
| 方法 | 描述 |
|---|---|
| public void write(int c) | 写入一个字符 |
| public void write(char[] cbuf) | 写入一个字符数组 |
| public void write(char[] cbuf, int off, int len) | 写入一个字符数组的一部分 |
| public void write(String str) | 写入一个字符串 |
| public void write(String str, int off, int len) | 写入一个字符串的一部分 |
6.2.2.2、字符流读数据的四种方式
| 方法 | 描述 |
|---|---|
| public int read() | 读入一个字符 |
| public int read(char[] cbuf) | 读入一个字符数组 |
| public int read(char[] cbuf, int offset, int length) | 读入一个字符数组的一部分 |
| public String readLine() | 读入一个字符串 |
6.2.2.3、字符流复制文本的七种方式
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
method4();
method5();
method6();
method7();
}
// 基本字符流一次读写一个字符
public static void method1() throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("sFolder\\demo.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("dFolder\\demo.txt"));
int ch;
while ((ch = isr.read()) != -1) {
osw.write(ch);
}
osw.close();
isr.close();
}
// 基本字符流一次读写一个字符数组
public static void method2() throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("sFolder\\demo.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("dFolder\\demo.txt"));
char[] chs = new char[1024];
int len;
while ((len = isr.read(chs)) != -1) {
osw.write(chs, 0, len);
}
osw.close();
isr.close();
}
// 文件字符流一次读写一个字符
public static void method3() throws IOException {
FileReader fr = new FileReader("sFolder\\demo.txt");
FileWriter fw = new FileWriter("dFolder\\demo.txt");
int ch;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
fw.close();
fr.close();
}
// 文件字符流一次读写一个字符数组
public static void method4() throws IOException {
FileReader fr = new FileReader("sFolder\\demo.txt");
FileWriter fw = new FileWriter("dFolder\\demo.txt");
char[] chs = new char[1024];
int len;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
}
fw.close();
fr.close();
}
// 字符缓冲流一次读写一个字符
public static void method5() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("sFolder\\demo.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("dFolder\\demo.txt"));
int ch;
while ((ch = br.read()) != -1) {
bw.write(ch);
}
bw.close();
br.close();
}
// 字符缓冲流一次读写一个字符数组
public static void method6() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("sFolder\\demo.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("dFolder\\demo.txt"));
char[] chs = new char[1024];
int len;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
}
bw.close();
br.close();
}
// 字符缓冲流特有功能复制文本文件
public static void method7() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("sFolder\\demo.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("dFolder\\demo.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
}
}
6.3、文件夹复制
6.3.1、复制单级文件夹
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File srcFolder = new File("D:\\sFolder");
File destFolder = new File("D:\\dFolder");
copyFolder(srcFolder, destFolder);
}
/**
* 复制单级文件夹
*
* @param srcFolder 源文件夹
* @param destFolder 目的文件夹
* @throws IOException
*/
private static void copyFolder(File srcFolder, File destFolder) throws IOException {
// 判断路径是否存在
if (!destFolder.exists()) {
destFolder.mkdirs();
}
// 获取目的文件列表
File[] listFiles = srcFolder.listFiles();
// 遍历目的文件列表
for (File file : listFiles) {
copyFile(file, new File(destFolder, file.getName()));
}
}
/**
* 复制文件
*
* @param srcFile 源文件
* @param destFile 目的文件
* @throws IOException
*/
private static void copyFile(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
6.3.2、复制多级文件夹
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File srcFolder = new File("D:\\sFolder");
File destFolder = new File("D:\\dFolder");
copyFolder(srcFolder, destFolder);
}
/**
* 复制多级文件夹
*
* @param srcFolder 源文件夹
* @param destFolder 目的文件夹
* @throws IOException
*/
private static void copyFolder(File srcFolder, File destFolder) throws IOException {
// 判断路径是否存在
if (!destFolder.exists()) {
destFolder.mkdirs();
}
// 获取目的文件列表
File[] listFiles = srcFolder.listFiles();
// 遍历目的文件列表
for (File file : listFiles) {
if (file.isDirectory()) {
copyFolder(file, new File(destFolder, file.getName()));
} else {
copyFile(file, new File(destFolder, file.getName()));
}
}
}
/**
* 复制文件
*
* @param srcFile 源文件
* @param destFile 目的文件
* @throws IOException
*/
private static void copyFile(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
6.3.3、捕获异常新特性
6.3.3.1、JDK7以前做法
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
method();
}
private static void method() {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("fr.txt");
fw = new FileWriter("fw.txt");
char[] chs = new char[1024];
int len;
while ((len = fr.read()) != -1) {
fw.write(chs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
6.3.3.2、JDK7版本改进
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
method();
}
private static void method() {
try (FileReader fr = new FileReader("fr.txt");
FileWriter fw = new FileWriter("fw.txt");) {
char[] chs = new char[1024];
int len;
while ((len = fr.read()) != -1) {
fw.write(chs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.4、IO特殊流
6.4.1、标准输入流
import java.io.IOException;
import java.io.InputStream;
public class Main {
public static void main(String[] args) throws IOException {
InputStream is = System.in;
int by;
while ((by = is.read()) != -1) {
System.out.print((char) by);
}
is.close();
}
}
6.4.2、标准输出流
import java.io.IOException;
import java.io.PrintStream;
public class Main {
public static void main(String[] args) throws IOException {
PrintStream ps = System.out;
ps.println("Hello,World");
ps.write("Hello,World".getBytes());
ps.close();
}
}
6.4.3、字节打印流
import java.io.IOException;
import java.io.PrintStream;
public class Main {
public static void main(String[] args) throws IOException {
PrintStream ps = new PrintStream("ps.txt");
ps.println(97);
ps.write(97);
ps.close();
}
}
6.4.4、字符打印流
import java.io.IOException;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter("pw.txt");
pw.println("hello");
pw.write("Hello");
pw.close();
}
}
6.4.5、对象序列化流
注意:需要实现Serializable接口,同时需要给出serialVersionUID
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student implements Serializable {
private static final long serialVersionUID = 5923003911550370832L;
private String name;
private Integer age;
public Student() {
super();
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class Main {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));
Student s = new Student("曹晨磊", 30);
oos.writeObject(s);
oos.close();
}
}
6.4.6、对象反序列化流
注意:成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
class Student implements Serializable {
private static final long serialVersionUID = 5923003911550370832L;
private String name;
private Integer age;
public Student() {
super();
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));
Object obj = ois.readObject();
Student s = (Student) obj;
System.out.println(s);
ois.close();
}
}
6.5、Properties集合
import java.util.Properties;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Properties prop = new Properties();
// 存储元素
prop.put("student1", "林青霞");
prop.put("student2", "张曼玉");
// 普通遍历
Set<Object> keySet = prop.keySet();
for (Object key : keySet) {
Object value = prop.get(key);
System.out.println(key + "," + value);
}
// 特有方法
prop.setProperty("student3", "赵云");
prop.setProperty("student4", "张飞");
// 特有遍历
Set<String> names = prop.stringPropertyNames();
for (String key : names) {
String value = prop.getProperty(key);
System.out.println(key + "," + value);
}
}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws IOException {
// 把集合中的数据保存到文件
myStore();
// 把文件中的数据加载到集合
myLoad();
}
private static void myStore() throws IOException {
Properties prop = new Properties();
prop.setProperty("student1", "林青霞");
prop.setProperty("student2", "张曼玉");
FileWriter fw = new FileWriter("fw.txt");
prop.store(fw, null);
fw.close();
}
private static void myLoad() throws IOException {
Properties prop = new Properties();
FileReader fr = new FileReader("fw.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
}
第六章 文件&IO流的更多相关文章
- Java - 文件(IO流)
Java - 文件 (IO) 流的分类: > 文件流:FileInputStream | FileOutputStream | FileReader | FileWriter ...
- Java文件IO流的操作总结
Java中的IO操作涉及到的概念及相关类很多,很容易弄混,今天特来整理总结一下,并附上一份完整的文件操作的代码. 概念解析 读和写 流就是管道,向管道里面写数据用输出流:write 从管道里面读数据, ...
- 文件IO流总结
文件在网络上或不同设备之间是怎么传输的,在Java程序中又是怎么来实现文件的传输,带着这两个问题,来了解一下Java中的IO流相关类及操作. 一.什么是流及流的用途 流是一组有顺序,有起点和终点的字节 ...
- Java File类与文件IO流总结
1.File类 File类被定义为“文件和目录路径名的抽象表示形式”,这是因为File类既可以表示“文件”也可以表示“目录”,他们都通过对应的路径来描述.通过构造函数创建一个File类对象,则该对象就 ...
- Python学习笔记 -- 第六章 文件操作
I/O编程 在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这 ...
- 关于java读取文件IO流学习总结(一)
IO流的分类: 1.根据流的数据对象来分: 高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Outpu ...
- 3,Java中的文件IO流
1,File类 ··· 概念:File对象可以表示一个文件或目录.可以对其进行增删改查. ··· 常用方法: File f = new File("."); 判断是 ...
- 文件IO流完成文件的复制(复杂版本主要用来演示各种流的用途,不是最佳复制方案哦)
package io; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import j ...
- 文件IO流
//字节流读写含有中文的文本文件会出现问题,我在实践中居然没有检验出该问题,新人小菜,希望大家能指出: import java.io.FileInputStream; import java.io.F ...
随机推荐
- RabbitMQ(dotnet基本使用
前言 RabbitMQ环境环境搭建及基本配置,在此不讨论.网上一大堆. NET环境下,Rabbit库可以在官网或NUGET上查找得到. 生产者 static void Main(string[] ar ...
- Jenkins 主题:jenkins-theme-v2
说明 本次样式是基于 Jenkins ver. 2.235.1 写的,所有对于之前的版本可能样式不兼容,好像变化挺大的.个人测试了在用的 Jenkins ver. 2.190.1,完全不行,所有建议想 ...
- linux就该这么学 第一天学习笔记
题外话 在每天的网上冲浪中,一次无意间的点击,发现了linux就该这么学的网站,然后就看了一晚上,当时还是学生的我特别想要参加培训,可是碍于眼前的经济状况,只得将这个想法深深的藏在了心里,并加了一下网 ...
- 【线型DP】洛谷P2066 机器分配
[线型DP]洛谷P2066 机器分配 标签(空格分隔): 线型DP [题目] 题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配 ...
- day78 作业
目录 1 在作业.html的代码基础上,完成商品数量的加减,注意商品数量如果低于0个,则自动删除当前商品 2 在作业.html的代码基础仧,完成购物车总价格的计算 3 使用ajax获取北京天气,并把昨 ...
- vue重置data里的值
this.$options.data() 这个可以获取原始的data值,this.$data 获取当前状态下的data,拷贝重新赋值一下就行了. Object.assign(this.$data, t ...
- 数据可视化之PowerQuery篇(四)二维表转一维表,看这篇文章就够了
https://zhuanlan.zhihu.com/p/69187094 数据分析的源数据应该是规范的,而规范的其中一个标准就是数据源应该是一维表,它会让之后的数据分析工作变得简单高效. 在之前的文 ...
- python 面向对象专题(一):面向对象初识、面向对象结构、类、self、实例化对象
https://www.cnblogs.com/liubing8/p/11301344.html 目录 Python面向对象01 /面向对象初识.面向对象结构.类.self.实例化对象 1. 面向对象 ...
- 有效提高java编程安全性的12条黄金法则
安全性是软件开发中最复杂,最广泛和最重要的考量之一.Java是具有许多内置安全性功能的开发平台,java在长期的发展过程中,已经经过了很多高强度的安全测试,并经常更新安全漏洞.并且Java生态系统还包 ...
- 手把手从零开始---封装一个vue视频播放器组件
现在,在网页上播放视频已经越来越流行,但是网上的资料鱼龙混杂,很难找到自己想要的,今天小编就自己的亲身开发体验,手把手从零开始---封装一个vue视频播放器组件. 作为一个老道的前端搬砖师,怎么可能会 ...