目录
java.io.File类:主要用于文件和目录路径名的创建,查找,删除操作
IO流FileReader,FileWriter对文本文件进行复制操作
IO流FileInputStream,FileOutputStream对非文本文件(图片,音频等等)进行操作
转换流InputStreamReader,OutputStreamWriter:字符--->字节的管道
java.io.File类:主要用于文件和目录路径名的创建,查找,删除操作
// 对文件进行操作 @Test public void file01(){ File file = new File("D:" + File.separator + "yzc" + File.separator + "filetest.txt"); System.out.println("文件是否可读:" + file.canRead()); System.out.println("文件是否可写:" + file.canWrite()); System.out.println("文件的名字:" + file.getName()); System.out.println("上级目录:" + file.getParent()); System.out.println("是否是一个目录:" + file.isDirectory()); System.out.println("是否是一个文件:" + file.isFile()); System.out.println("是否隐藏:" + file.isHidden()); System.out.println("文件的大小:" + file.length()); System.out.println("文件是否存在:" + file.exists()); } // File.separator:由于电脑操作系统不同,该方法可以根据电脑的操作系统来生成分隔符 // 结果: /* 文件是否可读:true 文件是否可写:true 文件的名字:filetest.txt 上级目录:D:\yzc 是否是一个目录:false 是否是一个文件:true 是否隐藏:false 文件的大小:5512 文件是否存在:true */
绝对路径和相对路径
绝对路径:从盘符开始的路径,是个完整的路径
相对路径:从项目目录开始的路径,便捷路径,开发中常使用
@Test
public void filepathTest() throws IOException {
File file = new File("file.txt");
/*
createNewFile():创建文件
exists():判断改文件是否存在
getAbsolutePath():获取绝对路径
getPath():获取相对路径
delete():删除文件
*/
if(!file.exists()){
System.out.println(file.createNewFile());
System.out.println(file.getAbsolutePath());
System.out.println(file.getPath());
System.out.println(file.toString());
}else{
System.out.println(file.delete());
}
}
/*
当文件file.txt不存在时的运行结果:
绝对路径:D:\yzc\IdeaProjects\HELLOWORLD\file.txt
相对路径:file.txt
toString:file.txt
当文件存在时的运行结果:true
*/
文件夹操作
mkdir():只能创建单层目录
mkdirs():可以创建多层目录
delete():只能删除最后一层空文件夹
@Test
public void dirTest(){
// 创建单层目录
File file = new File("D:\\yzc\\a");
file.mkdir();
// 创建多层目录
File file2 = new File("D:\\yzc\\a\\b\\c");
file2.mkdirs();
// 删除一层目录:只能删除最后一层文件夹,其中文件夹还得是空文件夹才行
file2.delete();
// 查看
/*
listFiles():获取到的是File对象,所以接下来可以实用各种file的方法获取目录或者增删改查操作
*/
File file3 = new File("D:\\yzc\\a\\b");
File[] files = file3.listFiles();
for (File f: files) {
System.out.println(f.getName() + "," + f.getAbsolutePath());
}
IO流FileReader,FileWriter对文本文件进行复制操作
字符流:
输入:Reader--->FileReader:输入流就是把数据从其他设备上读取到内存中的流。
包含的常用方法:
int read():每次可以读取一个字符数据,并自动提升为int类型。
void close():关闭输入流并释放与这个流相关的所有资源
/*复制文件操作(以灌溉为例:河-通过管道取水-存入水箱-通过管道喷水-水落入稻田地)
此测试方法测试的是:河-通过管道取水-存入水箱 */
@Test
public void testCopy() throws Exception {
// 准备一条河(被复制的文件)
File file = new File("D:\\yzc\\iotest\\iotest.txt");
// 准备好管道,把管道插入水中
FileReader fr = new FileReader(file);
// 通过管道吸水并装入水箱:read():每次只能读取文件中的一个字符,并自动提升为int类型,而一个文件中有很多字符,当读取完毕后,read返回值位-1
int read = fr.read();
while (read != -1){
read = fr.read();
}
// 水吸够了,关闭吸水管道
fr.close();
}
int read(char[] char):从输入流中读取一些字符,并存储到char[]中,最多存储该数组长度的字符,返回读取到的有效字符个数
/* 复制文件操作,一次性读出多个字符(以灌溉为例:河-通过管道取水-存入水箱-通过管道喷水-水落入稻田地)
此测试方法测试的是:河-通过管道取水-存入水箱 */
@Test
public void testCopy2() throws Exception{
// 准备一条河(被复制的文件)
File file = new File("D:\\yzc\\iotest\\iotest.txt");
// 准备好管道,把管道插入水中
FileReader fr = new FileReader(file);
/* 通过管道吸水并装入水箱:每次读五个字符,read(length):每次能读取文件中的length个字符,
当读取完毕前,read返回的是读取到的字符个数;读取最后一个后,read返回值位-1*/
char[] length = new char[5];
int read = fr.read(length);
while (read != -1){
// 看看每次读取的字符是什么
for (int i = 0;i < read; i++){
System.out.println(length[i]);
}
read = fr.read(length);
}
// 水吸够了,关闭吸水管道
fr.close();
}
@Test
public void testCopy3() throws Exception{
// 准备一条河(被复制的文件)
File file = new File("D:\\yzc\\iotest\\iotest.txt");
// 准备好管道,把管道插入水中
FileReader fr = new FileReader(file);
/* 通过管道吸水并装入水箱:每次读五个字符,read(length):每次能读取文件中的length个字符,并自动提升为int类型,
当读取完毕前,read返回的是读取到的字符个数;读取最后一个后,read返回值位-1*/
char[] length = new char[5];
int read = fr.read(length);
while (read != -1){
// 看看每次读取的字符是什么
String s = new String(length,0,read);
System.out.println(s);
read = fr.read(length);
}
// 水吸够了,关闭吸水管道
fr.close();
}
输出:Writer--->FileWriter:输出流就是把内存中的数据写到设备上的流。
包含的常用方法:
write(char c) :每次可以写出一个字符数据
write(String str) :每次可以写出字符串中的数据,更为方便
void close():关闭输出流并释放与这个流相关的所有资源
/* 复制文件操作,一次性读出多个字符(以灌溉为例:河-通过管道取水-存入水箱-通过管道喷水-水落入稻田地)
此测试方法测试的是:通过管道喷水-水落入稻田地 */
@Test
public void paste() throws Exception{
// 要知道灌溉的土地在哪(目标文件,如果不存在则自动创建)
File file = new File("D:\\yzc\\iotest\\iotest2.txt");
// 准备好水要通过管道
/* FileWriter(file,true):
true:表示对文件中的内容进行追加操作;
false:表示对文件中的内容进行替换操作*/
FileWriter fw = new FileWriter(file,true);
// 通过管道将水箱中的水喷出(准备输出)
String s = "我家有玩具 and dog";
// 一个字符一个字符的输出
for (int i = 0;i < s.length();i++){
fw.write(s.charAt(i));
}
// 几个字符几个字符的输出
String s2 = "过来play吧";
fw.write(s2);
// 水喷够了,关闭喷水管道
fw.close();
}
升级一下
String(length.0,read):字符串长度应该是读取到的有效字符个数,而不是缓存数组的长度
// 完成复制文件操作(以灌溉为例:河-通过管道取水-存入水箱-通过管道喷水-水落入稻田地)全部操作
@Test
public void cp() throws Exception {
// 准备被复制的文件
File file = new File("D:\\yzc\\iotest\\iotest.txt");
// 准备目标文件
File file2 = new File("D:\\yzc\\iotest\\iotest2.txt");
// 准备输入管道
FileReader fr = new FileReader(file);
// 准备输出管道
FileWriter fw = new FileWriter(file2);
// 一个字符一个字符的写入
/*int read = fr.read();
while (read != -1){
fw.write(read);
read = fr.read();
}*/
// 几个字符几个字符的写入1
/*char[] length = new char[5];// 缓存数组
int read = fr.read(length);
while (read != -1){
fw.write(length,0,read);
read = fr.read(length);
}*/
// 几个字符几个字符的写入2
char[] length = new char[5];// 缓存数组
int read = fr.read(length);
while (read != -1){
String s = new String(length,0,read);
fw.write(s);
read = fr.read(length);
}
// 关闭输入输出流
fw.close();
fr.close();
}
在一次升级:利用缓冲区完成文本文件的复制粘贴操作--->处理流--->在FileReader,FileWriter外面再套一层流:BufferedReader,BufferedWriter
IO流FileInputStream,FileOutputStream对非文本文件(图片,音频等等)进行操作
完成图片文件的复制粘贴操作
@Test
public void cpTest() throws Exception {
// 1.准备原始图片
File file1 = new File("D:\\yzc\\iotest\\iotest.JPG");
// 2.准备目标图片
File file2 = new File("D:\\yzc\\iotest\\iotest2.JPG");
// 3.准备输入流
FileInputStream fis = new FileInputStream(file1);
// 4.准备输出流
FileOutputStream fos = new FileOutputStream(file2);
// 5.读取和写入操作
//缓冲数组
byte[] length = new byte[1024*5];
int read = fis.read(length);
while (read != -1){
fos.write(length,0,read);
read = fis.read(length);
}
// 6.关闭流
fos.close();
fis.close();
}
再一次升级:利用缓冲区完成非文本文件复制粘贴操作--->处理流--->在FileInputStream,FileOutputStream外面再套一层流:BufferedInputStream,BufferedOutputStream
目的:减少访问硬盘的次数,做到复制或粘贴过程都只访问一次
@Test
public void bcpTest() throws Exception {
// 1.准备原始图片
File file1 = new File("D:\\yzc\\iotest\\iotest.JPG");
// 2.准备目标图片
File file2 = new File("D:\\yzc\\iotest\\iotest2.JPG");
// 3.准备输入流
FileInputStream fis = new FileInputStream(file1);
// 4.准备输出流
FileOutputStream fos = new FileOutputStream(file2);
// 5.功能加强:在FileInputStream外面套一根管--BufferedInputStream
BufferedInputStream bis = new BufferedInputStream(fis);
// 6.功能加强:在FileOutputStream外面套一根管--BufferedOutputStream
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 7.读取和写入操作
// 缓冲数组
byte[] length = new byte[1024*5];
int read = bis.read(length);
while (read != -1){
bos.write(length,0,read);
read = bis.read(length);
}
// 8.关闭流
bos.close();
bis.close();
/* 如果处理流包裹着节点流的话,那么其实只要关闭高级流Bu...,那么里面的字节流也会随之关闭
fos.close();
fis.close();*/
}
转换流InputStreamReader,OutputStreamWriter:字符--->字节的管道
把字符类文件通过转换流转换为字节
@Test
public void StreamReaderTest() throws Exception {
// 准备被复制的文件
File file1 = new File("D:\\yzc\\iotest\\iotest.txt");
// 准备目标文件
File file2 = new File("D:\\yzc\\iotest\\iotest2.txt");
// 3.准备输入流
FileInputStream fis = new FileInputStream(file1);
// 4.准备输出流
FileOutputStream fos = new FileOutputStream(file2);
// 5.准备转换流
InputStreamReader isr = new InputStreamReader(fis);
OutputStreamWriter osr = new OutputStreamWriter(fos);
// 缓冲数组
char[] length = new char[20];
// 6.开始转换
int read = isr.read(length);
while (read != -1){
osr.write(new String(length),0,read);
read = isr.read(length);
}
// 7.关闭流
osr.close();
isr.close();
}
把通过键盘输入的字符存储到硬盘的文本文件中
@Test
public void test01() throws IOException {
// 键盘录入
InputStream in = System.in;
// 将字节流转换为字符流
InputStreamReader isr = new InputStreamReader(in);
// 再外面套上一个缓冲流
BufferedReader bis = new BufferedReader(isr);
// 准备目标文件
File file = new File("D:\\yzc\\iotest\\iotest2.txt");
FileWriter fw = new FileWriter(file);
// 再外面套上一个缓冲流
BufferedWriter bw = new BufferedWriter(fw);
// 开始动作
String s = bis.readLine();
while (!s.equals("exit")){
bw.write(s);
bw.newLine();
s = bis.readLine();
}
// 关闭流
bw.close();
bis.close();
}