如何将Txt中的一组数据排序
首先是判断哪些需要的字符,然后进行逐行读取
public static String textTouse1(String str) {
if (str.charAt(2) != ' ') {
str = str.substring(2, 4);
} else {
str = str.substring(3, 4);
}
return str;
}
然后是逐行读取
public static String txt2String(File file) {
StringBuilder result = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {
// result.append(System.lineSeparator()+s);//System.lineSeparator()是用来加回车的
xi[num] = Integer.parseInt(textTouse1(s));
yi[num] = Double.parseDouble(textTouse2(s));
num++;
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();// 以字符串形式返回给主函数
}
然后在主函数中进行引用,进行一系列处理
File file = new File("E:/Programs/A.txt");/* 文件名 */
// 开始排序
txt2String(file);// 读取文件中的数据
linearregression(xi, yi);
排序完成