2016-02-19 36 views
2

我已經編寫了一個文件內容複製到另一個文件的代碼。但我無法將第二個文件內容複製到第三個文件。 爲我寫下面的代碼:我想將2個文件的內容複製到第三個文件中

try { 
     File infile = new File("d:\\vijay.txt"); 
     File outfile = new File("d:\\ajay.txt"); 

     FileInputStream instream = new FileInputStream(infile); 
     FileOutputStream outstream = new FileOutputStream(outfile); 

     byte[] buffer = new byte[1024]; 

     int length; 

     while ((length = instream.read(buffer)) > 0) { 
      outstream.write(buffer, 0, length); 

     } 
     instream.close(); 
     outstream.close(); 

     System.out.println("File Copied successfully"); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 

請幫助我,在此先感謝。

+0

問題是什麼?如果你能做到這一點,你可以輕而易舉地做到兩件事。順便說一句你應該修復你的格式,我建議使用你的IDE。 –

+0

您通常看不到以同一方式同時寫入多個文件,因爲通常通過多次調用不同輸出流來處理該方法。換句話說,一旦你有一種方法可以將1個文件複製到另一個文件中,你就有一種方法可以複製1次文件多次,或者將許多文件複製到一個文件或其任意組合。 – Neil

回答

-1

你可以試試這個:

try { 
      File infile = new File("/home/bobo/test/a.txt"); 
      File infile1 = new File("/home/bobo/test/b.txt"); 

      //The third file 
      File outfile = new File("/home/bobo/test/c.txt"); 

      FileInputStream instream = new FileInputStream(infile); 
      FileInputStream instream1 = new FileInputStream(infile1); 

      FileOutputStream outstream = new FileOutputStream(outfile); 

      byte[] buffer = new byte[1024]; 

      int length; 

      while ((length = instream.read(buffer)) > 0) { 
       outstream.write(buffer, 0, length); 
      } 

      while ((length = instream1.read(buffer)) > 0) { 
       outstream.write(buffer, 0, length); 
      } 

      instream.close(); 
      instream1.close(); 
      outstream.close(); 

      System.out.println("File Copied successfully"); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
1

做它,如下:

try { 
    // the files to be copied 
    String[] filePaths = {"file1.txt", "file2.txt"}; 
    // out file 
    File outfile = new File("d:\\ajay.txt"); 
    FileOutputStream outstream = new FileOutputStream(outfile); 

    // loop to all files copied 
    for (String filePath : filePaths) { 
     FileInputStream instream = new FileInputStream(new File(filePath)); 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = instream.read(buffer)) > 0) { 
      outstream.write(buffer, 0, length); 
     } 
     // close each file on copy finished 
     instream.close(); 
    } 
    // at the end close the output stream 
    outstream.close(); 
    System.out.println("File Copied successfully"); 
} catch (IOException ioe) { 
    ioe.printStackTrace(); 
} 

現在,您可以將文件複製N到一個文件中。

-1

只是試試看

public class Filescombining 
{ 
     public static void main(String[] args) throws IOException 
     {  
     ArrayList<String> list = new ArrayList<String>(); 
     try 
     { 
     BufferedReader br = new BufferedReader(new FileReader("input1.txt")); 
     BufferedReader r = new BufferedReader(new FileReader("input2.txt")); 
      String s1 =null; 
      String s2 = null; 

         while ((s1 = br.readLine()) != null) 
         {       
             list.add(s1);   
         } 
         while((s2 = r.readLine()) != null) 
         {  
             list.add(s2);  
         } 
     } 
     catch (IOException e) 
      { 
      e.printStackTrace(); 
      } 

      BufferedWriter writer=null; 
      writer = new BufferedWriter(new FileWriter("output.txt")); 
      String listWord;    
        for (int i = 0; i< list.size(); i++) 
        { 
         listWord = list.get(i); 
         writer.write(listWord); 
         writer.write("\n"); 
        } 
          System.out.println("completed"); 
          writer.close();  
     } 
    } 

希望我的幫助,工作快樂編碼

+1

您是否創建了一個隨機數生成器來縮進您的代碼?你怎麼能這樣工作? – Neil

+0

它在第一個文件完成複製後追加 – SmashCode

1

如果你有Java 7的,我會建議你使用Files utils的。例如:

Path source1 = Paths.get("src1.txt"); 
Path source2 = Paths.get("src2.txt"); 

Path destination = Paths.get("dest.txt");  
out = Files.newOutputStream(destination, CREATE, APPEND); 

Files.copy(source1, destination, StandardCopyOption.REPLACE_EXISTING); 
Files.copy(source2, destination); 
相關問題