2016-07-08 36 views
5

問題和我在的位置:我無法將文本追加到用程序創建的這些新文件中。目前它只複製文件但不附加它們。見評論 "// append file name into the new file "在新文件中未附加文本以嘗試創建文本文件操縱器

其次,最終轉儲文件似乎只追加.java文件,它不讀取或追加輸入文件。

的我想要做的解釋:

長期和短期的是,我試圖做出將被放置到與數據填充.txt文件夾隨機程序。

我需要它是唯一

  • 該文件夾的範疇程序

    • 看然後採取任何txt文件,並
      一)進行復制,但追加原文件名進入文本正文(頂部),在子文件夾裏面,如「< filenamehere.txt」進入正文(頂部)

      b)然後複製original.txt的正文內容 c) prepended .txt文件和應用程序它終結了dump.txt文件 d)重複此爲所有本地txt文件,並保留追加到dump.txt文件

    所以在最後的最後,如果我有7個文件,我將有7個附加副本和1個巨大轉儲文件,其中包含7個附加副本的所有內容。例如,如果我有三個文本文件,每個文件只有一個單詞。所以a.txt,b.txt,c.txt 和三個詞是「Hello world!」。該A.TXT副本將有內部

    內容「> A.TXT

    你好

    現在它只是複製你好(原主體內容),但不追加> A.TXT。最終的轉儲文本文件不會累積其他文件中的任何內容,但奇怪的是從.java文件中獲取源代碼。所以本質上,我得到一個// Output文件夾,裏面是.txt文件的副本和一個管理拾取.java文本的megaDump.txt,但沒有其他文本文件被追加。

    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

    你經常捕捉並忽略'IOException'。所以你可能在你的程序中有幾個例外,你不知道。首先實施適當的錯誤記錄 – Nikem

    回答

    2

    這裏有相當多的問題:

    • 您使用您的文件路徑有時削減,有時2反斜線而最終導致我的Mac上至少問題,有時甚至是雙斜線。只需使用常規正斜槓。

    • 該代碼沒有過濾.txt文件,所以當前目錄中的所有內容都被處理 - 即使是正在執行的程序本身。

    • 當前代碼將> sometext.txt行直接寫入您的masterDump.txt,而不是間接通過您的文件副本。

    • 對於循環的每次迭代,代碼覆蓋masterDump.txt,因爲文件未以附加模式打開。

    以下是目前生產與a.txt中,b.txt和c.txt含「你好」,「世界」和「!」的文件夾中調用的時候,下面的結果代碼分別。我希望這是所需的行爲。
    請注意,在這段代碼中有很多需要改進的地方,特別是處理已經在註釋中指出的錯誤。

    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

    與他人達成一致:你刪除你的進步,你的每一次 '觸摸' 你masterDump文件。 這是我的版本:

    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")); 
        } 
    } 
    
    */