我编写了一个程序,它从一个文件夹中读取 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();
}
}
我的问题是文件正在创建到文件夹但内容没有被写入..请指导我