本篇文章为大家展示了TXT文件的内容如何利用Java实现读取,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Java读取txt文件内容。可以作如下理解:首先获得一个文件句柄。File file = n
本篇文章为大家展示了TXT文件的内容如何利用Java实现读取,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Java读取txt文件内容。可以作如下理解:
package com.campu; import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.Reader;public class H20121012 { public static void readTxtFile(String filePath){ try { String encoding="GBK"; File file=new File(filePath); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ System.out.println(lineTxt); } read.close(); }else{ System.out.println("找不到指定的文件"); } }catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } } public static void main(String argv[]){ String filePath = "L:\\Apache\\htdocs\\res\\20121012.txt";// "res/"; readTxtFile(filePath); } }
--结束END--
本文标题: TXT文件的内容如何利用Java实现读取
本文链接: https://www.lsjlt.com/news/225734.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0