1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

csvファイル出力

Posted at

csvファイル出力

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class CSVWriter {
    public static void writeDataToCSV(List<String[]> data, String filePath) {
        try (FileWriter writer = new FileWriter(filePath)) {
            for (String[] row : data) {
                StringBuilder csvRow = new StringBuilder();
                for (String value : row) {
                    // 文字列中にカンマや改行が含まれる場合は適切にエスケープする必要があります
                    String escapedValue = escapeSpecialCharacters(value);
                    csvRow.append(escapedValue).append(",");
                }
                csvRow.deleteCharAt(csvRow.length() - 1); // 最後のカンマを削除
                csvRow.append("\n"); // 改行を追加
                writer.write(csvRow.toString());
            }
            System.out.println("CSVファイルにデータを書き込みました: " + filePath);
        } catch (IOException e) {
            System.err.println("CSVファイルの書き込みエラー: " + e.getMessage());
        }
    }

    private static String escapeSpecialCharacters(String value) {
        // 文字列中のカンマや改行をエスケープする必要がある場合に実装するロジック
        // 例: カンマが含まれる場合にはダブルクォートで囲むなど
        // この例ではエスケープ処理は省略しています
        return value;
    }
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?