标签:style blog color java os io 文件 for
java读取txt文件
class DataLoad{ public double[][] LoadTxt(String filePath,int n,int m,int k) { double[][] data=new double[m][n]; 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; String[] temp=new String[15]; for(int i=0;i<m;i++) { lineTxt = bufferedReader.readLine(); //System.out.println(lineTxt); temp=lineTxt.split("\t"); for(int j=k;j<k+n;j++) { data[i][j-k]=Double.parseDouble(temp[j]); } } read.close(); }else{ System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } return data; } }
标签:style blog color java os io 文件 for
原文地址:http://www.cnblogs.com/villa/p/3904925.html