发布网友 发布时间:2022-04-22 04:51
共2个回答
热心网友 时间:2023-09-09 01:44
可以通过Java的IO流实现txt文本的读取,然后用readline实现按行读取。具体代码如下:
package test;热心网友 时间:2023-09-09 01:44
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<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:/file4.txt";
double[] nums = writeToDat(path);
for(int i=0;i<nums.length;i++){
System.out.println(nums[i]);
}
}
}