問題和我在的位置:我無法將文本追加到用程序創建的這些新文件中。目前它只複製文件但不附加它們。見評論 "// 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
你經常捕捉並忽略'IOException'。所以你可能在你的程序中有幾個例外,你不知道。首先實施適當的錯誤記錄 – Nikem