# 1 - File 类
# 1.1 - 基本概念
java.io.File类用于描述文件和目录的路径信息,可以获取文件大小等相关属性。
# 1.2 - 构造方法
方法名 |
说明 |
File(String pathname) |
通过给定的路径名转换为 File 对象 |
File(String parent,String child) |
从父类的路径名和子类的文件名来创建一个 File 对象 |
File(File parent,String child) |
从父类的 Flie 目录和子类的文件名来创建一个 File 对象 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import java.io.File;
public class Demo02 { public static void main(String[] args) {
File file = new File("./myTest.txt"); System.out.println(file);
File file1 = new File("./", "myTest.txt"); System.out.println(file1);
File file2 = new File("./"); File file3 = new File(file2, "myTest.txt"); System.out.println(file3); } }
|
# 1.3 - File 类的创建功能
方法名 |
说明 |
boolean createNewFile() |
创建文件 |
boolean mkdir() |
创建目录(单个) |
boolean mkdirs() |
创建目录(多级) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import java.io.File; import java.io.IOException;
public class Demo03 { public static void main(String[] args) throws IOException { File file = new File("./myTest.txt"); if (file.createNewFile()){ System.out.println("文件创建成功!"); }else{ System.out.println("文件创建失败!"); }
File myDir = new File("./", "myDir"); if (myDir.mkdirs()){ System.out.println("目录创建成功!"); }else{ System.out.println("目录创建失败!"); }
File file1 = new File("./H/E/L/L/O");
if (file1.mkdirs()){ System.out.println("多级目录创建成功~"); }else{ System.out.println("多级目录创建失败~"); } } }
|
# 1.4 - File 类的常用功能
方法名 |
说明 |
boolean isDirectory() |
判断一个 File 对象指向的是否是一个目录 |
boolean isFile() |
判断一个 File 对象指向的是否是一个文件 |
boolean exists() |
判断一个 File 对象指向的文件或目录是否存在,存在则返回 true |
String getAbsolutePath() |
获取 File 对象指向的文件或目录的绝对路径 |
String getPath() |
获取创建 File 对象时候使用的路径和文件名 |
String getName() |
获取创建 FIle 对象的文件名 |
String getParent() |
获取创建 File 对象所在的目录名 |
File[] listFiles() |
获取 File 对象中所有的文件(File 对象形式) |
long lastModified() |
获取到 File 对象中文件最后被修改的时间 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import java.io.File; import java.text.SimpleDateFormat; import java.util.Date;
public class Demo04 { public static void main(String[] args) { File file = new File("./"); File file1 = new File("./a.txt"); File file2 = new File("./myTest.txt"); File file3 = new File("./myDir");
System.out.println(file.isDirectory()); System.out.println(file1.isDirectory()); System.out.println(file2.isDirectory()); System.out.println(file3.isDirectory()); System.out.println("----------------------------------------------"); System.out.println(file.isFile()); System.out.println(file1.isFile()); System.out.println(file2.isFile()); System.out.println(file3.isFile()); System.out.println("----------------------------------------------"); System.out.println(file.exists()); System.out.println(file1.exists()); System.out.println(file2.exists()); System.out.println(file3.exists()); System.out.println("----------------------------------------------"); System.out.println(file.getAbsolutePath()); System.out.println(file1.getAbsolutePath()); System.out.println(file2.getAbsolutePath()); System.out.println(file3.getAbsolutePath()); System.out.println("----------------------------------------------"); System.out.println(file.getPath()); System.out.println(file1.getPath()); System.out.println(file2.getPath()); System.out.println(file3.getPath()); System.out.println("----------------------------------------------"); System.out.println(file.getName()); System.out.println(file1.getName()); System.out.println(file2.getName()); System.out.println(file3.getName()); System.out.println("----------------------------------------------"); System.out.println(file.getParent()); System.out.println(file1.getParent()); System.out.println(file2.getParent()); System.out.println(file3.getParent()); System.out.println("----------------------------------------------"); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { System.out.println(files[i]); }
System.out.println("----------------------------------------------"); Date date = new Date(file2.lastModified()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = sdf.format(date); System.out.println(format); } }
|
# 1.5 - File 类的删除功能
方法名 |
说明 |
boolean delete() |
删除文件或(空)目录 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import java.io.File;
public class Demo05 { public static void main(String[] args) {
File file = new File("./H");
if(file.exists()){ if (file.delete()){ System.out.println(file.getName() + "删除成功!"); }else{ System.out.println(file.getName() + "删除失败!"); } }else{ System.out.println(file.getName() + "不存在!"); } } }
|
# 1.6 - 目录的递归删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import java.io.File;
public class Demo06 { public static void main(String[] args) { final String SRC = "./H"; File file = new File(SRC); delDir(file); }
private static void delDir(File file) { File[] files = file.listFiles(); if(files.length > 0){ for (int i = 0; i < files.length; i++) { if(files[i].isDirectory()){ delDir(files[i]); }else{ files[i].delete(); } } } file.delete(); } }
|
# 1.7 - 修改文件后缀名
- 将指定目录下的所有文件后缀名为 txt 统一修改成 md 为结尾的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import java.io.File;
public class Demo07 { public static void main(String[] args) { File file = new File("./dir"); renameFile(file); }
private static void renameFile(File file) { if(file.exists()){ File[] files = file.listFiles(); for (File f : files) { if(f.isFile()){ if(f.getName().endsWith("txt")){ String parent = f.getParent(); String name = f.getName();
String newName = name.substring(0, name.lastIndexOf(".")) + ".md"; File file1 = new File(parent, newName); f.renameTo(file1); } }else{ renameFile(f); } } } } }
|
思考题
递归实现查看指定目录中的所有文件以及目录的绝对路径
# 2 - I/O 字节流
# 2.1 - I/O 分类
I/O : Input (输入) / Output (输出)
流:是一种抽象的概念,表示数据传输的总称,就是说数据在设备之间的传输称之为流,流的本质就是传输
站在内存的角度,以内存为基准去理解
# 2.2 - 字节流写数据
方法名 |
说明 |
FileOutputStream(String name) |
根据参数指定的路径来构造对象并关联起来 |
FileOutputStream(String name,boolean append) |
以追加的方式构造对象 |
void write(int b) |
用于将参数指定的单个字节写入到输出流中 |
void write(byte[] bytes) |
用于将参数指定的字节数组内容全部写入到输出流中 |
void write(byte[] bytes, int off , int len) |
用于将数组中的一部分内容写入到输出流中 |
void close() |
关闭流并释放资源 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
public class Demo02 { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("./myTest.txt");
byte[] bytes = "123456789腾讯".getBytes(); fos.write(bytes,5,7); fos.close();
} }
|
# 2.3 - 字节流异常处理
- 抛出去 或者 try、catch
- 通过 finally 来 close
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
public class Demo03 { public static void main(String[] args) { FileOutputStream fos = null; try { fos = new FileOutputStream("./myTest.txt"); byte[] bytes = "123456789腾讯".getBytes(); fos.write(bytes,5,7); } catch (IOException e) { e.printStackTrace(); }finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|
# 2.4 - 字节流读数据
方法名 |
说明 |
FileInputStream(File file) |
根据参数指定的 File 对象来构造字节输入流对象 |
FileInputStream(String name) |
根据参数指定的字符串来构造字节输入流对象 |
int read() |
用于从输入流中读取单个的字节数据 |
int read(byte[] bytes) |
用于从输入流中读取数组长度个字节数组 |
int read(byte[] bytes , int off ,int len) |
用于从输入流中读取数组中一部分内容 |
void close() |
关闭流并释放资源 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import java.io.FileInputStream; import java.io.IOException;
public class Demo04 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("./myTest.txt");
byte[] bytes = new byte[1024]; int len;
while ((len = fis.read(bytes)) != -1){ System.out.println(new String(bytes,0,len)); }
fis.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
public class Demo05 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("./myTest.txt"); FileOutputStream fos = new FileOutputStream("./newTest.txt");
int by; while ((by = fis.read()) != -1){ fos.write(by); }
fis.close(); fos.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
public class Demo06 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("./pic.png"); FileOutputStream fos = new FileOutputStream("./newPic.png");
byte[] bytes = new byte[1024]; int len; while ((len = fis.read(bytes)) != -1){ fos.write(bytes,0,len); }
fis.close(); fos.close(); } }
|
# 3 - I/O 字符流
# 3.1 - 字符输出流写数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter;
public class Demo01 { public static void main(String[] args) throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("./myTest.txt"));
char[] chs = {'a','b','c','d','e','f','g'};
osw.write(chs,4,2);
osw.close(); } }
|
# 3.2 - 字符输入流读数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader;
public class Demo02 { public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("./myTest.txt"));
char[] chs = new char[1024]; int len; while ((len = isr.read(chs)) != -1){ System.out.println(new String(chs,0,len)); }
isr.close(); } }
|
# 3.3 - 字符流的文本拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import java.io.*;
public class Demo03 { public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("./myTest.txt")); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("./newTest.txt"));
char[] chs = new char[1024]; int len; while ((len = isr.read(chs)) != -1){ osw.write(chs,0,len); }
isr.close(); osw.close(); } }
|
# 3.4 - 字符流的简化写法
FileReader FileWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import java.io.*;
public class Demo04 { public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("./myTest.txt"); FileWriter fw = new FileWriter("./newTest.txt");
char[] chs = new char[1024]; int len; while ((len = fr.read(chs)) != -1){ fw.write(chs,0,len); }
fr.close(); fw.close(); } }
|
# 3.5 - 缓冲区高效读写
方法名 |
说明 |
BufferedReader(Reader reader) |
根据参数构造缓冲区字符输入流对象 |
BufferedWriter(Writer writer) |
根据参数构造缓冲区字符输出流对象 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import java.io.*;
public class Demo05 { public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("./myTest.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("./newTest.txt"));
String line; while ((line = br.readLine()) != null){ bw.write(line); bw.newLine(); bw.flush(); }
br.close();
} }
|
# 4 - 标准输入 / 输出 / 错误流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import java.io.*; import java.util.Scanner;
public class Demo06 { public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new FileWriter("./myTest.txt"));
String str; while (true){ if((str = br.readLine()).equals("over")){ break; }
bw.write(str); bw.newLine(); bw.flush(); }
br.close(); bw.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintStream;
public class Demo07 { public static void main(String[] args) throws IOException { System.out.println("正常打印");
PrintStream out = System.out; BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write("Hello JavaSE!");
bw.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
|
public class Demo08 { public static void main(String[] args) { System.out.println("标准输出流"); System.err.println("标准错误流");
} }
|
# 5 - 对象的持久化存储
# 5.1 - 通过之前学过的知识去做对象的持久化存储
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import java.io.*;
public class Demo01 { public static void main(String[] args) throws IOException {
Student newStu = myLoad(); System.out.println(newStu); }
private static Student myLoad() throws IOException { BufferedReader br = new BufferedReader(new FileReader("./myTest.txt")); String str = br.readLine(); String[] sp = str.split("##");
br.close();
return new Student(sp[0],Integer.parseInt(sp[1])); }
private static void mySave() throws IOException { Student stu = new Student("Andy", 18); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./myTest.txt")));
bw.write(stu.getName() + "##" + stu.getAge());
bw.close(); } }
|
# 5.2 - 对象的序列化操作
方法名 |
说明 |
ObjectOutputStream(OutputStream out) |
通过参数构造序列化对象(写) |
ObjectInputStream(InputStream in) |
通过参数构造反序列化对象(读) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import java.io.*;
public class Demo02 { public static void main(String[] args) throws IOException, ClassNotFoundException {
System.out.println("-------------------------------------");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./myTest.txt"));
Student newStu = (Student) ois.readObject();
System.out.println(newStu);
ois.close(); } }
|