2016-07-08 8 views
5

問題とはどこですか:私はプログラムで作成した新しいファイルにテキストを追加できません。現在のところ、ファイルはコピーされますが、ファイルは追加されません。コメント "// append file name into the new file "の行を参照してください。テキストファイルマニピュレータを作成しようとすると、テキストが新しいファイルに追加されない

第2に、最後のダンプファイルは.javaファイルのみを追加するように見えますが、入力ファイルを読み込んだり追加したりしていません。

私が何をしようとしているの説明:

長いと短いが、私はデータで埋め.txtファイルとランダムなフォルダに配置されますプログラムを作成しようとしているということです。

は私が次にどんな.txtファイルを取り、
a)のコピーを作成しますが、元のファイル名を追加し、それが唯一の

  • にあるフォルダの領域内

    • 見るためにプログラムが必要テキスト本体(上部)に、次いでoriginal.txt Cのボディの内容をコピーする(上部の)体内へ「< filenamehere.txt」

      B)のようなサブフォルダ内に)取りますプリペンドド.txtファイルとアプリ単一のファイルをdump.txt Dにそれを終わら)私は7つのファイルを持っていた場合は、すべてのローカルのtxtファイルのためにこれを繰り返し、それで終わりをdump.txtファイルの末尾に

    を付加しておく、I 7つの追加コピーがあり、1つの巨大なダンプファイルには7つの追加コピーがすべて含まれます。たとえば、3つのテキストファイルがあり、それぞれに1つの単語しか含まれていないとします。したがってa.txt、b.txt、c.txt と3つの単語は "Hello world!"です。 A.TXTコピーは今

    それはちょうどこんにちは(オリジナルボディの内容)をコピーしますが追加いない「

    こんにちは

    A.TXT>」

    内部の内容を持っているでしょう> a.txt。最終的なダンプテキストファイルは他のファイルから何も蓄積されませんが、それは不思議ですが、.javaファイルからソースコードを取り出すだけです。ですから、本質的には、// Outputフォルダがあり、insideは.txtファイルのコピーであり、megaDump.txtは.javaテキストをピックアップしますが、他のテキストファイルは追加されません。

    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ファイルをフィルタリングしませんでした。したがって、現在のディレクトリにあるすべてのものが処理されました。

    • 現在、コードでは、ファイルコピーを介して間接的にではなくmasterDump.txtに直接> sometext.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")); 
        } 
    } 
    
    */ 
    
    関連する問題