Java中按行读取文件的问题

nuptsww 2015-09-02 02:26:59
我在代码中想读取如下格式的文件,但是问题是逗号后面的那个数字丢失了,这怎么解决?
0.0220472,0.0299213 0 0.0220472 0 0.00314961 0.0283465 0.00629921 0.0015748
0.0204724,0 0.0220472 0 0.00314961 0.0283465 0.00629921 0.0015748 0.0220472

代码大致如下:
double [][] x;
double [] y;

int lag=8;
File file = new File("src/data.txt");
int row=getFileLineCounts("src/data.txt");
x=new double[row][];
y=new double[row];
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));

int line = 1;
String tempString = null;
String [] tempStringComma = null;
String [] tempStringSpace = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
// System.out.println("line " + line + ": " + tempString);
tempString = reader.readLine();
tempStringComma = tempString.split(",");
tempStringSpace = tempStringComma[1].split(" ");
StringTokenizer st = new StringTokenizer(tempString,", ");
while(st.hasMoreTokens() ){
System.out.println(st.nextToken());
}
line++;
}

System.out.println("line is "+line);

reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
System.out.println("Wrong");
}
}
}
System.out.println("READ END");
...全文
224 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
 while ((tempString = reader.readLine()) != null) {
                // 显示行号
                // System.out.println("line " + line + ": " + tempString);
//                tempString = reader.readLine(); 这里你又读了1行,注释掉就行了
                tempStringComma = tempString.split(",");
                tempStringSpace = tempStringComma[1].split(" ");
                StringTokenizer st = new StringTokenizer(tempString,", ");
                while(st.hasMoreTokens() ){
                    System.out.println(st.nextToken());
                }
                line++;
            }
铁匠梁老师 2015-09-02
  • 打赏
  • 举报
回复
因为你的没有使用读取到的第一行代码

static void readTest(){
		double [][] x;
        double [] y;

        int lag=8;
        File file = new File("c:/data.txt");
        int row=getFileLineCounts(file);
        x=new double[row][];
        y=new double[row];
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            int lineunm = 1;
            String line = null;
            String [] values = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                values = line.split(" ");
                for(String one:values){
                	System.out.println(one);
                }
                lineunm++;
            }

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    System.out.println("Wrong");
                }
            }
        }
        System.out.println("READ END");
	}
	
	public static int getFileLineCounts(File file) {
        int cnt = 0;
        LineNumberReader reader = null;
        try {
            reader = new LineNumberReader(new FileReader(file));
            while (reader.readLine() != null) {}
            cnt = reader.getLineNumber();
        } catch (Exception ex) {
            cnt = -1;
            ex.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return cnt;
    }
三仙半 2015-09-02
  • 打赏
  • 举报
回复
你这段代码是数据丢行,还是在每行里面丢数据啊?我觉得是丢行,但是,不丢数据。
0萌萌哒0 2015-09-02
  • 打赏
  • 举报
回复

System.out.println(Arrays.toString("0.0204724,0 0.0220472 0 0.00314961 0.0283465 0.00629921 0.0015748 0.0220472".split("[\\,\\ ]+")));
另:那个tokenizer也支持使用正则表达式作为分隔符。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧