0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Java csv 値挿入

Last updated at Posted at 2024-01-14
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;

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

public class CSVInsertValue {

    public static void insertValueToCSV(String inputFilePath, String outputFilePath, int columnIndex, String newValue) {
        try (CSVReader reader = new CSVReader(new FileReader(inputFilePath));
             CSVWriter writer = new CSVWriter(new FileWriter(outputFilePath))) {

            // CSVファイルの読み込み
            List<String[]> rows = reader.readAll();

            // 特定の列に値を挿入
            for (String[] row : rows) {
                String[] newRow = new String[row.length + 1];
                // 挿入位置より前の要素をコピー
                System.arraycopy(row, 0, newRow, 0, columnIndex);
                //値を追加
                newRow[columnIndex] = newValue;
                // 挿入位置より後の要素をコピー
                System.arraycopy(row, columnIndex, newRow, columnIndex + 1, row.length - columnIndex);
                writer.writeNext(newRow);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String inputFilePath = "input.csv";
        String outputFilePath = "output.csv";
        int columnIndex = 2;  // 挿入する列のインデックス(0から始まる)
        String newValue = "新しい値";

        insertValueToCSV(inputFilePath, outputFilePath, columnIndex, newValue);
    }
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVInsertValue {

    public static void insertValueToCSV(String inputFilePath, String outputFilePath, int columnIndex, String newValue) {
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {

            String line;
            while ((line = reader.readLine()) != null) {
                List<String> values = new ArrayList<>();
                String[] columns = line.split(",");
                
                // 特定の列に値を挿入
                for (int i = 0; i < columns.length; i++) {
                    if (i == columnIndex) {
                        values.add(newValue);
                    }
                    values.add(columns[i]);
                }

                // 新しい行を書き込む
                writer.write(String.join(",", values));
                writer.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String inputFilePath = "input.csv";
        String outputFilePath = "output.csv";
        int columnIndex = 2;  // 挿入する列のインデックス(0から始まる)
        String newValue = "新しい値";

        insertValueToCSV(inputFilePath, outputFilePath, columnIndex, newValue);
    }
}

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?