① File 类的一个对象,代表一个文件或文件目录(俗称:文件夹)
② File 类声明的砸 Java.io 包下
③ File 类中涉及关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法,并涉及到写入的读取文件内容的操作。如果需要读取或写入的 ”终点“。
常用的构造器
File(String filePath)
File(String parenPath, String childPath)
File(File parenFile, String childPath)
路径的分类
相对路径:相对某个路径,指明的路径
绝对路径:包含盘符在内的文件或文件目录的路径
路径分隔符
windows 和 DOC 系统的默认使用“\”来表示
UNIX 和 URL 使用“/” 来表示
图示:
说明: 红框对应的是 IO 流中的 4 个抽象基类。
① 输入过程
说明: 程序中出现的异常需要使用 try-catch-finally 处理。
② 输入过程
说明:程序中出现的异常需要使用 try-catch-finally 处理。
① FileReader 的使用
@Test
public void test() {
FileReader fr = null;
try {
// 1. File的实例化
File file = new File("hello.txt");
// 2.FileReader流的实例化
fr = new FileReader(file);
// 3.读入的操作
// read(char[] ch):返回每次读入ch数组中的字符的个数。如果达到文件末尾
char[] ch = new char[1024];
int len;
while((len = fr.read(ch)) != -1){
String str = new String(ch, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fr != null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
② FileWrite 的使用
@Test
public void test2() {
FileWriter fw = null;
try {
// 1.提供File类的对象,指明写出到文件
File file = new File("hello.txt");
// 2.提供FileWriter的对象,用于数据写出
fw = new FileWriter(file, false);
// 3.写出的操作
fw.write("I have a dream!\n");
fw.write("you need to have a dream!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.流资源关闭
try {
if (fw != null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
③ 文本文件复制
@Test
public void test3(){
FileReader fr = null;
FileWriter fw = null;
try {
// 1.创建File类的对象,指明读入和写入文件
File srcfile = new File("hello.txt");
File dsrcfile1 = new File("hello1.txt");
// 2.创建输入流和输出流
fr = new FileReader(srcfile);
fw = new FileWriter(dsrcfile1);
// 数据的读写和写入操作
char[] ch = new char[1024];
// 记录每次读入到ch数组中的字符串的个数
int len;
while((len = fr.read(ch)) != -1){
// 每次写出len个字符
fw.write(ch,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. 关闭流资源
try {
if(fw != null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fr != null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test1(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 1. 造文件
File srcfile = new File("图片.jpg");
File desrfile = new File("图片2.jpg");
// 2.造流
fis = new FileInputStream(srcfile);
fos = new FileOutputStream(desrfile);
// 3.复制的过程
byte[] bt = new byte[1024];
int len;
while((len = fis.read(bt)) != -1){
fos.write(bt,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
【注意】
作用:提高流的读取、写入的速度
提高读写速度的原因:内部提供了一个缓冲区。默认情况下是 8kb
① 使用 BufferadInputStream 和 BufferadOutputStream :处理非文本
@Test
public void test1(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1.造文件
File srcfile = new File("图片.jpg");
File dsrcfile = new File("图片3.jpg");
// 2.造流
// 2.1节点流
FileInputStream fis = new FileInputStream(srcfile);
FileOutputStream fos = new FileOutputStream(dsrcfile);
// 2.2 节点流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 3.复制的细节:读取、写入
byte[] bt = new byte[1024];
int len;
while((len = bis.read(bt)) != -1){
bos.write(bt, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. 关闭资源
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
② 使用 BufferedReader 和 BufferedWriter:处理文本文件
@Test
public void test2(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
File srcfile = new File("hello.txt");
File dsrcfile = new File("hello3.txt");
// 造流
br = new BufferedReader(new FileReader(srcfile));
bw = new BufferedWriter(new FileWriter(dsrcfile));
// 3. 读取
// 第一种方式
String data;
while((data = br.readLine()) != null){
// data中不包含换行符
bw.write(data);
// 提供换行符
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. 关闭流资源
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//读取的第二种方式
char[] cbuf = new char[1024];
int len;
while((len = br.read(cbuf)) != -1){
bw.write(cbuf,0,len);
bw.flush();
}
提供字节流与字符流之间的转换
@Test
public void test1() {
InputStreamReader isr = null;
try {
FileInputStream fis = new FileInputStream("hello.txt");
isr = new InputStreamReader(fis, "UTF-8");
char[] ch = new char[1024];
int len;
while((len = isr.read(ch)) != -1){
String str = new String(ch, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr != null){
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test2() {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
// 1.造文件、造流
File file1 = new File("hello.txt");
File file2 = new File("hello_gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
isr = new InputStreamReader(fis, "UTF-8");
osw = new OutputStreamWriter(fos, "gbk");
// 2.读写过程
char[] ch = new char[1024];
int len;
while((len = isr.read(ch)) != -1){
osw.write(ch, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(isr != null){
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(osw != null){
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ObjectInputStream: 内存中的对象 –> 存储中的文件、通过网路传输过去:序列化过程
ObjectOutputStream: 存储中文件、通过网络接收过来 –> 内存中的对象:反序列化的过程
创建 Person 类(实现 Serializable 接口,添加序列号):
public class Person implements Serializable {
/**
* 序列号
*/
public static final long serialVersionUID = 475463534532L;
private String name;
private int id;
public Person() {
}
public Person(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
@Test
public void test1() {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("你好"));
oos.flush();//刷新操作
oos.writeObject(new Person("小明",23));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null){
oos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test2(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String) obj;
Person p = (Person) ois.readObject();
System.out.println(str);
System.out.println(p);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null){
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
修改默认的输入和输出行为:
System 类的 setIn(InputStream is) / setOut(prinStream ps) 方式重新指定输入和输出流。
作用:用于读取或写出基本数据类型的变量或字符串
实例 1:将内存中的字符串、基本数据类型的变量写到文件中。
@Test
public void test1(){
// DataInputStream dis = new DataInputStream(new FileInputStream("hello.txt"));
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF("OY");
// 刷新操作,将内存中的数据写入文件
dos.flush();
dos.writeInt(19);
dos.flush();
dos.writeBoolean(true);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dos != null){
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
实例 2:将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。
@Test
public void test2() {
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("data.txt"));
String name = dis.readUTF();
int age = dis.readInt();
Boolean isMale = dis.readBoolean();
System.out.println("name=" + name);
System.out.println("age=" + age);
System.out.println("isMale=" + isMale);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null){
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test1(){
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
raf1 = new RandomAccessFile(new File("图片.jpg"), "r");
raf2 = new RandomAccessFile(new File("图片4.jpg"), "rw");
byte[] bt = new byte[1024];
int len;
while((len = raf1.read(bt)) != -1){
raf2.write(bt, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (raf1 != null){
raf1.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (raf2 != null){
raf2.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}