1 文件
文件在程序中是以流的形式来操作的
输入流:数据从输入源/文件到程序/内存的路径
输出流:数据从程序/内存到输入源/文件的路径
常用的文件操作
(1) 创建文件对象(包括文件和文件夹)相关构造器和方法
new File(String pathname) //根据路径构建File对象
new File(File parent, String child) //根据父目录文件+子路径构建
new File(String parent, String chile) //根据父目录+子路径构建
creatNewFile // file仅是java程序中的一个对象,只有执行了creatNewFile方法,才在磁盘中创建文件
(2)获取文件的相关信息
getName
getAbsolutePath
getParent
length文件的大小
exists
isFile
isDirectory
(3)目录的操作和删除
mkdir() //创建一级目录(文件夹),返回boolean
mkdirs() //创建多级目录(文件夹),返回boolean
delete() //删除空目录或文件,返回boolean,可同时做判断条件看删除成功与否
2 IO流
流的分类
按操作数据单位不同分为:
字节流(8bit)二进制文件 | 字符流(按字符)文本文件 | |
---|---|---|
输入流 | InputStream 按字节读取效率较慢 | Reader |
输出流 | OutputStram 如图片/音乐/视频/doc/pdf | Writer |
按流的角色不同分为:节点流,处理流/包装流
常用的流(节点流)
FileInputStream & FileOutputStream
FileInputStream
1 创建输入流对象用于读取文件
2 对象调用read()方法≠-1,while循环继续读取,转换为char显示
3 关闭流资源
String filePath = "xxx";
//(1)一次读取一个字节
int readData = 0;
//创建 FileInputStream,用于读取文件
FileInputStream fileInputStream = new FileInputStream(filePath);
//从该输入流中读取一个字节的数据。如果返回-1,表示读取完毕。如果没有输入可用,该方法将阻止。
while((readData = fileInputStream.read()) != -1 ) {
System.out.print((char)readData);//转成char显示
}
//(2)一次读取多个字节
byte[] buf = new byte[8]; //初始化字节数组,如音乐/图片一次读取1024个字节(1kb)
int readLen = 0;
FileInputStream fileInputStream = new FileInputStream(filePath);
//从该输入流读取最多buf.length字节的数据到字节数组。如果返回-1,表示读取完毕。如果没有输入可用,该方法将阻止。
while((readLen = fileInputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen)); //显示:如果读取正常,返回实际读取的字节数
}
//关闭流资源
fileInputStream.close();
FileOutputStream
1 创建FileOutputStream对象,获得输出流,用于写文件
2 通过write(char), write(byte[] b)写入内容到文件输出流
3 关闭输出流资源
//创建FileOutputStream对象,获得输出流
String filePath = "xxx"; //如果文件不存在,会创建文件,前提时目录已存在
FileOutputStream fileOutputStream = new (filePath); //(1)写入内容会覆盖原内容
FileOutputStream fileOutputStream = new (filePath, true);//(2)写入内容会追加原内容
//写入内容
fileOutputStream.write('H'); //(1)写入一个字符
String str = "hello, world!"; //(2)写入字符串
fileOutputStream.write(str.getBytes()); //getBytes()把字符串转换为字节数组
fileOutputStream.write(str.getBytes(), 0, 3); //write(byte[] b, int off, int len)将len字节从位于偏移量off的指定字节数组写入此文件输出流
//关闭输出流资源
fileOutputStream.close();
应用FileCopy
String srcFilePath = "srcFilePath";
String destFilePath = "destFilePath";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
byte[] buf = new byte[1024];
int readLen = 0;
//1 创建流对象
fileInpuStream = new FileInputStream("srcFilePath");
fileOutpuStream = new FileOutputStream("destFilePath");
// 2 读取并写入
while((readLen = fileInputStream.read(buf)) != -1) {
fileOustStream.write(buf, 0, readLen);
}
//3 关闭流资源
fileOutputStream.close();
fileInputStream.close();
FileReader & FileWriter
FileReader
//创建FileReader对象
new FileReader(File/String)
//读取文件
read() //每次读取单个字符,返回该字符,如果到文件末尾返回-1
read(char[]) //批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾返回-1
//相关API
new String(char[]) //将char[]转换成String
new String(char[], off, len) //将char[]的指定部分转换成String
//关闭流资源
FileWriter
//创建FileWriter对象
new FileWriter(File/String) //(1)覆盖模式
new FileWriter(Fiel/String, true) //(2)追加模式
//写入内容到输出流
write(char) //写入单个字符
write(char[]) //写入指定数组
write(char[], off, len) //写入指定数组的指定部分
write(string) //写入整个字符串
write(string, off, len) //写入字符串的指定部分
//相关API
toCharArray() //将String类转换成char[]
//FileWriter使用后,必须要关闭close或刷新flush,否则写不到指定的文件
处理流
节点流时底层流/低级流,直接跟数据源相接
处理流/包装流包装节点流,使用了修饰器模式,不会直接与数据源相连,消除了不同节点流的实现差异,也提供更方便的方法完成输入输出
优点1:增加缓冲提高输入输出效率
优点2:提供方法一次输入输出大批量数据,操作更便捷
处理流 BufferedReader & BufferedWriter
String srcFilePath = "srcFilePath";
String destFilePath = "destFilePath";
//创建 BufferedInputStream和BufferedOutputStream对象
BufferedReader br = null;
BufferedWriter bw = null;
br = new BufferedReader(new FileReader(srcFilePath));
bw = new BufferedWriter(new FileWriter(destFilePath));
//IO读写
String line;
while((line = br.readLine) != null) { //readLine读取一行内容,但没有换行
bw.write(line);
bw.newLine; // 插入一个换行
}
//关闭流,关闭外层处理流即可,底层会去关闭节点流
bw.close();
br.close();
处理流 BufferedInputStream & BufferedOutputStream
String srcFilePath = "srcFilePath";
String destFilePath = "destFilePath";
//创建 BufferedInputStream和BufferedOutputStream对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
bis = new BufferedInputStream(new FileinputStream(srcFilePath));
bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
//循环读取文件并写入
byte[] buf = new byte[1024];
int readLen = 0;
while((readLen = bis.read(buf)) != -1) {
bos.write(buf, 0, readLen);
}
//关闭流,关闭外层处理流即可,底层回去关闭节点流
bos.close();
bis.close();
转换流 InputStreamReader & OutputStreamWriter
// 把FileInputStream 转成InputStreamReader,指定编码gbk/utf-8,把转换成的InputStreamReader传入BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "gbk"));