天天看点

文件读写(JDK7)

以下我是在”/Users/zhanqian/demo”的路径下进行的文件操作。

package main;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Properties;

/**
* @author jeff.zhan
* 2016年11月6日 上午10:46:14
*
*/

public class FileManagement {

    public static void main(String[] args) throws IOException {
        Properties props = System.getProperties();
        System.out.println(props.getProperty("user.dir"));/// Users/zhanqian/Documents/workspace/java-core
        Path relative = Paths.get("relative", "child");
        System.out.println(relative);// relative/child
        Path absolute = Paths.get("/Users", "zhanqian", "demo");
        System.out.println(absolute);// /Users/zhanqian/demo
        System.out.println(absolute.getParent());// /Users/zhanqian
        System.out.println(absolute.resolve("work"));// /Users/zhanqian/demo/work
        System.out.println(absolute.resolve("work").resolveSibling("brother_work"));// /Users/zhanqian/demo/brother_work

        byte[] bytes = Files.readAllBytes(Paths.get("/Users", "zhanqian", "demo", "test.txt"));
        String content = new String(bytes, "utf-8");
        // new line
        // append new line
        System.out.println(content);

        List<String> lines =
            Files.readAllLines(Paths.get("/Users", "zhanqian", "demo", "test.txt"), Charset.forName("iso-8859-1"));
        for (String line : lines) {
            // new line
            // append new line
            System.out.println(line);
        }

        Files.write(Paths.get("/Users", "zhanqian", "demo", "test.txt"), "new line".getBytes());
        Files.write(Paths.get("/Users", "zhanqian", "demo", "test.txt"), "\nappend new line".getBytes(),
            StandardOpenOption.APPEND);

        Files.copy(Paths.get("/Users", "zhanqian", "demo", "test.txt"),
            Paths.get("/Users", "zhanqian", "Project", "test2.txt"));

        Files.delete(Paths.get("/Users", "zhanqian", "Project", "test2.txt"));
        boolean deleted = Files.deleteIfExists(Paths.get("/Users", "zhanqian", "Project", "test2.txt"));

        // 返回文件字节数(java 英文字母是一个byte,而汉字是两个byte。网上另种说法是unicode字符集都是两个字节存储,所以都是两个字节,本地环境已经验证这样的说法是不对的,
        // 我觉得合理的解释是依据本地的编码方式而决定的。unicode只是字符集,映射规则还是看本地编码决定的。)
        System.out.println(Files.size(Paths.get("/Users", "zhanqian", "demo", "test.txt")));// 24
        System.out.println("new line append new line".getBytes().length);// 24
        System.out.println("n".getBytes().length); // 1
    }
}
           

可以看到jdk7的Paths类和Files(readAllBytes(),write())大大简化了对文件的操作。但如果文件较大或则是二进制文件,则还是需要用我们所熟知的读入器/写出器。

InputStream in = Files.newInputStream(Paths.get("/Users", "zhanqian", "demo", "test.txt"));
        OutputStream out = Files.newOutputStream(Paths.get("/Users", "zhanqian", "demo", "test.txt"));
        Reader in2 = Files.newBufferedReader(Paths.get("/Users", "zhanqian", "demo", "test.txt")) ;
        Writer out2 = Files.newBufferedWriter(Paths.get("/Users", "zhanqian", "demo", "test.txt"));