以下實例示範了使用BufferedWriter 類別的read 和write 方法將檔案內容複製到另一個檔案:
/* author by w3cschool.cc Main.java */import java.io.*;public class Main { public static void main(String[] args) throws Exception { BufferedWriter out1 = new BufferedWriter (new FileWriter("srcfile")); out1.write("string to be copied\n"); out1.close(); InputStream in = new FileInputStream (new File("srcfile")); OutputStream out = new FileOutputStream (new File("destnfile")); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); BufferedReader in1 = new BufferedReader (new FileReader("destnfile")); String str; while ((str = in1.readLine()) != null) { System.out.println(str); } in.close(); }}
以上程式碼執行輸出結果為:
string to be copied
以上就是Java 實例- 將檔案內容複製到另一個檔案的內容,更多相關內容請關注PHP中文網(www.php.cn)!