问题和我在的位置:我无法将文本追加到用程序创建的这些新文件中。目前它只复制文件但不附加它们。见评论 "// 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