file0.txt 中数据:
1234
90.7
78.8
4567
87.3
65.6
java代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static double[] writeToDat(String path) {
File file = new File(path);
List list = new ArrayList();
double[] nums = null;
try {
BufferedReader bw = new BufferedReader(new FileReader(file));
String line = null;
//因为不知道有几行数据,所以先存入list集合中
while((line = bw.readLine()) != null){
list.add(line);
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
//确定数组长度
nums = new double[list.size()];
for(int i=0;i<list.size();i++){
String s = (String) list.get(i);
nums[i] = Double.parseDouble(s);
}
return nums;
}
public static void main(String[] args) {
String path = "d:/file0.txt";
double[] nums = writeToDat(path);
for(int i=0;i<nums.length;i++){
System.out.println(nums[i]);
}
}
}