【问题标题】:Returning a CSV file in java using "public File"使用“公共文件”在java中返回一个CSV文件
【发布时间】:2017-07-25 03:35:43
【问题描述】:

我必须实现一个 digitSquareSum() 方法,我已经实现如下:

 public int digitSquareSum(int n) {
    int total;
    if (n < 10) {
        total = (int) Math.pow(n, 2);
        return total;
     } else {
        total = (int) ((Math.pow((n %10), 2)) + digitSquareSum(n/10));
        return total;
     }
}

现在,我想创建一个方法,该方法将为 CSV 文件返回一个 Java File 对象,该文件由 1-500 的 digitSquareSum 填充,格式如下:

public File questionOne() throws Exception {
    //code here
}

所以文件应该看起来像
1,1
2,4
.
.
500,25

我该如何解决这个问题?

【问题讨论】:

标签: java csv


【解决方案1】:

给你:

public File questionOne() throws Exception {
    File file = new File("C:\\your\\path\\here", "your_file_name.csv");
    if (!file.exists()) {
        file.createNewFile();
    }
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));
    for (int i = 1; i <= 500; i++) {
        bw.append(i + "," + digitSquareSum(i) + "\n");
    }
    bw.close();
    return file;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多