问题以及我在哪里:我无法将文字附加到我使用该程序创建的这些新文件中。目前它只复制文件但不附加它们。见评论行
"// append file name into the new file "
。
其次,最终转储文件似乎只附加.java文件,它没有读取或附加输入文件。
对我尝试做的事情的解释:
长期和短期是我试图制作一个程序,将其放入随机文件夹中,其中.txt文件填充数据。
我需要程序
然后取任何.txt文件和
a)制作副本,但将原始文件名附加到文本正文中(在顶部),在"<< filenamehere.txt"进入体内(在顶部)
b)然后复制original.txt的正文内容 c)获取前置的.txt文件并将其附加到单个dump.txt文件中 d)对所有本地txt文件重复此操作并继续追加到dump.txt文件的末尾
所以最后,如果我有7个文件,我将有7个附加副本和1个巨大转储文件,其中包含7个附加副本的所有内容。例如,如果我有三个文本文件,每个文件只有一个单词。所以a.txt,b.txt,c.txt 这三个词是" Hello world!"。 a.txt副本将包含
内的内容"> A.TXT
您好
"
现在它只是复制Hello(原始正文内容)但不附加> a.txt。最终的转储文本文件不会累积其他文件中的任何内容,但奇怪的是从.java文件中获取源代码。基本上,我得到一个//输出文件夹,里面是.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 :(得分:2)
这里有很多问题:
您使用的文件路径有时会斜线,有时会出现2个反斜杠,有时甚至是双斜线,至少在我的Mac上会出现问题。只需使用常规正斜杠即可。
代码还没有过滤.txt文件,因此处理了当前目录中的所有内容 - 甚至是正在执行的程序本身。
目前,代码将> sometext.txt
行直接写入您的masterDump.txt
,而不是间接通过您的文件副本。
由于文件未在附加模式下打开,代码会在循环的每次迭代中覆盖masterDump.txt
。
以下是当在包含“Hello”,“World”和“!”的a.txt,b.txt和c.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
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 :(得分:1)
与他人同意:每当您触摸&#39;时,您都会删除自己的进度。你的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"));
}
}
*/