1

我编写了一个程序,它从一个文件夹中读取 150 个文件,然后创建一个新文件夹并创建这 150 个文件并将内容写入其中。

这是我的代码:

package TestPackage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class EmployeeSorting {
    // path till 'employee files' folder.
    File folder = new File("D:\\Arthi iyer\\employee files");

    // listFiles() : list all the files in a folder.
    File[] listOfFiles = folder.listFiles();

    ArrayList<Employee> emp = new ArrayList<Employee>();
    String[] split_input = null;

    public void sortFiles() throws IOException {

        for (File file : listOfFiles) {
            Scanner scanner = new Scanner(file);

            String input = scanner.nextLine();
            split_input = input.split("=");
            int id = Integer.parseInt(split_input[1]);

            String input1 = scanner.nextLine();
            split_input = input1.split("=");
            String name = split_input[1];

            String input2 = scanner.nextLine();
            split_input = input2.split("=");
            int age = Integer.parseInt(split_input[1]);

            emp.add(new Employee(id, name, age));

            FileReader fr = new FileReader(file);

            BufferedReader br = new BufferedReader(fr);
            File idFile = new File("D:\\Arthi iyer\\employee id" + "\\" + id
                    + ".txt");
            FileWriter idcopy = new FileWriter(idFile);

            int idbuffersize1;
            char[] a = new char[1000];

            while ((idbuffersize1 = br.read(a)) > 0) {

                idcopy.write(a, 0, idbuffersize1);

            }

        }// for ends
    }// method ends

    public void sortByID() {

        System.out.println("----Sort By Employee Id----");
        Collections.sort(emp, new EmployeeSortById());
        printEmployees(emp);
    }

    // print employee
    public void printEmployees(ArrayList<Employee> emp) {
        for (Employee e : emp) {

            System.out.println("Id->" + e.getEmp_id() + " " + " Name-> "
                    + e.getEmp_name() + "  " + "age-> " + e.getAge());
        }
    }

    public static void main(String[] args) throws IOException {

        File id_folder = new File("D:\\Arthi iyer\\employee id");
        id_folder.mkdir(); // mkdir() for making directory.

        EmployeeSorting sort = new EmployeeSorting();
        sort.sortFiles();
        // sort.sortByID();

    }

}

我的问题是文件正在创建到文件夹但内容没有被写入..请指导我

4

3 回答 3

3

您没有关闭编写器,因此所有数据可能都仅保留在内部缓冲区中。

理想情况下,使用 try-with-resources 语句,或在一个finally块中关闭它。

int idbuffersize1;
char[] a = new char[1000];
FileWriter idcopy = new FileWriter(idFile);
try {  
    while ((idbuffersize1 = br.read(a)) > 0) {
        idcopy.write(a, 0, idbuffersize1);
    }
} finally {
    idcopy.close();
}

顺便说一句,我个人不喜欢FileWriter,因为它总是使用系统默认编码,而不是允许您指定编码。我更喜欢使用FileOutputStream包裹在OutputStreamWriter.

于 2013-06-05T06:47:20.067 回答
0

您没有刷新或关闭文件,因此无法将内容写入文件。

此外,我强烈建议您使用BufferedWriterorPrintWriter来编写普通文本。

于 2013-06-05T06:58:33.977 回答
0

尝试这个

FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
于 2013-06-05T06:50:19.440 回答