File文件类
构造方法
构造方法 | 作用 |
---|---|
new File(String path) | 生成文件路径 |
new File(String fuPath,String ziPath) | 连接文件路径 |
new File(File fuPath,String ziPath); | 连接文件路径 |
常用方法
方法名 | 作用 | 返回值 |
---|---|---|
createNewFile() | 创建文件 | boolean |
File file = new File(file, "名称");
file.createNewFile();
方法名 | 作用 | 返回值 |
---|---|---|
mkdir() | 创建文件夹 | boolean |
mkdirs() | 创建多级文件夹 | boolean |
File file = new File(file, "名称");
file.mkdir();
File file = new File(file, "名称\\名称\\...");
file.mkdirs();
方法名 | 作用 | 返回值 |
---|---|---|
delete() | 删除文件(夹) | boolean |
File file = new File(file, "名称");
file.delete();
方法名 | 作用 | 返回值 |
---|---|---|
getName() | 获取文件(夹)名称 | String |
getPath() | 获取完整路径 | String |
toString() | 获取完整路径 | String |
File file = new File(file, "路径//名称");
file.getName();//名称
file.getPath();//路径//名称
file.toString();//路径//名称
方法名 | 作用 | 返回值 |
---|---|---|
length() | 获取文件字节数 | long |
File file = new File(file, "名称.类型");
file.length();
方法名 | 作用 | 返回值 |
---|---|---|
list() | 获取路径下所有文件(夹)名称 | String[] |
listFiles() | 获取路径下所有文件(夹)对象 | File[] |
File file = new File("路径");
//文件名数组
String[] filename = file.list();
//文件路径数组
File[] files = file.listFiles();
方法名 | 作用 | 返回值 |
---|---|---|
isFile() | 路径是否指向文件对象 | boolean |
isDirectory() | 路径是否指向文件夹对象 | boolean |
exits() | 路径是否存在 | boolean |
File file = new File("路径\\文件夹");
file.isFile();//false
file.isDirectory();//true
file.exists();//true
传输流
- | 读入流 | 写出流 |
---|---|---|
字节流 | InputStream | OutputStream |
字符流 | Reader | Writer |
方法 | read() | write() |
字节流
字节读入流
new FileInputStream(File 读入地址) -- 文件必须存在,否则异常
常用方法
方法名 | 作用 | 返回值 |
---|---|---|
read() | 读一个字节 返回其ASCII值 | int |
read(byte[]) | 数组长度决定每次读入字节数 | int |
//创建读入文件的对象
File file = new File("路径\\文件");
//创建字节读入流对象
FileInputStream fis = new FileInputStream(file);
//可选择缓冲区(字节数组)读入
//定义合适长度的数组
byte[] bs = new byte[4];
//记录每次实际读入的字节数
int l = 0;
while((l = fis.read(bs))!=-1){
//String类构造方法将读入字节连接输出
syso(new String(bs,0,l));
}
//一次性读完
byte[] bs = new byte[(int)file.length];
//记录每次实际读入的字节数
fis.read(bs)
//String类构造方法将读入字节连接输出
syso(new String(bs);
}
//以上代码针对单字节文本 没有问题 但是包含有汉字等多字节字符 可能会出现乱码
字节写出流
new FileOutputStream(File 写出地址) -- 文件可不存在,自动创建
new FileOutputStream(File,boolean)
boolean:false(默认值)
false -- 覆盖原数据
true -- 原数据后追加
常用方法
方法 | 作用 |
---|---|
write(int) | 将字符写出:字符串(部分)、数组 |
flush() | 刷新缓冲区 |
close() | 关闭对象 |
//创建写出目标地址,最终文件可不存在 自动创建
File file = new File("路径\\文件");
//创建字节写出流对象
//默认第二个参数为false,将覆盖数据
FileOutputStream fos = new FileOutputStream(file);
fos.write(97);//a
fos.write('a');//a
fos.write("中国".getBytes());//中国
fos.write("中国你好".getBytes(),4,4);//你好
fos.flush();//刷新缓冲区 保证全部写出
fos.close();//刷新缓冲区 同时关闭写出流对象
字符流
字符读入流
new FileReader(File 读入地址) -- 文件必须存在,否则异常
常用方法
方法名 | 作用 | 返回值 |
---|---|---|
read() | 读一个字节 返回其ASCII值 | int |
read(char[]) | 数组长度决定每次读入字节数 | int |
//准备读入的文件对象
File file = new File("路径\\文件");
//创建字符读入流对象
FileReader fr = new FileReader(file);
char[] cs = new char[4];
int l = 0;
while((l = fr.read(cs))!=-1) { //每次最多读4个字符
System.out.print(new String(cs,0,l));
}
//一次性读完
char[] cs = new char[(int)file.length];
//记录每次实际读入的字节数
fis.read(cs)
//String类构造方法将读入字节连接输出
syso(new String(cs);
}
字符写出流
new FileWriter(File 写出地址) -- 文件可不存在,自动创建
new FileWriter(File,boolean)
boolean:false(默认值)
false -- 覆盖原数据
true -- 原数据后追加
常用方法
方法名 | 作用 |
---|---|
write() | 将字符写出:字符串(部分)、数组 |
flush() | 刷新缓冲区 |
close() | 关闭对象 |
使用字符写出流写出字符后,必须刷新或者关闭,否则写出失败
缓冲流
字节读入缓冲流
new BufferedInputStream(FileInputStream)
字节写出缓冲流
new BufferedOutputStream(FileOutputStream)
字符读入缓冲流
new BufferedReader(FileReader) -- 额外方法:readLine()→读入整行
BufferedReader br = new BufferedReader(new FileReader(new File("路径\\文件")));
//使用缓冲字符读入流提供的整行读入方法
String str = null;
while((str = br.readLine())!=null) {
System.out.println(str);
}
br.close();
字符写出缓冲流
new BufferedWriter(FileWriter) -- -- 额外方法:newLine()→写出换行
对象流
将对象以字节码形式(加密)读写数据
被序列化的对象所属的类 必须实现可序列化接口 -- Serializable
为序列化类添加UID (增删属性后)保证数据反序列化成功
//序列号对象 -- 必须实现可序列化接口(Serializable)
public class User implements Serializable {
...
//为序列化类添加UID 保证数据反序列化成功
private static final long serialVersionUID = 1L;
}
序列化(写出)
标准
new ObjectOutputStream(FileOutputStream)
void writeObject(写出对象)
//写出文件地址
File file = new File("路径\\文件");
//普通字节写出流对象
FileOutputStream fos = new FileOutputStream(file);
//序列化流对象
ObjectOutputStream oos = new ObjectOutputStream(fos);
//写出对象
User u = new User("张三",18);
oos.writeObject(u);
//刷新
oos.flush();
//关闭
oos.close();
fos.close();
反序列化(读入)
new ObjectInputStream(FileInputStream)
Object readObject()
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("路径\\文件")));
//对象存储
User user = (User)ois.readObject();
System.out.println(user.name+" "+user.age);
ois.close();