2015-04-23 75 views
0

我正在創建一個程序,旨在採取n拆分數量並將文件拆分爲該數量的子文件。在我的SplitFile.java中,我正在讀取一個文件,傳遞一個子字符串數組,以顯示每個拆分文件應該執行的文本。然後,我將字符串轉換爲一個字節數組,並將字節數組寫入分割文件,但是我創建的每個文件都只是輸出只是略有不同。字節陣列沒有打印到文件正確

SplitFile.java

import java.io.*; 
import java.nio.charset.Charset; 
import java.util.ArrayList; 

public class SplitFile 
{ 
    int numberOfSplits = 1; 
    File file; 
    String[] parts = new String[5]; 

    public SplitFile(File file,int numberOfSplits) 
    { 
     this.file = file; 
     this.numberOfSplits = numberOfSplits; 
    } 

    public void FileSplitter() throws IOException 
    { 
     FileInputStream fis = new FileInputStream(file); 
     String fileText = readFile(); 

     if(numberOfSplits == 2) 
     { 
      int mid = fileText.length()/2; 

      parts[0] = fileText.substring(0, mid); 
      parts[1] = fileText.substring(mid); 
     } 
     else if(numberOfSplits == 3) 
     { 
      int third = fileText.length()/3; 
      int secondThird = third + third; 

      parts[0] = fileText.substring(0, third); 
      parts[1] = fileText.substring(third, secondThird); 
      parts[2] = fileText.substring(secondThird); 
     } 

     for(int i = 1; i <= numberOfSplits; i++) 
     { 
      BufferedInputStream bis = new BufferedInputStream(new fileInputStream(file)); 
      FileOutputStream out; 
      String name = file.getName(); 

      byte[] b = parts[i - 1].getBytes(Charset.forName("UTF-8")); 
      int temp = 0; 

      while((temp = bis.read(b)) > 0); 
      { 
       File newFile = new File(name + " " + i + ".txt"); 
       newFile.createNewFile(); 
       out = new FileOutputStream(newFile); 
       out.write(b, 0, temp); // Writes to the file 
       out.close(); 
       temp = 0; 
      } 

     } 

    } 

    public String readFile() throws IOException 
    { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 

     try 
     { 
      StringBuilder sb = new StringBuilder(); 
      String line = br.readLine(); 

      while (line != null) 
      { 
       sb.append(line); 
       sb.append("\n"); 
       line = br.readLine(); 
      } 

      return sb.toString(); 
     } 
     finally 
     { 
      br.close(); 
     } 
    } 
} 

如果我在第2傳,因爲我想,這是不對的,在中間分裂它的分裂適量,用文件1是上半年和文件2的存在下半部分,而是給這兩個文件的文本文件的結尾。我的問題似乎是在這裏:

while((temp = bis.read(b)) > 0); 
{ 
    File newFile = new File(name + " " + i + ".txt"); 
    newFile.createNewFile(); 
    out = new FileOutputStream(newFile); 
    out.write(b, 0, temp); // Writes to the file 
    out.close(); 
    temp = 0; 
} 

我會在這裏使用的示例文件是這樣的文件:

MYFILE.TXT

ABCDEFGHIJKLMNOPQRSTUVWXYZ

它分成兩個文件,如下所示:

MYFILE.TXT 1

nopqrstuvqxyz

MYFILE.TXT 2

opqrstuvqxyz

的問題是什麼你知道嗎?

回答

1
  1. 在你的代碼中,你在whilte循環中定義了File newFile = new File(name + " " + i + ".txt");out = new FileOutputStream(newFile);,這是不正確的。
  2. while((temp = bis.read(b)) > 0);這裏沒有分號= =」
  3. 在你的代碼 很多錯誤,我會改變像你的代碼:

     File newFile = new File(name + " " + i + ".txt"); 
         newFile.createNewFile(); 
         out = new FileOutputStream(newFile); 
         out.write(b); // Writes to the file 
         out.flush(); 
         out.close(); 
    

如果您需要您的代碼運行,只要你想,在這裏你是

 for (int i = 1; i <= numberOfSplits; i++) { 
      String name = file.getName(); 
      byte[] b = parts[i - 1].getBytes(Charset.forName("UTF-8")); 
      ByteArrayInputStream bis = new ByteArrayInputStream(b); 

      int temp = 0; 
      File newFile = new File(name + " " + i + ".txt"); 
      newFile.createNewFile(); 
      FileOutputStream out = new FileOutputStream(newFile);      
      while ((temp = bis.read()) > 0) 
      { 
       out.write(temp); // Writes to the file 
      } 
      out.flush(); 
      out.close(); 
     } 
+0

感謝您的編輯,但哇...我試過的東西,我甚至沒有考慮這兩行是我的概率最大的罪魁禍首LEM。非常感謝您的幫助。 – Midge