java简易读取文本的第一行和最后一行内容

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;

public class TimeCount {
public static void main(String[] args) throws IOException {
//第一行内容
String firstLine = “”;
//最后一行内容
String lastLine = “”;
//当前行内容
String currentLine = “”;
//读取的文件路径
File file = new File(“C://Users//chusi//Desktop//断点使用.txt”);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
//获取第一行内容
if((currentLine = br.readLine()) != null){
firstLine = currentLine;
}
//获取最后一行内容
while((currentLine = br.readLine())!= null){
lastLine = currentLine;
}
//输出,大功告成
System.out.println(firstLine);
System.out.println(lastLine);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(java知识点小记)