既存の CSV ファイルにデータを追加する
CSV ファイルを作成したので、Java プログラムを変更して、既存のファイルを上書きするのではなく、追加のデータを追加するようにしましょう。
CSVAppendExample.java
という名前の新しい Java ファイルを作成します。
touch src/main/java/com/example/CSVAppendExample.java
- WebIDE エディタで
CSVAppendExample.java
を開き、次のコードを追加します。
package com.example;
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CSVAppendExample {
public static void main(String[] args) {
try {
// Create a CSVWriter in append mode by setting the second parameter of FileWriter to true
CSVWriter writer = new CSVWriter(new FileWriter("students.csv", true));
// Write additional data to the existing file
String[] student3 = {"Robert Johnson", "24", "[email protected]"};
writer.writeNext(student3);
String[] student4 = {"Maria Garcia", "23", "[email protected]"};
writer.writeNext(student4);
// Close the writer
writer.close();
System.out.println("Data appended to CSV file successfully!");
} catch (IOException e) {
System.out.println("Error appending to CSV file: " + e.getMessage());
e.printStackTrace();
}
}
}
このコードは前の例と似ていますが、2 つの重要な違いがあります。
FileWriter
の 2 番目のパラメータを true
に設定することで、追加モードで CSVWriter
を作成します。
- ファイルにすでにヘッダー行があるため、ヘッダー行を書き込みません。
- 既存の CSV ファイルにデータを追加するためにプログラムを実行します。
cd ~/project/csv-writer
mvn exec:java -Dexec.mainClass="com.example.CSVAppendExample"
- 現在の CSV ファイルの内容を確認しましょう。
cat students.csv
元のデータに新しく追加されたレコードが含まれているはずです。
"Name","Age","Email"
"John Doe","25","[email protected]"
"Jane Smith","22","[email protected]"
"Robert Johnson","24","[email protected]"
"Maria Garcia","23","[email protected]"
ご覧の通り、新しいデータは既存の内容を乱すことなくファイルの末尾に追加されました。
- カスタム区切り文字を使用して CSV ファイルを書き込む例をもう 1 つ作成しましょう。
touch src/main/java/com/example/CSVCustomExample.java
- WebIDE エディタで
CSVCustomExample.java
を開き、次のコードを追加します。
package com.example;
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CSVCustomExample {
public static void main(String[] args) {
try {
// Create a CSVWriter with semicolon as separator instead of comma
CSVWriter writer = new CSVWriter(new FileWriter("students_semicolon.csv"),
';',
CSVWriter.DEFAULT_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
// Write header line
String[] header = {"Name", "Age", "Email"};
writer.writeNext(header);
// Write data lines
String[] student1 = {"John Doe", "25", "[email protected]"};
writer.writeNext(student1);
String[] student2 = {"Jane Smith", "22", "[email protected]"};
writer.writeNext(student2);
// Close the writer
writer.close();
System.out.println("CSV file with semicolon separator created successfully!");
} catch (IOException e) {
System.out.println("Error writing to CSV file: " + e.getMessage());
e.printStackTrace();
}
}
}
この例では、カンマの代わりにセミコロンを区切り文字として使用して CSV ファイルを作成します。これは、カンマが小数点区切り文字として使用される一部のヨーロッパ諸国で一般的です。
- カスタム区切り文字の例を実行します。
cd ~/project/csv-writer
mvn exec:java -Dexec.mainClass="com.example.CSVCustomExample"
- 新しい CSV ファイルの内容を確認します。
cat students_semicolon.csv
セミコロンを区切り文字とする CSV ファイルが表示されるはずです。
"Name";"Age";"Email"
"John Doe";"25";"[email protected]"
"Jane Smith";"22";"[email protected]"