2016-07-08 35 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")); 
        } 
    } 
    
    */