本篇博客,博主将从File类的用法及InputStream、OutputStream的用法几个方面详细介绍,坐好板凳发车啦~~
Java中通过Java.io.File类来对一个文件(包括目录)进行抽象的描述。注意,有File对象,并不代表真实存在该文件。
我们先来看看File类中的常见属性、构造方法和方法。
属性
修饰符及类型 属性 说明
static String pathSeparator 依赖于系统的路径分隔符,String类型的表示
static char pathSeparator 依赖于系统的路径分隔符,char类型的表示
构造方法
签名 说明
File(File parent,String child) 根据父目录+孩子文件路径,创建一个新的File实例
File(String pathnam) 根据文件路径创建一个新的File实例,路径可以是绝对路径
或是相对路径
File(String parent,String child) 根据父目录+孩子文件路径,创建一个新的File实例,
父目录用路径表示
方法


- import java.io.File;
- import java.io.IOException;
- public class Main {
- public static void main(String[] args) throws IOException {
- File file = new File("..\\hello-world.txt"); // 并不要求该⽂件真实存在
- System.out.println(file.getParent());
- System.out.println(file.getName());
- System.out.println(file.getPath());
- System.out.println(file.getAbsolutePath());
- System.out.println(file.getCanonicalPath());
- }
- }
-
-
- 运行结果
- ..
- hello-world.txt
- ..\hello-world.txt
- D:\代码练习\⽂件⽰例1\..\hello-world.txt
- D:\代码练习\hello-world.txt
- import java.io.File;
- import java.io.IOException;
- public class Main {
- public static void main(String[] args) throws IOException {
- File file = new File("hello-world.txt"); // 要求该⽂件不存在,才能看
- System.out.println(file.exists());
- System.out.println(file.isDirectory());
- System.out.println(file.isFile());
- System.out.println(file.createNewFile());
- System.out.println(file.exists());
- System.out.println(file.isDirectory());
- System.out.println(file.isFile());
- System.out.println(file.createNewFile());
- }
- }
-
-
- 运行结果
- false
- false
- false
- true
- true
- false
- true
- false
删除操作
- import java.io.File;
- import java.io.IOException;
- public class Main {
- public static void main(String[] args) throws IOException {
- File file = new File("some-file.txt"); // 要求该⽂件不存在,才能看到相
- System.out.println(file.exists());
- System.out.println(file.createNewFile());
- System.out.println(file.exists());
- System.out.println(file.delete());
- System.out.println(file.exists());
- }
- }
-
-
-
- 运行结果
- false
- true
- true
- true
- false
- import java.io.File;
- import java.io.IOException;
- public class Main {
- public static void main(String[] args) throws IOException {
- File file = new File("some-file.txt"); // 要求该⽂件不存在,才能看到相
- System.out.println(file.exists());
- System.out.println(file.createNewFile());
- System.out.println(file.exists());
- file.deleteOnExit();
- System.out.println(file.exists());
- }
- }
-
-
-
- 运行结果
- false
- true
- true
- true
- import java.io.File;
- import java.io.IOException;
- public class Main {
- public static void main(String[] args) throws IOException {
- File dir = new File("some-dir"); // 要求该⽬录不存在,才能看到相同的现
- System.out.println(dir.isDirectory());
- System.out.println(dir.isFile());
- System.out.println(dir.mkdir());
- System.out.println(dir.isDirectory());
- System.out.println(dir.isFile());
- }
- }
-
-
-
- 运行结果
- false
- false
- true
- true
- false
- import java.io.File;
- import java.io.IOException;
- public class Main {
- public static void main(String[] args) throws IOException {
- File dir = new File("some-parent\\some-dir"); // some-parent 和 so
- System.out.println(dir.isDirectory());
- System.out.println(dir.isFile());
- System.out.println(dir.mkdir());
- System.out.println(dir.isDirectory());
- System.out.println(dir.isFile());
- }
- }
-
-
-
-
- 运行结果
- false
- false
- false
- false
- false
注:mkdir() 的时候,如果中间⽬录不存在,则⽆法创建成功; mkdirs() 可以解决这个问题。
- import java.io.File;
- import java.io.IOException;
- public class Main {
- public static void main(String[] args) throws IOException {
- File dir = new File("some-parent\\some-dir"); // some-parent 和 so
- System.out.println(dir.isDirectory());
- System.out.println(dir.isFile());
- System.out.println(dir.mkdirs());
- System.out.println(dir.isDirectory());
- System.out.println(dir.isFile());
- }
- }
-
-
-
-
- 运行结果
- false
- false
- true
- true
- false
- import java.io.File;
- import java.io.IOException;
- public class Main {
- public static void main(String[] args) throws IOException {
- File file = new File("some-file.txt"); // 要求 some-file.txt 得存在
- File dest = new File("dest.txt"); // 要求 dest.txt 不存在
- System.out.println(file.exists());
- System.out.println(dest.exists());
- System.out.println(file.renameTo(dest));
- System.out.println(file.exists());
- System.out.println(dest.exists());
- }
- }
-
-
-
-
-
- 运行结果
- true
- false
- true
- false
- true

说明:
InputStream 只是⼀个抽象类,要使⽤还需要具体的实现类。关于 InputStream 的实现类有很多,基本可以认为不同的输⼊设备都可以对应⼀个 InputStream 类,我们现在只关⼼从⽂件中读取,所以使⽤ FileInputStream

示例代码1:
- import java.io.*;
- // 需要先在项⽬⽬录下准备好⼀个 hello.txt 的⽂件,⾥⾯填充 "Hello" 的内容
- public class Main {
- public static void main(String[] args) throws IOException {
- try (InputStream is = new FileInputStream("hello.txt")) {
- while (true) {
- int b = is.read();
- if (b == -1) {
- // 代表⽂件已经全部读完
- break;
- }
-
- System.out.printf("%c", b);
- }
- }
- }
- }
- import java.io.*;
- // 需要先在项⽬⽬录下准备好⼀个 hello.txt 的⽂件,⾥⾯填充 "Hello" 的内容
- public class Main {
- public static void main(String[] args) throws IOException {
- try (InputStream is = new FileInputStream("hello.txt")) {
- byte[] buf = new byte[1024];
- int len;
-
- while (true) {
- len = is.read(buf);
- if (len == -1) {
- // 代表⽂件已经全部读完
- break;
- }
-
- for (int i = 0; i < len; i++) {
- System.out.printf("%c", buf[i]);
- }
- }
- }
- }
- }
示例代码二:
- import java.io.*;
- // 需要先在项⽬⽬录下准备好⼀个 hello.txt 的⽂件,⾥⾯填充 "你好中国" 的内容
- public class Main {
- public static void main(String[] args) throws IOException {
- try (InputStream is = new FileInputStream("hello.txt")) {
- byte[] buf = new byte[1024];
- int len;
- while (true) {
- len = is.read(buf);
- if (len == -1) {
- // 代表⽂件已经全部读完
- break;
- }
- // 每次使⽤ 3 字节进⾏ utf-8 解码,得到中⽂字符
- // 利⽤ String 中的构造⽅法完成
- // 这个⽅法了解下即可,不是通⽤的解决办法
- for (int i = 0; i < len; i += 3) {
- String s = new String(buf, i, 3, "UTF-8");
- System.out.printf("%s", s);
- }
- }
- }
- }
- }
示例代码如下:
- import java.io.*;
- import java.util.*;
- // 需要先在项⽬⽬录下准备好⼀个 hello.txt 的⽂件,⾥⾯填充 "你好中国" 的内容
- public class Main {
- public static void main(String[] args) throws IOException {
- try (InputStream is = new FileInputStream("hello.txt")) {
- try (Scanner scanner = new Scanner(is, "UTF-8")) {
- while (scanner.hasNext()) {
- String s = scanner.next();
- System.out.print(s);
- }
- }
- }
- }
- }


说明:
五个示例代码如下:
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- try (OutputStream os = new FileOutputStream("output.txt")) {
- os.write('H');
- os.write('e');
- os.write('l');
- os.write('l');
- os.write('o');
- // 不要忘记 flush
- os.flush();
- }
- }
- }
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- try (OutputStream os = new FileOutputStream("output.txt")) {
- byte[] b = new byte[] {
- (byte)'G', (byte)'o', (byte)'o', (byte)'d'
- };
- os.write(b);
-
- // 不要忘记 flush
- os.flush();
- }
- }
- }
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- try (OutputStream os = new FileOutputStream("output.txt")) {
- byte[] b = new byte[] {
- (byte)'G', (byte)'o', (byte)'o', (byte)'d', (byte)'B', (byte)'a'
- };
- os.write(b, 0, 4);
-
- // 不要忘记 flush
- os.flush();
- }
- }
- }
- 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();
- }
- }
- }
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- try (OutputStream os = new FileOutputStream("output.txt")) {
- String s = "你好中国";
- byte[] b = s.getBytes("utf-8");
- os.write(b);
-
- // 不要忘记 flush
- os.flush();
- }
- }
- }
- OutputStream os = ...;
- OutputStreamWriter osWriter = new OutputStreamWriter(os, "utf-8");
- PrintWriter writer = new PrintWriter(osWriter);
- // 接下来我们就可以⽅便的使⽤ writer 提供的各种⽅法了
- writer.print("Hello");
- writer.println("你好");
- writer.printf("%d: %s\n", 1, "没什么");
- // 不要忘记 flush
- writer.flush();
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- try (OutputStream os = new FileOutputStream("output.txt")) {
- try (OutputStreamWriter osWriter = new OutputStreamWriter(os, "UTF-8
- try (PrintWriter writer = new PrintWriter(osWriter)) {
- writer.println("我是第⼀⾏");
- writer.print("我的第⼆⾏\r\n");
- writer.printf("%d: 我的第三⾏\r\n", 1 + 1);
- writer.flush();
- }
- }
- }
- }
- }
这篇博客到这里就结束啦,希望可以给大家带来帮助~~