我有一个任务要制作一个程序,该程序从3个csv文件中计算总和,首先是每个文件,然后最后将所有总和相加并打印出来。我已经提出了解决方案,但是在运行程序时它不会打印任何内容。检查工作目录及其是否良好。
public class MoneyCounter {
public void count(Path path) throws Exception {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
int[] amount = {0, 0, 0};
if (file.endsWith("csv")) {
BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file)));
while(reader.ready()){
String line = reader.readLine();
if(line.split(" ")[1].equals("EUR")) amount[0] += Integer.parseInt(line.split(" ")[2]);
else if(line.split(" ")[1].equals("HRK")) amount[1] += Integer.parseInt(line.split(" ")[2]);
else amount[2] += Integer.parseInt(line.split(" ")[2]);
}
System.out.println("\"" + file + "\"" + " found");
System.out.println("\t Totals by currencies: ");
System.out.println("Money in all countries: ");
if(amount[0] > 0 ) System.out.println("\t EUR: " + amount[0]);
if(amount[1] > 0 ) System.out.println("\t HRK: " + amount[1]);
if(amount[2] > 0 ) System.out.println("\t USD: " + amount[2]);
}
return FileVisitResult.CONTINUE;
}
});
}
public static void main(String[] args) throws Exception {
MoneyCounter moneyCounter = new MoneyCounter();
moneyCounter.count(new File("path.csv").toPath());
}
}
答案 0 :(得分:1)
Path.endsWith(String)并没有您所想的那样,它检查路径的最后一个“元素”是否等于给定的字符串。
例如,路径“ foo / bar”以“ foo / bar”和“ bar”结尾
它不以“ r”或“ / bar”结尾
尝试改为致电file.toString().endsWith("csv")
。
答案 1 :(得分:0)
不满足endWith条件。 检查下面的工作代码。
package files;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class Test {
public static void main(String[] args) throws IOException {
Test.count(new File("d:\\Files").toPath());
}
private static void count(Path path) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
int[] amount = { 0, 0, 0 };
BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file)));
while (reader.ready()) {
String line = reader.readLine();
if (line.split(" ")[1].equals("EUR"))
amount[0] += Integer.parseInt(line.split(" ")[2]);
else if (line.split(" ")[1].equals("HRK"))
amount[1] += Integer.parseInt(line.split(" ")[2]);
else
amount[2] += Integer.parseInt(line.split(" ")[2]);
}
System.out.println("\"" + file + "\"" + " found");
System.out.println("\t Totals by currencies: ");
System.out.println("Money in all countries: ");
if (amount[0] > 0)
System.out.println("\t EUR: " + amount[0]);
if (amount[1] > 0)
System.out.println("\t HRK: " + amount[1]);
if (amount[2] > 0)
System.out.println("\t USD: " + amount[2]);
return FileVisitResult.CONTINUE;
}
});
}
}
D://Files/a.csv下有csv文件,其中包含以下数据。
a.csv
10欧元
b.csv
HRK 20
c.csv
30美元
结果:
货币总计:
所有国家/地区的钱:
EUR: 10
HRK: 20
货币总计:
所有国家/地区的钱:
HRK: 20
货币总计:
所有国家/地区的钱:
USD: 10