可以通过Java的IO流实现txt文本的读取,然后用readline实现按行读取。具体代码如下:
package test;
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;iString s = (String) list.get(i);
nums[i] = Double.parseDouble(s);
}
return nums;
}
public static void main(String[] args) {
String path = "d:/file4.txt";
double[] nums = writeToDat(path);
for(int i=0;iSystem.out.println(nums[i]);
}
}
}
package test;
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
nums[i] = Double.parseDouble(s);
}
return nums;
}
public static void main(String[] args) {
String path = "d:/file4.txt";
double[] nums = writeToDat(path);
for(int i=0;i
}
}
}