我想要读取一个文件,其中包含2个不同的部门,拆分行并将具有信息技术的行放在新的文本文件中,将那些具有商务部门的行放在另一个文本文件中。请帮忙。这是我读取文件所做的:
public class Splitter {
private static ArrayList<String> lineArray = new ArrayList<String>();
public static class Score { // to close a file
public void closeFile(Scanner fileToClose) {
try {
if (fileToClose != null)
fileToClose.close();
} catch (Exception exception) {
System.err.println("Error closing the files.");
System.exit(1);
}
}
private static void read() throws FileNotFoundException {
File myFile = new File("D:\\CourseMasterFile.txt"); // find and get
// the
// external
// file information
Scanner inputFile = new Scanner(myFile); // open and read the file
// from
// the hard disk in to
// the
// RAM
while (inputFile.hasNextLine()) { // while there is a line left in
// the
// file
String sentence = inputFile.nextLine(); // get the one line from
// the
// file
System.out.println(sentence); // print the line in to the consul
lineArray.add(sentence);
}
inputFile.close();
}
public static void main(String[] args) throws FileNotFoundException
{
read();
}
}
答案 0 :(得分:0)
尝试以下代码;
FileWriter out2 = new FileWriter(file2);
FileWriter out1 = new FileWriter(file1);
try{
if(sentence.contains("information technology"))
out1.write(sentence);
else
out2.write(sentence);
} finally {
try {
out1.close();
out2.close();
} catch (IOException e) {
e.printStackTrace();
}
}