Java文件操作
一、File类
1、基本定义:
File类可表示一个文件或目录,创建File对象可以利用该对象对文件或目录进行属性的修改(日期、时间、名称等)
2、文件构造器
package com.zhujiantao;
//引入java内置包,专门用于文件读写的一个操作的类
import java.io.File;
//创建文件对象
public class test02 {
File file = new File("D\\test.txt");
}
3、文件类的方法
列举其中几个较为重要的:
3.1 创建、判断文件,创建、判断目录,删除,重命名
//判断文件
if(!file.exists()){
//创建文件
file.createNewFile();
}
//判断目录
File file2 = new File("D:\\java15\\1a\\2b\\3c");
if(!file2.isDirectory()){
//创建目录
//mkdir:单级目录
//mkdirs:多级目录
file2.mkdirs();
}
//删除文件
file.delete();
//重命名
file.renameTo(new File("D:\\hello2.txt"));
3.2文件的获取
package com.zhujiantao;
import java.io.File;
public class test02<fileArr> {
public static void main(String[] args) {
File file = new File("D\\java课程");
File[] fileArr = file.listFiles();
//遍历所有文件名
for(File fileModel:fileArr){
//获取路径
System.out.println(fileModel.getAbsolutePath());
}
}
}
3.3文件的写入
public class WriteToFile {
public static void main(String[] args) {
//异常处理
try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("这是个例子");
//写入操作要关闭文件
myWriter.close();
System.out.println("成功写入文件");
} catch (IOException e) {
System.out.println("出错了");
e.printStackTrace();
}
二、递归
1、概述
函数自身直接或间接调用函数的本身。
会出现死循环问题
2、适用
一个功能在被重复使用,并每次使用时,参与运算的结果和上一次调用有关,这时就可以使用递归来解决这个问题。
3、经典例子
计算5!
public class Test03 {
// 计算5的阶乘
public static void main(String[] args) {
// 5*4*3*2*1
// 5*4! 4*3! 3*2! 2*1!
Test03 test03 = new Test03();
//调用方法
int result = test03.count(4);
System.out.println(result);
}
//计算阶乘的方法
public int count(int num) {
if (num == 1 || num == 0) {
return 1;
} else {
return num * count(num - 1);
}
}
}
4、练习
利用递归思想,输出文件夹中所有文件名
public class test02<fileArr> {
public static void main(String[] args) {
File file = new File("D\\java课程");
File[] fileArr = file.listFiles();
for(File fileModel:fileArr){
System.out.println(fileModel.getAbsolutePath());
}
}
}
三、IO流
1、流的介绍
IO(input/output): 输入和输出,指的是某个设备或环境进行数据的输入或输出,例如: 键盘的输入,显示器是输出设备,用来输出图像。
java将输入和输出的问题抽象成流对象解决。
在java.io包中操作文件内容的主要有两大类:字节流、字符流,两类都分为输入和输出操作。
字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,而字节流处理单元为1个字节,操作字节和字节数组。如果是音频文件、图片、歌曲,就用字节流好点,如果是关系到中文(文本)的,用字符流好点。 字节流可用于任何类型的对象,包括二进制对象,而字符流只能处理字符或者字符串; 字节流提供了处理任何类型的IO操作的功能,但它不能直接处理Unicode字符,而字符流就可以。
io流主要分为:
从数据流分:输入流、输出流
从处理数据分:字节流(处理一切)、字符流(处理纯文本)
从功能分:节点流、处理流
2、输入流、输出流
输入与输出是相对而言,是数据源与程序之间的交互数据的具体体现。
3、字节流、字符流
字节流与字符流用法相同,区别只是在处理的数据单元上有所不同。
3.1字符流
有两个抽象基类:Reader、Writer
write(): 往缓冲区写入数据 flush(): 将缓冲区(8192个字节)的数据全部写入到文件中 close():需要手动关闭的资源,一般都是虚拟机之外的资源,比如说端口(网络编程),文件、显存。这些是虚拟机不能通过垃圾回收来释放的。所以我们要手动的调用close方法来释放。
字符输入流Reader主要方法:
read():读取单个字符。 read(char[] cbuf) :将字符读入数组。 read(char[] cbuf, int off, int len) : 将字符读入数组的某一部分。 read(CharBuffer target) :试图将字符读入指定的字符缓冲区。 flush() :刷新该流的缓冲。 close() :关闭此流,但要先刷新它。 字符输出流Writer主要方法:
write(char[] cbuf) :写入字符数组。 write(char[] cbuf, int off, int len) :写入字符数组的某一部分。 write(int c) :写入单个字符。 write(String str) :写入字符串。 write(String str, int off, int len) :写入字符串的某一部分。 flush() :刷新该流的缓冲。 close() :关闭此流,但要先刷新它。
3.1.1创建文件输出流
//案例: 使用字符流向文件输入 helloworld
//步骤:
//1. 创建文件
//2. 创建输出流对象
//3. 把流指向指定的文件
//4. 释放资源
public class Test04 {
public static void main(String[] args) {
Writer writer = null;
try {
writer = new FileWriter(new File("D:/java15/123.txt"));
writer.write("hello world");
// writer.flush(); 将缓冲区的数据输出到文件中
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
3.1.2 Reader读取
//案例: 将txt文件内容打印到控制台
//步骤:
//1. 创建输入流对象FileReader
//2. 读取数据
//3. 关闭输入流
public class Test05 {
public static void main(String[] args) {
Reader reader = null;
try {
reader = new FileReader(new File("D:/java15/123.txt"));
//单个去读取
// int i1 = reader.read();
// System.out.println(i1);
// int i2 = reader.read();
// System.out.println(i2);
// int i3 = reader.read();
// System.out.println(i3);
//定义数组长度,读取流
char[] cr = new char[10];
int i = -1;
//循环读取
while((i=reader.read(cr))!=-1){
System.out.println(new String(cr,0,i));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
3.2高效缓冲区输入、输出流
BufferedReader
从字符输入流中读取文本,缓冲各个字符,从而实现字符,数组和行的高效读取
行读取: readLine();
BufferedWriter
写换行符: newLine();
public class Test06 {
public static void main(String[] args) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(new File("D:\\java15\\123.txt")));
String str = null;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.3字节流
与字符流相类似
字节输入流InputStream主要方法:
read() :从此输入流中读取一个数据字节。 read(byte[] b) :从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。 read(byte[] b, int off, int len) :从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。 close():关闭此输入流并释放与该流关联的所有系统资源。 字节输出流OutputStream主要方法:
write(byte[] b) :将 b.length 个字节从指定 byte 数组写入此文件输出流中。 write(byte[] b, int off, int len) :将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 write(int b) :将指定字节写入此文件输出流。 close() :关闭此输入流并释放与该流关联的所有系统资源。
字节输入流
InputStream 常用的子类: FileInputStream
字节输出流
OutputStream 常用子类: FileOutputStream
4、节点流、处理流
5、序列化流(对象流)
ObjectOutputStream
主要方法: writeObject
ObjectInputStream
主要方法: readObject
把对象以流的形式存储在硬盘或者数据库中的过程就是写序列化流
package com.mashang;
import java.io.*;
public class Test10 {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.age = 18;
teacher.name = "zhangsan";
ObjectOutputStream objectOutputStream = null;
ObjectInputStream objectInputStream = null;
try {
// objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\java15\\teahcer.txt"));
// objectOutputStream.writeObject(teacher);
objectInputStream = new ObjectInputStream(new FileInputStream("D:\\java15\\teahcer.txt"));
Teacher teacher1 = (Teacher) objectInputStream.readObject();
System.out.println(teacher1.name);
System.out.println(teacher1.age);
System.out.println(teacher1.heght);
System.out.println(teacher1.weight);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}