导入Textfile并在Java中逐行读取 [英] Import Textfile and read line by line in Java

查看:100
本文介绍了导入Textfile并在Java中逐行读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何导入文本文件。我想导入一个文件,然后逐行阅读。

I was wondering how one would go about importing a text file. I want to import a file and then read it line by line.

谢谢!

推荐答案

我不知道导入文件是什么意思,但这是使用标准Java类逐行打开和读取文本文件的最简单方法。 (这应适用于所有版本的Java SE返回JDK1.1。使用Scanner是JDK1.5及更高版本的另一个选项。)

I've no idea what you mean by "importing" a file, but here's the simplest way to open and read a text file line by line, using just standard Java classes. (This should work for all versions of Java SE back to JDK1.1. Using Scanner is another option for JDK1.5 and later.)

BufferedReader br = new BufferedReader(
        new InputStreamReader(new FileInputStream(fileName)));
try {
    String line;
    while ((line = br.readLine()) != null) {
        // process line
    }
} finally {
    br.close();
}

这篇关于导入Textfile并在Java中逐行读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆