- 文件:
- 文本文件:
- 二进制文件:
- File 常⽤构造⽅法
- 观察 get 系列的特点和差异
- 文件的读写——数据流
- InputStream:输⼊流
- InputStream 常⽤⽅法
- 文件的读取
- OutputStream:输出流
- OutputStream 常⽤⽅法
- 观察字符串写入
- 使⽤ PrintWriter 进⾏写⼊
- 使⽤ FileWriter 追加数据
针对硬盘这种持久化存储的I/O设备,当我们想要进⾏数据保存时,往 往不是保存成⼀个整体,⽽是独⽴成⼀个个的单位进⾏保存,这个独⽴的单位就被抽象成⽂件的概念, 就类似办公桌上的⼀份份真实的⽂件⼀般。
文件的组织:
就是按照层级结构进⾏组织 —— 也就是我们数据结构中学习过的树形结构。
文件路径:
树中的每个结点都可以被⼀条从根开始,⼀直到达的结点的路径所描 述,⽽这种描述⽅式就被称为⽂件的绝对路径(absolute path)。
文件类型:
保存被字符集编码的⽂本
二进制文件:按照标准格式保存的⾮被字符集编码过的⽂件png)
文件权限:
⽂件由于被操作系统进⾏了管理,所以根据不同的⽤户,会赋予⽤户不同的对待该⽂件的权限,⼀般地 可以认为有可读、可写、可执⾏权限。
Java中操作文件:java.io.File 类来对⼀个⽂件(包括⽬录)进⾏抽象的描述。注意,有 File 对象,并 不代表真实存在该⽂件。
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
// 绝对路径(完整路径)
// File file = new File("D:\Data\a.txt");
// 相对路径
File file = new File("../a.txt");
System.out.println(file.getParent()); // ..
System.out.println(file.getName()); // a.txt
System.out.println(file.getPath());// ..
System.out.println(file.getAbsolutePath()); // D:ioexample..a.txt
System.out.println(file.getCanonicalPath()); // D:a.txt
}
}
结果
InputStream 输⼊流是⽤来读取数据的。
InputStream 常⽤⽅法 文件的读取import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamExample {
public static void main(String[] args) throws IOException {
// 创建流
InputStream inputStream = null;
try {
inputStream = new FileInputStream("hello.txt");
byte[] bytes = new byte[1024];
int c;
while (true) {
c = inputStream.read(bytes);
if (c == -1) break;
System.out.println(new String(bytes, "utf-8"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
// 关闭流
if (inputStream != null) inputStream.close();
}
}
}
OutputStream:输出流
OutputStream 输出流是进⾏数据写⼊的。
OutputStream 常⽤⽅法 观察字符串写入import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
try (OutputStream os = new FileOutputStream("output.txt")) {
String s = "Nothing";
byte[] b = s.getBytes();
os.write(b);
// 不要忘记 flush
os.flush();
}
}
}
使⽤ PrintWriter 进⾏写⼊
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
public class OutputStreamExample {
public static void main(String[] args) throws IOException {
File file = new File("hello.txt");
if (!file.exists()) {
System.out.println("⽂件不存在,先创建!");
if (!file.createNewFile()) {
System.out.println("⽂件创建成功!");
return;
}
}
// 给⽂件写⼊数据
try (PrintWriter printWriter = new PrintWriter(file)) {
printWriter.println("第⼀⾏数据");
printWriter.print("第⼆⾏数据");
printWriter.printf("第⼆⾏追加数据:%s", "Over.");
printWriter.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
使⽤ FileWriter 追加数据
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) throws IOException {
File file = new File("hello.txt");
if (!file.exists()) {
System.out.println("⽂件不存在,先创建!");
if (!file.createNewFile()) {
System.out.println("⽂件创建成功!");
return;
}
}
// 给⽂件追加数据
try (FileWriter fileWrite = new FileWriter(file, true)) { // 第⼆ 个参数设置为 true
fileWrite.append("我是追加的数据n");
fileWrite.append("我是追加的数据2n");
fileWrite.flush();
}



