1.目录
File f = new File("f:/data");
// 1. 文件或目录是否存在,存在true
System.out.println(f.exists());// true
// 2. 是否是目录,是true
System.out.println(f.isDirectory());// true
// 3 获得 目录下的子目录和文件
// 获得的是名称的数组 String []
File f = new File("f:/data2/data3");
System.out.println(f.exists()); // false
// 1. 创建目录
// mkdir () 但是父目录不存在,创建失败
f.mkdir();
// mkdirs() 父目录不存在,会创建父目录
f.mkdirs();
函数式接口:
1.FilenameFilter:文件名过滤器
// 3 获得 目录下的子目录和文件
// 获得的是名称的数组 String []
System.out.println("-----------------------------");
String [] fs = f.list();
Arrays.stream(fs).forEach(System.out::println);
// 过滤:只展示 java文件
fs = f.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith("java");
}
});
fs = f.list((dir,name)-> name.endsWith("jpg"));
Arrays.stream(fs).forEach(System.out::println);
2.FileFilter :文件过滤器
// 获得的是 文件和目录本身File的数组
System.out.println("---------------------------------");
File[] files = f.listFiles();
for(File fi : files) {
if(fi.isDirectory()) {
System.out.println(fi.getAbsolutePath());
}else if(fi.isFile()) {
System.out.println(fi.getName());
}
}
// 过滤 java文件
files = f.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith("java");
}
});
files = f.listFiles(p-> p.getName().endsWith("jpg"));
for(File fi : files) {
System.out.println(fi.getName());
}
2.流
2.1 分类
1)按照流的数据类型:分为字节流和字符流
2)按照方向:输入和输出
输出: (写) String -> a.txt
内存 -> 磁盘
输入:(读) a.txt -> String []
磁盘 -> 内存
3)按照功能:节点流和处理流
节点流: 直接操作数据源。
处理流: 包装在节点流的基础上的。
2.2 字节流
字节为单位处理的。
步骤:
1. 创建流对象 new
2. 读/写;
3. 关流 close()
2.3 文件流(节点流)
FileInputStream
FileOutputStream
2.3.1 读:
// f:/data/a.txt 读取出来,在控制台上展示
// 1.创建流对象
// FileNotFoundException
//FileInputStream fin = new FileInputStream("f:/data/a.txt");
File f = new File("f:/data/a.txt");
FileInputStream fin = new FileInputStream(f);
// 2.读一个字节 IOException -1 文件尾
int temp;
while((temp = fin.read() )!= -1) {
System.out.print((char)temp);
}
// 3.关闭流
fin.close();
2.3.2 读:中文处理
方式一: 用了 字符流: 转换字符流
InputStreamReader
OuputStreamWriter
// 1.创建流对象
FileInputStream fin = new FileInputStream("f:/data/a.txt");
// 把字节流 转换成字符流
InputStreamReader ir = new InputStreamReader(fin);
// 2.读一个字节 IOException -1 文件尾
int temp;
while((temp = ir.read() )!= -1) {
System.out.print((char)temp);
}
// 3.关闭流
ir.close();
fin.close();
ir.close();
方式二:byte[]
// 1.创建流对象
File f = new File("f:/data/a.txt");
FileInputStream fin = new FileInputStream(f);
// 2.读一个字节 IOException -1 文件尾
byte [] b = new byte[(int)f.length()];// 获得大小(1)
byte [] b = new byte[fin.available()]; // 获得大小(2)
fin.read(b);
String str = new String(b,"GBK");
System.out.println(str);
// 3.关闭流
fin.close();
2.3.3 read()方法重载:
// 1.创建流对象
File f = new File("f:/data/a.txt");
FileInputStream fin = new FileInputStream(f);
// 2.读一个字节 IOException -1 文件尾
byte [] b = new byte[(int)f.length()];// 获得大小(1)
byte [] b = new byte[fin.available()]; // 获得大小(2)
// 参数( 存储数据的数组,读到数组的什么位置,读几个字节)
fin.read(b, 2, 2);
String str = new String(b,"GBK");
System.out.println(str);
// 3.关闭流
fin.close();
2.3.4 try-catch处理代码语法:
public static void main(String[] args) {
// try-catch处理
FileInputStream fin = null;
try {
fin = new FileInputStream("f:/data/a.txt");
System.out.println((char)fin.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.3.5 输出流:FileOutputStream
public static void main(String[] args) throws IOException {
// 把String s= "hello"存到 f:/data/b.txt
// 1. 流对象
FileOutputStream fout = new FileOutputStream("f:/data/b.txt");
// 2. 写
String s = "hello你好";
fout.write(98);// (1)写一个字节
byte [] b = s.getBytes();
fout.write(b); // (2)写一个字节数组
//(3) (写的字节数组,从什么位置写,写几个字节)
fout.write(b, 2, 2);// ll
// 3. 关
fout.close();
}
2.3.6覆盖和追加内容:
// 覆盖false (默认) ,true (追加)
FileOutputStream fout = new FileOutputStream("f:/data/b.txt",true);
2.4 转换字符流
InputStreamReader
OuputStreamWriter
2.5 缓冲流
public static void main(String[] args) throws IOException {
//1.
FileInputStream fin = new FileInputStream("f:/data/datou.jpg");
BufferedInputStream bfin = new BufferedInputStream(fin);//读 8192
FileOutputStream fout = new FileOutputStream("f:/data/datounew.jpg");
BufferedOutputStream bfout = new BufferedOutputStream(fout);// 写 8192
//2
int temp;
while((temp = bfin.read())!= -1) {
bfout.write(temp);
}
bfout.flush();// 刷新缓冲区
//3
bfin.close();
bfout.close();
}
2.5.1 Scanner类:
// 键盘输入: System.in
Scanner input = new Scanner(System.in);
int n = input.nextInt();
System.out.println(n);
input.close();
//-----------------------------------------------
FileInputStream fin = new FileInputStream("f:/data/a.txt");
Scanner input1 = new Scanner(fin);
String s = input1.next();
System.out.println(s);
//--------------------------------------------------
Scanner input2 = new Scanner("aa bb cc dd ee");
// System.out.println(input2.next());
System.out.println(input2.nextLine());
2.6 数据流
DataInputStream
DataOutputStream
public static void main(String[] args) throws IOException {
// 把数组的内容 存到 文件中
int [] no = {11,22,33};
String [] name = {"aa","bb","cc"};
//---------------写----------------------
// 1.
FileOutputStream fout = new FileOutputStream("f:/data/arr.txt");
DataOutputStream dfout = new DataOutputStream(fout);
// 2. 写
for(int i = 0; i < no.length; i++) {
dfout.writeInt(no[i]);
dfout.writeUTF(name[i]);
}
// 3.关
dfout.close();
//------------------读---------------------------------------
// 1.
FileInputStream fin = new FileInputStream("f:/data/arr.txt");
DataInputStream dfin = new DataInputStream(fin);
// 2.读
for(int i = 0 ; i < no.length; i++) {
System.out.println(dfin.readInt());
System.out.println(dfin.readUTF());
}
//3.
dfin.close();
}
2.7 对象流
ObjectInputStream: 反序列化。
ObjectOutputStream: 序列化。
示例:
class Student implements Serializable{
private int no;
private String name;
public Student(int no, String name) {
super();
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "Student [no=" + no + ", name=" + name + "]";
}
}
public class TestObjectOutputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student guojing = new Student(11, "郭靖");
// 序列化 :把 guojing对象存储到 磁盘文件中 obj.txt
// 1.
FileOutputStream fout = new FileOutputStream("f:/data/obj.txt");
ObjectOutputStream objOut = new ObjectOutputStream(fout);
// 2.写
objOut.writeObject(guojing);
// 3.
objOut.close();
//--------------------还原---------------------------------------------
// 反序列化: 把 文件中的 对象还原
// 1.
FileInputStream fin = new FileInputStream("f:/data/obj.txt");
ObjectInputStream objIn = new ObjectInputStream(fin);
// 2.读
Student stu = (Student)objIn.readObject();
System.out.println(stu.toString());
//3.
objIn.close();
}
}
2.7.1 版本号
private static final long serialVersionUID = 1L;
2.8 字符流
字符为单位。
文本文件。
2.8.1 文件流(节点流)
FileReader
FileWriter
2.8.1.1 读:FileReader
public static void main(String[] args) throws IOException {
// f:/data/a.txt
// 1.
FileReader fr = new FileReader("f:/data/a.txt");
// 2.读
int temp;
while((temp = fr.read()) != -1) {
System.out.print((char)temp);
}
// 3.
fr.close();
}
2.8.1.1 写:FileWriter
public static void main(String[] args) throws IOException {
// String s = "hello你好";
// 1.
FileWriter fw = new FileWriter("f:/data/b.txt",true);
// 2.写
String s = "abc abc hello你好";
fw.write(s);
// 3.
fw.close();
}
2.8.2 缓冲流
BufferedReader
BufferedWriter
public static void main(String[] args) throws IOException {
// 1
FileReader fr = new FileReader("f:/data/a.txt");
BufferedReader bfr = new BufferedReader(fr);
// 2 读到文件尾 null
String str ;
while((str = bfr.readLine()) != null) {
System.out.println(str);
}
// 3
bfr.close();
}
2.8.3 文本输出流
PrintWriter
public static void main(String[] args) throws IOException {
// 打印输出流
// 1
PrintWriter pw = new PrintWriter("f:/data/print.txt");
// 2
for(int i = 1; i <= 5; i++) {
System.out.println("数字是:" + i);
pw.println("数字是:" + i);
}
// 3
pw.close();
}
3. try-with-resources 自动资源释放
前提: AutoCloseable接口
try (PrintWriter pw = new PrintWriter("f:/data/print.txt");){
for(int i = 1; i <= 5; i++) {
pw.println("数字是:" + i);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
4.编写:字节流 读写图片
public class TestFileOutputStream2_exam {
public static void main(String[] args) throws IOException {
//1.
FileInputStream fin = new FileInputStream("f:/data/datou.jpg");
BufferedInputStream bfin = new BufferedInputStream(fin);//读 8192
FileOutputStream fout = new FileOutputStream("f:/data/datounew.jpg");
BufferedOutputStream bfout = new BufferedOutputStream(fout);// 写 8192
//2
int temp;
while((temp = bfin.read())!= -1) {
bfout.write(temp);
}
bfout.flush();// 刷新缓冲区
//3
bfin.close();
bfout.close();
}
}
5. 编写:字节流 控制台输入的人名写入到文件中
public class TestBufferedOutputStream_exam {
public static void main(String[] args) throws IOException {
//1
Scanner input = new Scanner(System.in);
FileOutputStream fout = new FileOutputStream("f:/data/name.txt");
BufferedOutputStream bfout = new BufferedOutputStream(fout);
//2
String name;
while(true) {
System.out.println("-- 输入名字:");
name = input.next(); // 读
if(name.equals("q"))
break;
bfout.write(name.getBytes()); // 写
bfout.write("\r\n".getBytes());
}
bfout.flush();//刷新
//3
input.close();
bfout.close();
}
}
6. 注意:
1.在读写文件时,若直接调用字节流会导致效率很低(直接对磁盘操作),因为内存拍卖一次跑的过程只能带一个货物。
所以我们使用缓冲流Buffer创造一个读写的小车(缓冲区直接对内存操作),使内存每一次跑的时候可以带更多的“货物”,从而提高效率
最后才close前添加bfout.flush();
进行刷新缓冲区,从而保证货物安全传递。
2.写入时先写啥先还原啥
3.了解printString
4.重点:
字节流
buffer缓冲流
序列化与反序列化
5.自动清除:
将close省去,放入结构之中,这样无需抛出异常,也不用写异常声明了。