import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadCSV {
public static void main(String[] args) throws IOException {
System.out.println("输入文件所在路径");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String path = br.readLine();
// 创建目标文件路径
File file = new File(path);
// 创建输出文件目录
File out = new File("c:\\Users\\Administrator\\Desktop\\out.csv");
BufferedWriter bw = new BufferedWriter(new FileWriter(out, true));
// 获取目标文件数量
String[] names = file.list();
// 输出目标文件数量
System.out.println(names.length);
// 定义两个字符串数组用于存储正背面图像温度值
// str1存储正面,str2存储背面
String[] str1 = new String[names.length/2+1];
String[] str2 = new String[names.length/2+1];
System.out.println("输入手机型号");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String name = br1.readLine();
str1[0]=name+",";
str2[0]=",";
// 利用循环,调用max方法,将正背面的温度值分别存入相应数组
for (int i = 0; i < names.length; i++) {
String s1 = max(path,String.valueOf(i + 1));
if (i % 2 == 0) {
str1[i/2+1] = s1;
}
str2[i/2+1] = s1;
}
// 将正背面的数据写入目标文件
for (int i = 0; i < str1.length; i++) {
bw.write(str1[i]);
}
bw.newLine();
for (int i = 0; i < str2.length; i++) {
bw.write(str2[i]);
}
bw.newLine();
bw.close();
}
// 获取每个文件中最大温度值方法
public static String max(String path ,String str) {
try {
// 关联要获取最大值的文件
File csv = new File(path+"\\" + str + ".csv");
// 读取流
BufferedReader br = new BufferedReader(new FileReader(csv));
String line = null;
float max = 0;
// 读取文件中数值,并获取其最大值
while ((line = br.readLine()) != null) {
String item[] = line.split(",");// CSV格式文件为逗号分隔符文件,这里根据逗号切分
for (int i = 0; i < item.length - 1; i++) {
if (Float.parseFloat(item[i]) > max) {
max = Float.parseFloat(item[i]);
}
}
}
// 输出其最大值
// System.out.println(max);
// 将最大值转化成字符形式返回
String s = String.valueOf(max) + ",";
return s;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
工作中遇到的个小问题,写了点代码解决了,大大提高了效率啊
|
|