2012-10-25 59 views
8

Tôi có 4 file khác nhau ở một số nơi như: D: \ 1.txt D: \ 2.txt D: \ 3.txt và D: \ 4.txtPhụ thêm nhiều file vào một

Tôi cần tạo một tệp mới là NewFile.txt, Nó phải chứa tất cả nội dung có trong các tệp ở trên 1.txt, 2.txt, 3.txt 4.txt .......

Tất cả Dữ liệu sẽ có trong tệp Đơn Mới (NewFile.txt) ..

Hãy gợi ý cho tôi một số ý tưởng để làm ame trong java hay Groovy ....

Trả lời

9

Dưới đây là một cách để làm điều đó trong 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' 
    } 
    } 
} 

Ưu điểm của việc thực hiện nó theo cách này (trên gọi getText trên mỗi file nguồn) là nó sẽ không cần tải toàn bộ tệp vào bộ nhớ trước khi ghi nội dung của nó ra newfile. Nếu một trong các tệp của bạn to lớn, phương pháp khác có thể không thành công.

+0

'File' định nghĩa '# leftShift' là tốt, vì vậy bạn cũng có thể làm 'src.inject (new File (dst)) {out, it -> new File (nó) .withInputStream {out << it}}'. Tôi tin rằng sẽ mở và đóng tệp đích cho từng tệp nguồn, có thể là một vấn đề nếu bạn đang xử lý nhiều tệp nhỏ. –

2

Đây là trong groovy

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()) } 
0

tôi đang hiển thị cho bạn cách thức mà nó phải được thực hiện trong 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; 
    } 
} 

cho tôi biết nếu bạn có bất kỳ nghi ngờ

+0

Tại sao bạn bắt ngoại lệ, sau đó ném nó lại như một ngoại lệ mới (tổng quát hơn)? Ngoài ra, nếu một ngoại lệ được ném, điều này có thể không cho phép xử lý tệp mở (nếu mã được lấy và đưa vào một hàm tổng quát) không? –

+0

thực sự đây là một phần của một dự án lớn hơn mà tôi đã thực hiện. tôi đã viết một chương trình mới lấy các phần từ mô-đun đã thực hiện phần cụ thể này.Trong dự án thực tế của tôi chức năng chính này là một hàm riêng biệt được gọi bên trong một hàm khác. –

0

Bạn có thể làm một cái gì đó như thế này trong Java. Hy vọng nó giúp bạn giải quyết vấn đề của bạn:

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); 

    } 
} 
0

Một lót dụ:

def out = new File(".all_profiles") 
['.bash_profile', '.bashrc', '.zshrc'].each {out << new File(it).text} 

HOẶC

['.bash_profile', '.bashrc', '.zshrc'].collect{new File(it)}.each{out << it.text} 

thực hiện của Tim là tốt hơn nếu bạn có các tập tin lớn.

1

tôi đã cố gắng giải quyết này và tôi thấy nó khá dễ dàng nếu bạn sao chép nội dung vào một mảng và viết mảng vào một tập tin khác nhau

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); 
    } 
} 
} 
Các vấn đề liên quan