1.public static List<String> readLines(File file, Charset charset): 將文件中的文字以BufferedReader.readline的方式讀取成一行行字符串 @Test public void testFiles() throws IOException { String fileName = "/Users/jiabei/Downloads/CLEARING.sql"; File file = new File(fileName); List<String> result = Files.readLines(file, Charsets.UTF_8); } 2.public static <T> T readLines(File file, Charset charset, LineProcessor<T> callback):讀取文件並按照自定義回調方法處理讀取的行數據 @Test public void testFiles() throws IOException { String fileName ="/Users/jiabei/Downloads/CLEARING.sql"; File file = new File(fileName); List<Source> list = Files.readLines(file, Charsets.UTF_8, new LineProcessor<List<Source>>() { private ArrayList<Source> list = Lists.newArrayList(); @Override public boolean processLine(String s) throws IOException { if(s.contains("/*")){ Source source = new Source(); source.setName(s); list.add(source); } return true; } @Override public List<Source> getResult() { return this.list; } }); for (Source source : list) { System.out.println(source.getName()); } } 3.public static void copy(File from, File to):文件複製 @Test public void testFiles() throws IOException { String fileName ="/Users/jiabei/Downloads/CLEARING.sql"; File file = new File(fileName); Files.copy(file,new File("/Users/jiabei/Downloads/to.sql")); } 4.public static void move(File from, File to):移動文件到目標路徑 @Test public void testFiles() throws IOException { String fileName = "/Users/jiabei/Downloads/CLEARING.sql"; File file = new File(fileName); Files.move(file, new File("/Users/jiabei/Downloads/to.sql")); } 5.public static ByteSource asByteSource(File file):將file轉換爲字節數組,兩種方式 @Test public void testFiles() throws IOException { String fileName ="/Users/jiabei/Downloads/CLEARING.sql"; File file = new File(fileName); ByteSource byteSource = Files.asByteSource(file); byte[] read = byteSource.read(); String string = new java.lang.String(read); System.out.println(string); }