如何在java中读取一个文本文件,并将一些记录放在另一个文本文件中,其余的放在另一个java文本文件中
我想读一个文件,其中有两个不同的部门,拆分行,将具有信息技术的行放在一个新的文本文件中,将具有业务部门的行放在另一个文本文件中。请帮忙。以下是我读取文件时所做的操作:如何在java中读取一个文本文件,并将一些记录放在另一个文本文件中,其余的放在另一个java文本文件中,java,arrays,file-io,Java,Arrays,File Io,我想读一个文件,其中有两个不同的部门,拆分行,将具有信息技术的行放在一个新的文本文件中,将具有业务部门的行放在另一个文本文件中。请帮忙。以下是我读取文件时所做的操作: public class Splitter { private static ArrayList<String> lineArray = new ArrayList<String>(); public static class Score { // to close a file publ
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();
}
}
公共类拆分器{
私有静态ArrayList lineArray=新ArrayList();
公共静态类分数{//关闭文件
公共无效关闭文件(扫描程序文件关闭){
试一试{
if(fileToClose!=null)
fileToClose.close();
}捕获(异常){
System.err.println(“关闭文件时出错”);
系统出口(1);
}
}
私有静态void read()引发FileNotFoundException{
File myFile=新文件(“D:\\CourseMasterFile.txt”);//查找并获取
//
//外部的
//文件信息
Scanner inputFile=new Scanner(myFile);//打开并读取文件
//从
//将硬盘插入到
//
//公羊
while(inputFile.hasNextLine()){//while中还有一行
//
//文件
String语句=inputFile.nextLine();//从
//
//文件
System.out.println(句子);//将行打印到领事处
线性排列。添加(句子);
}
inputFile.close();
}
公共静态void main(字符串[]args)引发FileNotFoundException
{
read();
}
}
尝试下面的代码
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();
}
}
告诉我们您的示例出了什么问题,然后我们可以帮助您。我们需要一个特定的问题。我想知道如何将文件放入两个数组,并将数组放入两个不同的文本文件中。一半记录放在一个文件中,其余记录放在另一个文件中。非常感谢您的时间