我在像一些地點4個不同的文件: d:\的1.txt d:\ 2.txt d:\ 3.txt和 d:\ 4.txt追加多個文件到一個
我需要創建一個新文件爲NewFile.txt,它應該包含上述文件中存在的所有內容1.txt,2.txt,3.txt 4.txt .......
所有數據都應該出現在New Single文件中(NewFile.txt)..
請給我建議一些做法AME在Java或Groovy ....
我在像一些地點4個不同的文件: d:\的1.txt d:\ 2.txt d:\ 3.txt和 d:\ 4.txt追加多個文件到一個
我需要創建一個新文件爲NewFile.txt,它應該包含上述文件中存在的所有內容1.txt,2.txt,3.txt 4.txt .......
所有數據都應該出現在New Single文件中(NewFile.txt)..
請給我建議一些做法AME在Java或Groovy ....
這裏是做在Groovy的一種方式:
// Get a writer to your new file
new File('/tmp/newfile.txt').withWriter { w ->
// For each input file path
['/tmp/1.txt', '/tmp/2.txt', '/tmp/3.txt'].each { f ->
// Get a reader for the input file
new File(f).withReader { r ->
// And write data from the input into the output
w << r << '\n'
}
}
}
做這種方式(通過調用每個源文件的getText
)的優點是它在將其內容寫入newfile
之前,不需要將整個文件加載到內存中。如果你的一個文件很龐大,另一種方法可能會失敗。
這是常規
def allContentFile = new File("D:/NewFile.txt")
def fileLocations = ['D:/1.txt' , 'D:/2.txt' , 'D:/3.txt' , 'D:/4.txt']
fileLocations.each{ allContentFile.append(new File(it).getText()) }
我顯示你是用Java做的方式:
public class Readdfiles {
public static void main(String args[]) throws Exception
{
String []filename={"C:\\WORK_Saurabh\\1.txt","C:\\WORK_Saurabh\\2.txt"};
File file=new File("C:\\WORK_Saurabh\\new.txt");
FileWriter output=new FileWriter(file);
try
{
for(int i=0;i<filename.length;i++)
{
BufferedReader objBufferedReader = new BufferedReader(new FileReader(getDictionaryFilePath(filename[i])));
String line;
while ((line = objBufferedReader.readLine())!=null)
{
line=line.replace(" ","");
output.write(line);
}
objBufferedReader.close();
}
output.close();
}
catch (Exception e)
{
throw new Exception (e);
}
}
public static String getDictionaryFilePath(String filename) throws Exception
{
String dictionaryFolderPath = null;
File configFolder = new File(filename);
try
{
dictionaryFolderPath = configFolder.getAbsolutePath();
}
catch (Exception e)
{
throw new Exception (e);
}
return dictionaryFolderPath;
}
}
告訴我,如果您有任何疑問
爲什麼要捕捉異常,然後再將它作爲新的(更一般化的)異常進行包裝?另外,如果拋出一個異常,那麼這是不是可以打開文件句柄(如果代碼被取出並放入一個泛化函數)? –
實際上這是我實施的一個更大項目的一部分。我寫了一個新的程序,從部分模塊中完成了這個特定的部分。在我的實際項目中,這個主要功能是一個獨立的函數,在另一個函數中被調用。 –
你可以做像Java這樣的東西。希望它可以幫助你解決你的問題:
import java.io.*;
class FileRead {
public void readFile(String[] args) {
for (String textfile : args) {
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(textfile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
// Write to the new file
FileWriter filestream = new FileWriter("Combination.txt",true);
BufferedWriter out = new BufferedWriter(filestream);
out.write(strLine);
//Close the output stream
out.close();
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
public static void main(String args[]) {
FileRead myReader = new FileRead();
String fileArray[] = {"file1.txt", "file2.txt", "file3.txt", "file4.txt"};
myReader.readFile(fileArray);
}
}
一個襯墊例如:
def out = new File(".all_profiles")
['.bash_profile', '.bashrc', '.zshrc'].each {out << new File(it).text}
OR
['.bash_profile', '.bashrc', '.zshrc'].collect{new File(it)}.each{out << it.text}
蒂姆的實現是更好,如果你有大的文件。
我試圖解決這個,我發現它很容易,如果你的內容複製到陣列和陣列寫入不同的文件
public class Fileread
{
public static File read(File f,File f1) throws FileNotFoundException
{
File file3=new File("C:\\New folder\\file3.txt");
PrintWriter output=new PrintWriter(file3);
ArrayList arr=new ArrayList();
Scanner sc=new Scanner(f);
Scanner sc1=new Scanner(f1);
while(sc.hasNext())
{
arr.add(sc.next());
}
while(sc1.hasNext())
{
arr.add(sc1.next());
}
output.print(arr);
output.close();
return file3;
}
/**
*
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) {
try
{
File file1=new File("C:\\New folder\\file1.txt");
File file2=new File("C:\\New folder\\file2.txt");
File file3=read(file1,file2);
Scanner sc=new Scanner(file3);
while(sc.hasNext())
System.out.print(sc.next());
}
catch(Exception e)
{
System.out.printf("Error :%s",e);
}
}
}
'File'定義'#leftShift'一樣,所以你也可以執行'src.inject(new File(dst)){out,it - > new File(it).withInputStream {out << it}}'。我相信會打開和關閉每個源文件的目標文件,如果您處理很多小文件,這可能會成爲問題。 –