java中怎么从文件中读取数

比如:文件中放的是37107287533902102798797998220837590246510135740250这样连续的数字,通过什么办法可以把这串数字读入,并存... 比如:文件中放的是37107287533902102798797998220837590246510135740250
这样连续的数字,通过什么办法可以把这串数字读入,并存放在一个整形数组里面?
展开
 我来答
安子安辰
2017-10-25
知道答主
回答量:4
采纳率:0%
帮助的人:3.7万
展开全部
1.package txt;
2.
3.import java.io.BufferedReader;
4.import java.io.File;
5.import java.io.FileInputStream;
6.import java.io.InputStreamReader;
7.
8./**
9. * 读取TXE数据
10. */
11.public class ReadTxtUtils {
12. public static void main(String arg[]) {
13. try {
14. String encoding = "GBK"; // 字符编码(可解决中文乱码问题 )
15. File file = new File("c:/aa.txt");
16. if (file.isFile() && file.exists()) {
17. InputStreamReader read = new InputStreamReader(
18. new FileInputStream(file), encoding);
19. BufferedReader bufferedReader = new BufferedReader(read);
20. String lineTXT = null;
21. while ((lineTXT = bufferedReader.readLine()) != null) {
22. System.out.println(lineTXT.toString().trim());
23. }
24. read.close();
25. }else{
26. System.out.println("找不到指定的文件!");
27. }
28. } catch (Exception e) {
29. System.out.println("读取文件内容操作出错");
30. e.printStackTrace();
31. }
32. }
33.}
java读取TXT文件中的数据,每一行就是一个数,返回一个数组,代码?
?
List list=new ArrayList();
BufferedReader br=new BufferReader(new InputStreamReader(new FileInputStream(new File("in.txt"))));
String str=null;
while((str=br.readLine())!=null)
{
list.add(new Integer(str));

}
Integer[] i=new Integer[list.size()];
list.toArray(i);

TXT文本中如据形如:
123
456
789

读入二维数组效果为:
temp[0][]={1,2,3};
temp[1][]={4,5,6};
temp[2][]={7,8,9};

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.*;

public class xx{
public static void main(String[]args){
String s;
int[][]save=new int[3][3];
try{
BufferedReader in =new BufferedReader(new FileReader("C:\\txt.txt"));
int i=0;
while((s=in.readLine())!=null){
save[i][0]=Integer.parseInt(s.substring(0,1));
save[i][1]=Integer.parseInt(s.substring(1,2));
save[i][2]=Integer.parseInt(s.substring(2,3));
i++;
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++){
System.out.print(save[i][j]);
}
System.out.println();
}
}
}


?
BufferedReader bf=new BufferedReader(new FileReader("Your file"));
String lineContent=null;
int i = 0;
int [][] temp = new int [3][];
while((lineContent=bf.readLine())!=null){
String [] str = lineContent.split("\\d");// 将 lineContent 按数字拆分
for(int j = 0; j < str.length(); j++){
int [i][j] = Integer.parseInt(str[j]);
}
i++;
}

scp|cs|ff|201101
这是d:\\a.txt的数据,与“|”分割取数据出来,保存在变量a;b;c;d里

import java.io.*;

public class Test{
public static void main(String[] args)throws Exception{
String a, b, c, d;
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader("d:\\a.txt"));
String s = br.readLine();
while(s != null){
sb.append(s);
s = br.readLine();
}
s = sb.toString();
String[] str = s.split("|");
a = str[0];
b = str[0];
c = str[0];
d = str[0];
}
}
flyingFish211
2011-03-12 · TA获得超过2.1万个赞
知道大有可为答主
回答量:1.5万
采纳率:50%
帮助的人:1.2亿
展开全部
整形肯定不可以的。必须隔开多少位放入数组
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ABC {
public static void main(String[] args) throws IOException {

String fileName = "data.txt";//存放数据的文件
final int splitLength = 5;//要分割的单位,如果是5,就是每隔5个数字分一次,如果是1,就是
//每1个数字放入数组中

List<String> numberList = getFileContent(fileName);

Integer[] numAry = splitNumberList(numberList, splitLength);

for(int value: numAry){
System.out.println(value);
}
}

private static Integer[] splitNumberList(List<String> numberList, int splitLength) {

List<Integer> integerList = new ArrayList<Integer>();

for(int i = 0; i < numberList.size(); i++){
String lineContent = numberList.get(i);

while(lineContent.length() >= splitLength){
integerList.add(Integer.parseInt(lineContent.substring(0, splitLength)));
lineContent = lineContent.substring(splitLength);
}

if(lineContent.length() != 0){
integerList.add(Integer.parseInt(lineContent));
}
}

return integerList.toArray(new Integer[0]);
}

private static List<String> getFileContent(String fileName) throws IOException {
BufferedReader bf = new BufferedReader(new FileReader(fileName));

List<String> list = new ArrayList<String>();

String content = null;

while((content = bf.readLine()) != null){
if(!content.trim().equals("")){
list.add(content);
}
}

return list;
}
}
--------------------------结果里面输出4位的,因为最前面1位是0
37107
28753
39021
2798
79799
82208
37590
24651
1357
40250
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
lizhengkai1218
2011-03-12
知道答主
回答量:42
采纳率:0%
帮助的人:0
展开全部
可以用scanner先读取这字符串,然后根据题意分割成整形数放入数组再处理。
更多追问追答
追问
可不可以来个具体实现。。我试了很多方法都达不到我所说的那样。
追答
那你打算怎么分割这个字符串啊?这数太大估计放不进长整形里的
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
mszb3mzi
2011-03-12 · 超过15用户采纳过TA的回答
知道答主
回答量:28
采纳率:0%
帮助的人:30.9万
展开全部
是啊 你问的有点太粗了
1你是想从什么里面读取出来 配置文件? text? xml?,,,,,,,
2你是想怎么放 或者说 你到底想锝到 什么结果?
package dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test007 {

/**
* @param args
* 请先建立c:\b.txt这个文件
* 内容如下
* 37107287533902102798797998220837590246510135740250
*/
public static void main(String[] args){
File file = new File("c:\\b.txt");
String str = null;
try {
FileReader f_reader = new FileReader(file);
BufferedReader reader = new BufferedReader(f_reader);
str = reader.readLine();
String num_long = "";
while (str != null) {
System.out.println("读取到的数据为: "+str);
num_long = str;
str = reader.readLine();
}
System.out.println("开始对 "+num_long+ "进行操作...");
String num_small = "";
System.out.println(num_long.length());
List num_list = new ArrayList();
int[] num_list_int = new int[num_long.length()];
for (int i = 0; i < num_long.length(); i++) {
num_list.add(num_long.subSequence(i, i+1));
num_list_int[i] = Integer.parseInt((String) num_list.get(i));
}
System.out.print("放入到整数数列num_list_int里的值为: ");
for (int i = 0; i < num_list_int.length; i++) {
System.out.print(" "+num_list_int[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
antony9
2011-03-12 · TA获得超过278个赞
知道小有建树答主
回答量:73
采纳率:0%
帮助的人:121万
展开全部
根据楼上的说法,给出代码片段。未经测试:

Scanner sc = new Scanner(new FileInputStream(new File("你文件的路径")));
String line = sc.nextLine();
int[] nums = new int[line.length()];

for (int i = 0 ; i<line.length(); i++){
String numString = line.substring(i, i+1);
nums[i] = Integer.parseInt(numString);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式