1. 程式人生 > >列印流、commons-IO

列印流、commons-IO

列印流
位元組列印流    PrintStream
字元列印流    PrintWriter
方法:
void print(String str): 輸出任意型別的資料,
void println(String str): 輸出任意型別的資料,自動寫入換行操作


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream; import java.io.PrintWriter; public class Demo02 { public static void main(String[] args) throws IOException { method1(); method2(); copy(); } //列印流永遠不會拋IO異常 只有輸出目的地 //位元組列印流寫入和續寫 public static void method1() throws FileNotFoundException{ FileOutputStream fos
=new FileOutputStream("E:\\java\\print.txt",true); PrintStream ps=new PrintStream(fos); ps.print(100);//原樣輸出 ps.println("嗨"); ps.println("嗨"); ps.close(); } //字元列印流 public static void method2() throws FileNotFoundException{ //建立自動重新整理的字元列印流 FileOutputStream fos=new
FileOutputStream("E:\\java\\print.txt"); PrintWriter pw=new PrintWriter(fos,true); pw.println("嗨"); pw.print("啊"); pw.close(); } //列印流複製檔案 public static void copy() throws IOException{ FileReader fr=new FileReader("E:\\java\\print.txt"); BufferedReader br=new BufferedReader(fr); FileWriter fw=new FileWriter("E:\\java\\a\\print.txt"); PrintWriter pw=new PrintWriter(fw,true); String line=""; while((line=br.readLine())!=null){ pw.println(line); } br.close(); pw.close(); } } commons-IO 匯入classpath 加入classpath的第三方jar包內的class檔案才能在專案中使用 建立lib資料夾 將commons-io.jar拷貝到lib資料夾 右鍵點選commons-io.jar,Build Path→Add to Build Path FilenameUtils 這個工具類是用來處理檔名(譯者注:包含檔案路徑)的,他可以輕鬆解決不同作業系統檔名稱規範不同的問題 常用方法: getExtension(String path):獲取檔案的副檔名; getName():獲取檔名; isExtension(String fileName,String ext):判斷fileName是否是ext字尾名; FileUtils 提供檔案操作(移動檔案,讀取檔案,檢查檔案是否存在等等)的方法。 常用方法: readFileToString(File file):讀取檔案內容,並返回一個String; writeStringToFile(File file,String content):將內容content寫入到file中; copyDirectoryToDirectory(File srcDir,File destDir);資料夾複製 copyFile(File srcFile,File destFile);檔案複製 import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; public class Demo03 { public static void main(String[] args) throws IOException { //method1(); //method2(); //method3(); method4(); } //處理檔名的工具 public static void method1(){ //獲取副檔名 String ext=FilenameUtils.getExtension("E:\\java\\demo.txt"); System.out.println(ext); //獲取檔名 String filename=FilenameUtils.getName("E:\\java\\demo.txt"); System.out.println(filename); //判斷是否是java檔案 boolean flag=FilenameUtils.isExtension("E:\\java\\demo.txt", "java"); System.out.println(flag); } public static void method2() throws IOException{ File file=new File("E:\\java\\demo.txt"); String content=FileUtils.readFileToString(file); System.out.println(content); } public static void method3() throws IOException{ File file=new File("E:\\java\\demo.txt"); FileUtils.write(file, "你好"); } public static void method4() throws IOException{ //複製資料夾 File file=new File("E:\\java"); File file1=new File("F:\\java"); FileUtils.copyDirectoryToDirectory(file, file1); } public static void method5() throws IOException{ //複製檔案 File file=new File("E:\\java\\print.txt"); File file1=new File("E:\\java\\e\\print.txt"); FileUtils.copyFile(file, file1); } }