1 知识结构
![](https://i-blog.csdnimg.cn/blog_migrate/84a4831f212bbabffd389bcde511c766.png)
2 .1 练习 File文件类的测试
import java.io.File;
import java.io.IOException;
//测试 File文件类
public class Test666 {
public static void main(String[] args) throws IOException {
//1.创建文件类对象
File f1=new File("D:\\ready\\KKK");
//2.1创建文件夹
System.out.println(f1.mkdir());//创建单层文件夹
File f2=new File("D:\\ready\\A\\B\\c");
System.out.println(f2.mkdirs());//创建多层文件夹
//2.2创建文件
File f3=new File("D:\\ready\\KKK\\1.txt");
System.out.println(f1.createNewFile());//true---创建成功,在磁盘中查看
/*createNewFile()会抛出异常,可以选择在抛出异常*/
//3.测试基本属性---需要在1.txt文件中写入一些文字
System.out.println(f3.length());//获取文件的字节量
System.out.println(f2.exists());//判断文件是否存在
System.out.println(f3.isFile());//判断是否为文件
System.out.println(f1.isDirectory());//判断是否为文件夹
System.out.println(f3.getName());//获取文件名
System.out.println(f3.getParent());// 获取父级路径
System.out.println(f3.getAbsolutePath());//获取带盘符的完整路径:绝对路径
//4.展示文件列表
File f4=new File("D:\\ready");
System.out.println(f4.listFiles());
System.out.println(f2.delete());//删除空文件夹
System.out.println(f3.delete());//删除文件
}
}
2 .2 练习 用于字节流测试
import java.io.*;
public class Test98989889 {
public static void main(String[] args) {
method1();//用于字节流测试
}
private static void method1() {
//声明变量
InputStream in1=null;
OutputStream out1=null;
try {
//1.1创建字节输入流对象(选其一即可)
in1=new FileInputStream("D:\\ready\\yy.jpg");//多态形式创建字节输入流对象
InputStream in2=new FileInputStream(
new File("D:\\ready\\yy.jpg"));//多态形式多态形式创建字节输入流对象
FileInputStream in3=new FileInputStream("D:\\ready\\yy.jpg");
FileInputStream in4=new FileInputStream(
new File("D:\\ready\\yy.jpg"));
BufferedInputStream in5=new BufferedInputStream(//创建高效字节输入流对象
new FileInputStream(new File("D:\\ready\\yy.jpg")));
BufferedInputStream in6=new BufferedInputStream(//创建高效字节输入流对象
new FileInputStream("D:\\ready\\yy.jpg"));
//1.2创建字节输入流对象(选其一即可)
out1=new FileOutputStream("D:\\ready\\yy.jpg");//多态形式创建字节输出流对象
OutputStream out2=new FileOutputStream(//多态形式创建字节输出流对象
new File("D:\\ready\\yy.jpg"));
OutputStream out3=new FileOutputStream(//多态形式创建字节输出流对象
new File("D:\\ready\\yy.jpg"),true);
FileOutputStream out4=new FileOutputStream("D:\\ready\\yy.jpg");
FileOutputStream out5=new FileOutputStream(new File("D:\\ready\\yy.jpg"));
FileOutputStream out6=new FileOutputStream("D:\\ready\\yy.jpg",true);
BufferedOutputStream out7=new BufferedOutputStream(//创建高效字节输出流对象
new FileOutputStream("D:\\ready\\yy.jpg"));
BufferedOutputStream out8=new BufferedOutputStream(//创建高效字节输出流对象
new FileOutputStream(new File("D:\\ready\\yy.jpg")));
BufferedOutputStream out9=new BufferedOutputStream(//创建高效字节输出流对象
new FileOutputStream("D:\\ready\\yy.jpg",true));
//2.使用流对象
int b;
while ((b=in1.read())!=-1){
out1.write(b);
}
System.out.println("恭喜你,图片复制成功!");
} catch (Exception e) {
System.out.println("很遗憾,图片复制失败~");
e.printStackTrace();
}finally {
try {
//3.关流
out1.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2 .3 练习 用于字节流测试
import java.io.*;
public class Test777777 {
public static void main(String[] args) {
method1();
}
private static void method1() {
//声明变量
Writer out=null;
Reader in=null;
//1.1创建字符输出流(选其一即可)
try {
out=new FileWriter("D:\\ready\\1.txt");//多态形式创建字符输出流对象
Writer out1=new FileWriter(new File("D:\\ready\\1.txt"));//多态形式创建字符输出流对象
Writer out6=new FileWriter(new File("D:\\ready\\1.txt"),true);//多态形式创建字符输出流对象--覆盖输出
FileWriter out2=new FileWriter("D:\\ready\\1.txt");
FileWriter out3=new FileWriter(new File("D:\\ready\\1.txt"));
FileWriter out7=new FileWriter("D:\\ready\\1.txt",true);
BufferedWriter out4=new BufferedWriter(//创建高效字符输出流对象
new FileWriter("D:\\ready\\1.txt"));
BufferedWriter out5=new BufferedWriter(//创建高效字符输出流对象
new FileWriter(new File("D:\\ready\\1.txt")));
BufferedWriter out8=new BufferedWriter(//创建高效字符输出流对象---覆盖输出
new FileWriter(new File("D:\\ready\\1.txt"),true));
//1.2创建字符输入流(选其一即可)
in=new FileReader("D:\\ready\\1.txt");//多态形式创建字符输入流对象
Reader in1=new FileReader(new File("D:\\ready\\1.txt"));//多态形式创建字符输入流对象
FileWriter in2=new FileWriter("D:\\ready\\1.txt");
FileWriter in3=new FileWriter(new File("D:\\ready\\1.txt"));
BufferedWriter in4=new BufferedWriter(//创建高效字符输入流对象
new FileWriter(new File("D:\\ready\\1.txt")));
BufferedWriter in5=new BufferedWriter(//创建高效字符输入流对象
new FileWriter("D:\\ready\\1.txt"));
//2.使用流对象
int a;
while ((a=in.read())!=-1){
out.write(a);
}
System.out.println("恭喜您!文档复制成功!");
} catch (IOException e) {
System.out.println("很抱歉!文档复制失败~");
e.printStackTrace();
}finally {
try {
//3.关流
out.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}