不多说,直接上干货!
有时候,我们需要用到这样的一个场景。
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(); } } } }
得到