项目测试一 批量读取数据到数组

功能实现:

通过Reader类方法每次读入一行数据,然后对该行的8列数据进行转换为double型,显示  (n列)

通过readLine方法判断行是否读取完,进而判断有多少行  (m行)

功能缺陷:

虽然实现了对每一行数据按空格截取(使用正则可以区分多个空格),但是没有做到将数据读到二维数组中,然后对数据处理

代码:

package com.swust.file;
import java.io.*;
public class fileTest {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO Auto-generated method stub
        try {
               BufferedReader br = new BufferedReader(new FileReader("1.txt")); //reader类操作
               String str = "";
               //行列计数初始化
               int rank = 0;int coulumn = 0;
               
               while((str=br.readLine())!=null){  
                            System.out.println(str);
                            rank++;     //行计数
                            String [] arryStr = str.split("\s+");//表示一个或多个空格截取
                            //建立对应的double数组
                             Double [] result = new Double[8];
                             coulumn=arryStr.length;
                             //String转换为Double型
                            for(int i=0;i<8;i++){
                                if(!arryStr[i].trim().equals("")){
                                result[i]=Double.parseDouble(arryStr[i]);
                                }  
                             }
                            //显示double数据
                            for(int i=0;i<8;i++){
                                System.out.println("s"+i+":"+result[i]);
                               }
                        
               }
               System.out.println("一共有"+rank+"行"+";每行有"+coulumn+"列");
               br.close();
              } catch (FileNotFoundException e) {
               e.printStackTrace();
              } catch (IOException e) {
               e.printStackTrace();
              }
            
    }

}
原文地址:https://www.cnblogs.com/shuqingstudy/p/4729511.html