2016-07-08 14 views
5

Sự cố và vị trí của tôi: Tôi không thể nối văn bản vào các tệp mới này mà tôi tạo bằng chương trình. Hiện tại, nó chỉ sao chép các tệp nhưng không sao chép chúng. Xem dòng có chú thích "// append file name into the new file ".Văn bản không được thêm vào tệp mới khi cố gắng tạo một trình xử lý tệp văn bản

Thứ hai, tệp kết xuất cuối cùng dường như chỉ nối thêm tệp .java, nó không đọc hoặc chắp thêm tệp đầu vào.

Lời giải thích về những gì tôi đang cố gắng để làm:

Các dài và ngắn là tôi đang cố gắng để thực hiện một chương trình mà sẽ được đặt vào thư mục ngẫu nhiên với các file .txt chứa đầy dữ liệu.

tôi cần những chương trình để

  • Look trong lĩnh vực của thư mục đó là chỉ trong
  • Sau đó lấy bất kỳ tập tin .txt và
    a) tạo một bản sao nhưng thêm tên tập tin gốc vào nội dung văn bản (ở trên cùng), bên trong thư mục con như "< filenamehere.txt" vào phần thân (ở trên cùng)

    b) sau đó sao chép nội dung của tệp gốc.txt c) lấy tệp và ứng dụng đã được thêm trước .txt kết thúc nó vào một tập tin dump.txt đơn d) lặp lại điều này cho tất cả các file txt địa phương và giữ phụ thêm vào cuối của tập tin dump.txt

Vì vậy, ở cuối, nếu tôi đã có 7 tác phẩm, tôi sẽ có 7 bản sao được thêm vào và 1 tệp kết xuất khổng lồ chứa tất cả mọi thứ của 7 bản sao được thêm vào. Ví dụ: nếu tôi có ba tệp văn bản, mỗi tệp chỉ có một từ trong đó. Vì vậy, a.txt, b.txt, c.txt và ba từ là "Xin chào thế giới!". Bản sao a.txt sẽ có các nội dung bên trong

"> a.txt

Xin chào

"

Ngay bây giờ nó chỉ cần sao chép Hello (nội dung cơ bản gốc) nhưng không phải phụ thêm> a.txt. Tệp văn bản kết xuất cuối cùng không tích lũy bất kỳ thứ gì từ các tệp khác, nhưng nó đủ kỳ lạ để chọn mã nguồn từ tệp .java. Vì vậy, về cơ bản, tôi nhận được một // Thư mục đầu ra và bên trong là các bản sao của các tệp .txt và megaDump.txt quản lý để nhận văn bản .java, nhưng không có tệp văn bản nào khác được thêm vào.

import java.io.* ; 
import java.nio.file.*; 

public class FilePrepender // class name 
{ 
    public static void main (String [] args) 
    { 
     // make a giant dump file which we will append all read files into 
     try { 
      new File("Output\\").mkdirs(); 
      File megaDumpFile = new File("Output\\masterDump.txt"); 

      if (megaDumpFile.createNewFile()) { 
       System.out.println("File creation success"); 
      } else { 
       System.out.println("File was not made. File already exists. Please delete"); 
      } 

     } catch (IOException e) { 

     } 

     //grab file names 
     File folder = new File("."); 
     File[] listOfFiles = folder.listFiles(); 
     for (int i = 0; i < listOfFiles.length; i++) { 
      if (listOfFiles[i].isFile()) { 
       listOfFiles[i].getName(); 
      } else if (listOfFiles[i].isDirectory()) { 
       //do nothing 
      } 
     } 

     //open files + duplicate + prepend + and append product to end of master dump file 
     // main for 
     for (int j = 0; j < listOfFiles.length; j++){ 
      //append file name for mega dump file 
      String fileNameTemp = listOfFiles[j].getName(); // get file name 
      try { 
       PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Output//masterDump.txt", true))); 
       out.println(fileNameTemp); 
       out.flush(); 
       out.close(); 
      } 
      catch (IOException e) { 

      } 

      // duplicate input files 
      FileInputStream instream = null; 
      FileOutputStream outstream = null; 

      try { 
       File infile =new File(""+listOfFiles[j].getName()); 
       File outfile =new File("Output\\"+listOfFiles[j].getName()); 

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

       byte[] buffer = new byte[1024]; 

       int length; 

       // apend file name into the new file 
       // String fileNameTemp = listOfFiles[j].getName(); // get file name 

       try { 
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Output//masterDump.txt", true))); 
        out.println(">"+fileNameTemp); 
        out.flush(); 
        out.close(); 
       } 
       catch (IOException e) { 

       } 


       // now copy contents of previous file into the new file 

       /*copying the contents from input stream to 
       * output stream using read and write methods 
       */ 
       while ((length = instream.read(buffer)) > 0){ 
        outstream.write(buffer, 0, length); 
       } 
       //Closing the input/output file streams 
       instream.close(); 
       outstream.close(); 

       // file is copied 
      } catch(IOException ioe) { 

      } 

      // copy newly copied file into mega dump 
      try { 
       File infile =new File("Output\\"+listOfFiles[j]); // newly copied 
       File outfile =new File("Output\\masterDump.txt"); 

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

       byte[] buffer = new byte[1024]; 

       int length; 
       /*copying the contents from input stream to 
       * output stream using read and write methods 
       */ 
       while ((length = instream.read(buffer)) > 0){ 
        outstream.write(buffer, 0, length); 
       } 

       //Closing the input/output file streams 
       instream.close(); 
       outstream.close(); 

       // file is copied 

      } catch(IOException ioe) { 

      } 

     } // end for loop 
    } // end main 
} // end class 
+0

Bạn liên tục bắt và bỏ qua 'IOException'. Vì vậy, bạn có thể có một số ngoại lệ trong chương trình của bạn mà bạn không biết. Triển khai ghi nhật ký lỗi thích hợp trước – Nikem

Trả lời

2

Có khá một vài vấn đề ở đây:

  • Bạn sử dụng các đường dẫn tập tin của bạn đôi khi dấu gạch chéo, đôi khi 2 backslashes và đôi khi thậm chí dấu gạch chéo kép mà dẫn đến những vấn đề ít nhất là trên máy Mac của tôi. Chỉ cần sử dụng các dấu gạch chéo thường xuyên.

  • Mã chưa lọc cho tệp .txt, vì vậy mọi thứ trong thư mục hiện tại đã được xử lý - ngay cả chính chương trình thực thi.

  • Hiện tại mã đã viết các dòng > sometext.txt trực tiếp vào masterDump.txt thay vì gián tiếp thông qua các bản sao tệp của bạn.

  • Mã được ghi đè masterDump.txt cho mỗi lần lặp của vòng lặp khi tệp không được mở trong chế độ phụ thêm.

Sau đây là mã hiện đang tạo kết quả sau khi được gọi trong thư mục có a.txt, b.txt và c.txt chứa "Hello", "World" và "!" tương ứng. Tôi hy vọng đây là hành vi mong muốn.
Lưu ý rằng có nhiều cải tiến trong mã này, đặc biệt là xử lý các lỗi như đã được chỉ ra trong các nhận xét.

Result

import java.io.* ; 
import java.nio.file.*; 

public class FilePrepender // class name 
{ 
    public static void main (String [] args) 
    { 
     // make a giant dump file which we will append all read files into 
     try { 
      new File("Output/").mkdirs(); 
      File megaDumpFile = new File("Output/masterDump.txt"); 

      if (megaDumpFile.createNewFile()) { 
       System.out.println("File creation success"); 
      } else { 
       System.out.println("File was not made. File already exists. Please delete"); 
      } 

     } catch (IOException e) { 

     } 

     //grab file names 
     File folder = new File("."); 
     File[] listOfFiles = folder.listFiles(); 
     for (int i = 0; i < listOfFiles.length; i++) { 
      if (listOfFiles[i].isFile()) { 
       listOfFiles[i].getName(); 
      } else if (listOfFiles[i].isDirectory()) { 
       //do nothing 
      } 
     } 

     //open files + duplicate + prepend + and append product to end of master dump file 
     // main for 
     for (int j = 0; j < listOfFiles.length; j++){ 
      //append file name for mega dump file 
      String fileNameTemp = listOfFiles[j].getName(); // get file name 
      if (!fileNameTemp.toLowerCase().endsWith(".txt")) { 
       continue; 
      } 

      // duplicate input files 
      FileInputStream instream = null; 
      FileOutputStream outstream = null; 

      try { 
       File infile =new File(""+listOfFiles[j].getName()); 
       File outfile =new File("Output/"+listOfFiles[j].getName()); 

       instream = new FileInputStream(infile); 


       byte[] buffer = new byte[1024]; 

       int length; 

       // apend file name into the new file 
       // String fileNameTemp = listOfFiles[j].getName(); // get file name 
       outstream = new FileOutputStream(outfile); 
       PrintWriter out = new PrintWriter(outstream); 
       out.println(">"+fileNameTemp); 
       out.flush(); 
       out.close(); 

       // now copy contents of previous file into the new file 

       /*copying the contents from input stream to 
       * output stream using read and write methods 
       */ 
       outstream = new FileOutputStream(outfile, true); 
       while ((length = instream.read(buffer)) > 0){ 
        outstream.write(buffer, 0, length); 
       } 
       //Closing the input/output file streams 
       instream.close(); 
       outstream.close(); 

       // file is copied 
      } catch(IOException ioe) { 

      } 

      // copy newly copied file into mega dump 
      try { 
       File infile =new File("Output/"+listOfFiles[j]); // newly copied 
       File outfile =new File("Output/masterDump.txt"); 

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

       byte[] buffer = new byte[1024]; 

       int length; 
       /*copying the contents from input stream to 
       * output stream using read and write methods 
       */ 
       while ((length = instream.read(buffer)) > 0){ 
        outstream.write(buffer, 0, length); 
       } 

       //Closing the input/output file streams 
       instream.close(); 
       outstream.close(); 

       // file is copied 

      } catch(IOException ioe) { 

      } 

     } // end for loop 
    } // end main 
} // end class 
1

Đồng ý với những người khác: Bạn xóa tiến bộ của bạn mỗi khi bạn chạm vào 'tập tin masterDump của bạn. Đây là phiên bản của tôi:

import java.io.* ; 
import java.nio.file.*; 

public class FilePrepender // class name 
{ 
    public static void main (String [] args) 
    { 
     //Generates the string for the output for the right PC. 
     String OUTFILE="Output"+File.separator+"masterDump.txt"; 

     // make a giant dump file which we will append all read files into 
     try { 
      new File("Output").mkdirs(); 
      File megaDumpFile = new File(OUTFILE); 
      megaDumpFile.createNewFile(); 

     } catch (IOException e) { 
      System.out.println("Something weird occured!"); 
     } 

     File folder = new File("."); 
//  FileFilter filter = new istext(); 
//  File[] listOfFiles = folder.listFiles(filter); 

    //grab file names 
     File[] listOfFiles = folder.listFiles(); 

     try { 
      FileOutputStream fout = new FileOutputStream (new File(OUTFILE)); 
      PrintWriter pout = new PrintWriter(fout); 
      for (int j = 0; j < listOfFiles.length; j++){ 

       //Hacky fix cause java is hard: 
       if (! listOfFiles[j].getName().endsWith(".txt")) { continue ; } 

       //append file name for mega dump file 
       pout.println(listOfFiles[j].getName()); // Append File-name 
       pout.flush(); //Probably a better way than this, but eh. 

       //Copy the file's text 
       Files.copy(listOfFiles[j].toPath(), fout); 
       fout.flush(); //Again, eh. 
      } 
      pout.close(); 
      pout.close(); 
      } 
      catch (IOException e) { 

      } 

     } 
} 
/* Ugh, IDK how to java. (This is the non-hacky way, but idk how to.) 

public class istext implements FileFilter { 
    public static void accept(File pathname){ 
     return(pathname.getName().endsWith(".txt")); 
    } 
} 

*/ 
Các vấn đề liên quan