首页 > 代码库 > java 知识点

java 知识点

java读取txt文件

class DataLoad{public double[][] LoadTxt(String filePath,int n,int m,int k){double[][] data=http://www.mamicode.com/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;}}