【发布时间】:2022-10-02 21:22:11
【问题描述】:
我有一个文件,想逐行拆分文件。但我不想每次都创建一个新文件。只需将每一行存储在一个数组中。 .split() 方法正是我想要的,但它不能用于文件。
import java.io.File;
import java.io.FileNotFoundException;
class Read{
public static void main(String args[])
{
try{
File datei = new File(\"file.txt\");
String[] splitDatei = datei.split(System.lineSeparator());
myReader.close();
}catch(FileNotFoundException e){
System.out.println(\"\");
e.printStackTrace();
}
}
}
-
也许
BufferedReader.lines()后跟Stream.toArray()- 或Files.readAllLines()和List.toArray()(如果确实需要数组) -
\"want to create a new file each time\"- 但你只是在你的代码中读取一个文件,并且不是创建一个新文件或写任何东西。您至少需要用伪代码描述您的意图。是否要将文件的每一行拆分为多行,然后将这些数据写入新创建的文件中?