Java读取TXT文件内容的方法
作者:Java开发-10 时间:2023-11-23 22:33:41
Java读取txt文件内容。可以作如下理解:
首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人之间连通电话网络了。接下来可以开始打电话了。
通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西
既然你使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据
解读完成后要输出呀。那当然要转换成IO可以识别的数据呀。那就需要调用字节码读取的方法BufferedReader()。同时使用bufferedReader()的readline()方法读取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
{
/**
*
功能:Java读取txt文件的内容
*
步骤:1:先获得文件句柄
*
2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
*
3:读取到输入流后,需要读取生成字节流
*
4:一行一行的输出。readline()。
*
备注:需要考虑的是异常情况
*
@param filePath
*/
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);
}
}
来源:http://blog.csdn.net/qq_37810594/article/details/72867454?utm_source=tuicool&utm_medium=referral
标签:java,读取,txt
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mall整合SpringTask实现定时任务的方法示例
2023-09-15 18:08:08
举例讲解Java的Spring框架中AOP程序设计方式的使用
2022-11-21 21:22:15
DataBinding onClick的七种点击方式
2021-12-08 01:23:22
Android中ListView用法实例分析
2022-04-08 17:11:29
spring boot 加载web容器tomcat流程源码分析
2021-12-05 14:48:38
![](https://img.aspxhome.com/file/2023/9/73109_0s.png)
Winform圆形环绕的Loading动画实现代码
2023-09-09 00:13:47
![](https://img.aspxhome.com/file/2023/9/88389_0s.gif)
Android 高版本API方法在低版本系统上的兼容性处理
2022-08-22 08:51:53
C语言实现学生信息管理系统
2023-05-24 12:08:50
![](https://img.aspxhome.com/file/2023/8/129548_0s.jpg)
Android仿支付宝支付从底部弹窗效果
2022-04-30 10:37:13
![](https://img.aspxhome.com/file/2023/7/123307_0s.gif)
SpringBoot实现过滤器、拦截器与切片的实现和区别
2023-04-28 22:51:04
![](https://img.aspxhome.com/file/2023/1/84641_0s.png)
Kotlin协程Dispatchers原理示例详解
2022-09-26 00:09:45
SpringMVC使用@PathVariable接收参数过程解析
2021-09-03 20:52:41
Java保留两位小数的几种写法总结
2022-04-19 06:53:06
C#开发微信公众号接口开发
2023-10-14 11:49:01
![](https://img.aspxhome.com/file/2023/0/82110_0s.jpg)
如何用C#找出数组中只出现了一次的数字
2021-06-15 15:42:56
C# 字符串多行显示/文本换行以textbox为例讲解
2022-02-02 06:35:28
![](https://img.aspxhome.com/file/2023/7/102267_0s.png)
Android开发中MotionEvent坐标获取方法分析
2022-03-29 02:43:42
![](https://img.aspxhome.com/file/2023/0/93280_0s.png)
C#多线程的Join()方法
2022-05-08 23:56:13
![](https://img.aspxhome.com/file/2023/2/85712_0s.png)
SpringBoot优雅地实现全局异常处理的方法详解
2023-09-06 09:17:02
Java使用Redis实现秒杀功能
2023-04-11 11:26:54
![](https://img.aspxhome.com/file/2023/1/78001_0s.jpg)