不多說,直接上干貨!
有時候,我們需要用到這樣的一個場景。

ReadLocalFile1WriteLocalFile2.java
(以下是相當於復制,讀取文件1里的全部內容,並寫入到文件2里)
package zhouls.bigdata.DataFeatureSelection.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; public class ReadLocalFile1WriteLocalFile2 { /** * 從本地文件1.txt讀取數據寫入本地文件2.txt * * @author zhouls * */ public static void main(String[] args) throws IOException { //輸入流 InputStream in = new FileInputStream("F:/datamode/SnortFeatureSelectionData.txt"); //輸出流 OutputStream out = new FileOutputStream("F:/datamode/SnortFeatureSelectionData2.txt", true); try { byte[] buffer = new byte[1024]; while (true) { int byteRead = in.read(buffer); if (byteRead == -1) break; out.write(buffer, 0, byteRead); } } catch (MalformedURLException ex) { System.err.println(args[0] + " is not a URL Java understands."); } finally { if (in != null) in.close(); if (out != null) { out.close(); } } } }
得到

