Sự cố và vị trí của tôi: Tôi không thể nối văn bản vào các tệp mới này mà tôi tạo bằng chương trình. Hiện tại, nó chỉ sao chép các tệp nhưng không sao chép chúng. Xem dòng có chú thích "// append file name into the new file "
.Văn bản không được thêm vào tệp mới khi cố gắng tạo một trình xử lý tệp văn bản
Thứ hai, tệp kết xuất cuối cùng dường như chỉ nối thêm tệp .java, nó không đọc hoặc chắp thêm tệp đầu vào.
Lời giải thích về những gì tôi đang cố gắng để làm:
Các dài và ngắn là tôi đang cố gắng để thực hiện một chương trình mà sẽ được đặt vào thư mục ngẫu nhiên với các file .txt chứa đầy dữ liệu.
tôi cần những chương trình để
- Look trong lĩnh vực của thư mục đó là chỉ trong
Sau đó lấy bất kỳ tập tin .txt và
a) tạo một bản sao nhưng thêm tên tập tin gốc vào nội dung văn bản (ở trên cùng), bên trong thư mục con như "< filenamehere.txt" vào phần thân (ở trên cùng)b) sau đó sao chép nội dung của tệp gốc.txt c) lấy tệp và ứng dụng đã được thêm trước .txt kết thúc nó vào một tập tin dump.txt đơn d) lặp lại điều này cho tất cả các file txt địa phương và giữ phụ thêm vào cuối của tập tin dump.txt
Vì vậy, ở cuối, nếu tôi đã có 7 tác phẩm, tôi sẽ có 7 bản sao được thêm vào và 1 tệp kết xuất khổng lồ chứa tất cả mọi thứ của 7 bản sao được thêm vào. Ví dụ: nếu tôi có ba tệp văn bản, mỗi tệp chỉ có một từ trong đó. Vì vậy, a.txt, b.txt, c.txt và ba từ là "Xin chào thế giới!". Bản sao a.txt sẽ có các nội dung bên trong
"> a.txt
Xin chào
"
Ngay bây giờ nó chỉ cần sao chép Hello (nội dung cơ bản gốc) nhưng không phải phụ thêm> a.txt. Tệp văn bản kết xuất cuối cùng không tích lũy bất kỳ thứ gì từ các tệp khác, nhưng nó đủ kỳ lạ để chọn mã nguồn từ tệp .java. Vì vậy, về cơ bản, tôi nhận được một // Thư mục đầu ra và bên trong là các bản sao của các tệp .txt và megaDump.txt quản lý để nhận văn bản .java, nhưng không có tệp văn bản nào khác được thêm vào.
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
Bạn liên tục bắt và bỏ qua 'IOException'. Vì vậy, bạn có thể có một số ngoại lệ trong chương trình của bạn mà bạn không biết. Triển khai ghi nhật ký lỗi thích hợp trước – Nikem