关于JAVA中读取文件!!

来源:百度知道 编辑:UC知道 时间:2024/06/06 06:31:25
我有一个文件,3行,每都是:del a 45
请问怎么才能一行行读入,并取出每行中的3个元素呢(在终端显示)?
(好像要用split)
请高手指点!

// 读取文本内容
static void readText(String filePath) {
try {
FileReader reader = new FileReader(filePath);
BufferedReader bfReader = new BufferedReader(reader);
String text = null;
while ((text = bfReader.readLine()) != null) {
String[] texts = text.split(" ");
System.out.print("第一个元素是:" + texts[0]);
System.out.print("第一个元素是:" + texts[1]);
System.out.println("第一个元素是:" + texts[2]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
readText("test.txt");
}