700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java代码读取txt文件_java读取txt文件代码片段

java代码读取txt文件_java读取txt文件代码片段

时间:2023-11-20 12:15:52

相关推荐

java代码读取txt文件_java读取txt文件代码片段

本文实例为大家分享了java读取txt文件的具体代码,供大家参考,具体内容如下

学习小记:

1、首先要根据路径获取你的 txt 文本文件。File file = new File(path);

2、将获取到的这个字节码流读进缓存。new FileInputStream(file) ;

3、然后对刚才读进缓存的输入流进行解读,生成对应字节流。InputStreamReader(readIn)

4、再然后通过 BufferedReader 这个类进行一行一行的输出。bufferedReader.readLine()

代码:

public static void ReadTxt () {

try {

File file = new File("E:\\text.txt");

FileInputStream readIn = new FileInputStream(file);

InputStreamReader read = new InputStreamReader(readIn, "utf-8");

BufferedReader bufferedReader = new BufferedReader(read);

String oneLine= null;

while((oneLine= bufferedReader.readLine()) != null){

System.out.println(lineTxt);

}

read.close();

} catch (Exception e) {

System.out.println("读取文件内容出错");

e.printStackTrace();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持聚米学院。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。