java 读取文件里的数字

怎么在文件里读取数字啊?试了很多方法都不行。。。。。

java读取txt文件内容,可以作如下理解:
1、首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人之间连通电话网络了,接下来可以开始打电话了;
2、通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西;
3、既然使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据;
4、解读完成后就要输出,那么要转换成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;

/**
* @author 码农小江
* H20121012.java
* 2012-10-12下午11:40:21
*/
public class H20121012 {
/**
* @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);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-09-15

    读取文件

    分割文件:汉子2个字节 ,不要    //   字母 和数字  1个字节

    用正则表达式 匹配   留下数字

第2个回答  2013-08-13
String encoding="GBK";
File file=new File("D:/a.txt");
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){
char[] txt=lineTxt.toCharArray();
for(int index =0; index<txt.length; index++){
if(txt[index]>='0'&&txt[index]<='9'){
System.out.print(txt[index]);
}
}
}
read.close();
}

这是过滤掉你文件中的所有不属于数字的代码,希望对你有用追问

但是我的文本里一行可能出现多个数字啊。。。怎么破?

追答

这个就是读取文本里面所有的数字的

追问

不行。。。 数据为 100 291时就错了

追答

什么错误呢

追问

他得到的是100291,我要的是两个数100和291

追答

你的文件里面就只有数字吗?还是有别的一些什么呢

追问

有数字和空格

追答

String encoding="GBK";
File file=new File("D:/a.txt");
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){
String []str= lineTxt.split(" ");
for(String l:str){
System.out.println(l);
}
}
read.close();

这样试试

第3个回答  2013-08-13
类型转换一下不就哦了追问

肿么转换。。。要读的是一串数字。。。
001 002 994 774 113 442 553 563
就像这样。。。。

追答

最好用这个parseInt.valueof(你读的数字字符串)

追问

怎么用?有什么效果?

追答

就是把你读到lineTxt转换成int类型不就哦了

追问

不行。。。。。。抛出了异常

追答

中间有空的字符串,想办法把空格处理了。数字怎么可能中间有空格

追问

我就是在解决这个问题。。。因为一行里是有多个数字的。。。

追答

。。。这个问题你解决不了?string有一个方法去除空格的,你百度下

第4个回答  推荐于2018-02-27
java读取txt文本里的数字?读字母和数字有区别吗?追问

我只知道怎么读字符串。。。。。

追答

你把读字符串的代码贴出来

追问

这样只能一行一行读字符串,怎么读数字?

追答

数字不是也能出来吗?你说的读数字,是要怎么读数字?表达清楚一点

追问

对的,的确能读出数字,但是这个数字是字符串类型的,我要读整形的

追答

Integer.parseInt(str);
Integer.valueOf(str);
这2个都可以

追问

我的文件里会出现几个数字在一起的情况,怎么破?

追答

本回答被提问者和网友采纳
相似回答