读书人

JAVA怎样从txt文件中读入数据到相应类

发布时间: 2012-03-14 12:01:12 作者: rapoo

JAVA怎样从txt文件中读入数据到相应类型的数组
数据格式如下:
年份吉安峡江桑庄寨头
5 4
80101.5121.8227.2110
7981.9101.572.4106
7859.570.441.359.7
777976.985.1148.6

写的程序如下,抛出异常
[code=Java][/code]
import java.io.*;
public class analysis {
public static void main(String[] args){
String path=null;
String s1;
String s2[];
int m,n;
Double array[][];
FileReader in=null;
BufferedReader input=null;
path="D:\\JavaProject\\analysis\\src\\Data.txt";
try{
in=new FileReader(path);
input=new BufferedReader(in);
}
catch(FileNotFoundException e1){
System.out.println("");
System.exit(-1);
}
try{
s1=input.readLine();
s2=s1.split(" ", 0);
for(int i=0;i<s2.length;i++){
System.out.println(s2[i]);
}
s1=input.readLine();
s2=s1.split(" ", 0);
m=Integer.valueOf(s2[0]);
n=Integer.valueOf(s2[1]);
array=new Double[n][m];
for(int i=0;i<n;i++){
s1=input.readLine();
s2=s1.split(" ", 0);
for(int j=0;i<s2.length;i++){
array[i][j]=Double.valueOf(s2[j]);
System.out.println(array[i][j]);
}
}
input.close();
in.close();
}
catch(IOException e2){
System.out.println("");
System.exit(-1);
}
}
}

运行结果:
年份吉安峡江桑庄寨头
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.valueOf(Integer.java:582)
at analysis.main(analysis.java:29)


[解决办法]
中间有多个连续的空格吧?
[解决办法]
[code=Java][/code]import java.io.*;

public class analysis {
public static void main(String[] args) {
String path = null;
String s1;
String s2[];
int m, n;
Double array[][];
FileReader in = null;
BufferedReader input = null;
try {
in = new FileReader("Data.txt");
input = new BufferedReader(in);
} catch (FileNotFoundException e1) {
System.out.println("");
System.exit(-1);
}
try {
s1 = input.readLine();
s2 = s1.split(" ", 0);
for (int i = 0; i < s2.length; i++) {
System.out.println(s2[i]);
}
s1 = input.readLine();
s2 = s1.split(" ", 0);
m = Integer.valueOf(s2[0]);//m列
n = Integer.valueOf(s2[1]);//n行
System.out.println("m="+m+" "+"n="+n);
array = new Double[n][m];
for (int i = 0; i < n; i++) {
s1 = input.readLine();
s2 = s1.split(" ", 0);
for (int j = 0; j < s2.length; j++) {
array[i][j] = Double.valueOf(s2[j]);
System.out.print(array[i][j]+" ");
}
System.out.println();
}
input.close();
in.close();
} catch (IOException e2) {
System.out.println("");
System.exit(-1);
}
}
}


txt文档中数据之间只需一个空格
------解决方案--------------------


m=Integer.valueOf(s2[0]);
n=Integer.valueOf(s2[1]);

有字符转数字的 加上trim() 有空格也会出错的

m=Integer.valueOf((s2[0]).trim());

[解决办法]

Java code
package com.maijunjin.io;import java.io.*;public class analysis {    public static void main(String[] args) {        String path = null;        String s1;        String s2[];        int m, n;        Double array[][];        FileReader in = null;        BufferedReader input = null;        path = "D:\\Data.txt";        try {            in = new FileReader(path);            input = new BufferedReader(in);        } catch (FileNotFoundException e1) {            System.out.println("");            System.exit(-1);        }        try {            s1 = input.readLine();            s2 = s1.split(" ", 0);            for (int i = 0; i < s2.length; i++) {                System.out.print(s2[i]+"  ");            }            System.out.println();            s1 = input.readLine();            s2 = s1.split(" ", 0);            m = Integer.valueOf(s2[0]);            System.out.println("11" + s2.length + "11");//两个空格的话,比如"5  4"结果是"5" "" "4"            n = Integer.valueOf(s2[1]);            array = new Double[n][m];            for (int i = 0; i < n; i++) {                s1 = input.readLine();                s2 = s1.split(" ", 0);                for (int j = 0; j < s2.length; j++) {                    array[i][j] = Double.valueOf(s2[j]);                    System.out.println(array[i][j]);                }            }            input.close();            in.close();        } catch (IOException e2) {            System.out.println("");            System.exit(-1);        }    }}
[解决办法]
for(int j=0;i<s2.length;i++){

你这句话没问题么????
给j赋0值,给i加加
[解决办法]
如果考虑严谨的话, 可以将读取到的数据去空格,然后通过正则或其他方式匹配判断一下,属于什么类型转什么类型就好了,
毕竟这数据里面数据类型不多。
[解决办法]
String[] res = target.split("[ \t]+", 0);

用正则表达式不就行了


---------------
终于登录上来了,不容易啊

读书人网 >J2SE开发

热点推荐