试试这个: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileOpenTest { private int count = 0; private FileOpenTest(String filePath) { File f = new File(filePath); try { BufferedReader br = new BufferedReader(new FileReader(f)); while (br.ready()) { br.readLine(); count ++; } } catch (IOException e) { e.printStackTrace(); } } private int getCount() { return count; } public static void main(String[] args) { String filePath = "D:\\test.txt"; //文件本地路径 FileOpenTest fot = new FileOpenTest(filePath); System.out.println(fot.getCount()); } }