2016-08-20 52 views
2

我的硬盤上有好幾個文件需要放在一起作爲一個文件寫回硬盤。這些文件總共超過2GB,因此在我使用它們時會產生以下錯誤:「OutOfMemoryException:內存不足。」我認爲我的代碼太簡單了。任何人有任何其他想法?將大文件合併在一起

i=0;bt=new byte[0]; 
while(i>-1){ 
    if(i<10){txt="0"+i;}else{txt=""+i;} 
    txt=dir+"/"+dstring+"/"+"part"+txt+".tdd"; 
    if(File.Exists(txt)){ 
     bt=bt+File.ReadAllBytes(txt);// <--error is here 
     i++;} 
     else{i=-1;} 
} 
print("saving"+bt.Length); 
File.WriteAllBytes(dir+"/"+dstring+"/"+dstring+".mp4",bt); 
+0

這是C#代碼嗎? – dorukayhan

+0

它是javascript爲unity3d編寫的。 – TaterKing

+0

Unity是一款遊戲引擎。不是文件合併引擎...這個怎麼樣? http://stackoverflow.com/questions/4475855/easiest-scripting-method-to-merge-two-text-files-ruby-python-javascript-jav – 2016-08-20 17:16:46

回答

0

您正嘗試將所有文​​件保存在RAM中。由於文件的總大小很大(正如你在你的問題中所說的),你應該這樣做:

  • 步驟1)在所需的位置創建一個空文件。
  • 步驟2)將每個文件的內容寫入在步驟1中創建的文件ONE BY ONE

有沒有辦法過分強調一個事實,即你MUST寫文件一個接一個,而不是一下子。

+0

請注意:[Markdown在編輯摘要中不起作用。 ](http://stackoverflow.com/revisions/39057148/2) – dorukayhan

0

Darukayan是正確的。另一個論壇上的人給了我一個代碼示例來解決我的問題。這是我最終使用的:

function combine(){ 

//----finds an indefinate amount of files named part00.ttd, part01.tdd,part03.tdd 
//----in a folder and combines them into one file 

     var outStream = System.IO.File.OpenWrite(dir+"/"+dstring+"/"+dstring+".mp4"); 

     i=0; 
     while(i>-1){ 

     if(i<10){txt="0"+i;}else{txt=""+i;} 

     txt=dir+"/"+dstring+"/"+"part"+txt+".tdd"; 

     if(File.Exists(txt)){ 

      var inStream = System.IO.File.OpenRead(txt);  
      var fileinfo = new System.IO.FileInfo(txt); 
      var countread:int = fileinfo.Length; 

      var filebytes:byte[]=new byte[countread]; 

       inStream.Read(filebytes, 0, countread); 
       outStream.Write(filebytes, 0, countread); 
       inStream.Close(); 

      i++;}else{i=-1;} 

     } 

     outStream.Close(); 
}