In Java, you can delete a column from a CSV (Comma Separated Values) file by reading the file line by line, parsing the values on each line, and writing the values to a new file without the column you want to delete.
Here is an example of how to delete a column from a CSV file in Java:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Set the input and output file paths
Path inputFilePath = Paths.get("C:\\path\\to\\input.csv");
Path outputFilePath = Paths.get("C:\\path\\to\\output.csv");
// Set the column index to delete (0-based)
int columnIndex = 2;
try (BufferedReader reader = Files.newBufferedReader(inputFilePath);
BufferedWriter writer = Files.newBufferedWriter(outputFilePath)) {
// Read the file line by line
String line;
while ((line = reader.readLine()) != null) {
// Split the line into values
String[] values = line.split(",");
// Delete the column by copying the values to a new array
String[] newValues = new String[values.length - 1];
for (int i = 0, j = 0; i < values.length; i++) {
if (i != columnIndex) {
newValues[j++] = values[i];
}
}
// Write the new values to the output file
writer.write(String.join(",", newValues));
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This code reads the input CSV file line by line, splits each line into values using the split method, and then copies the values to a new array, skipping the value at the specified column index. Finally, it writes the new values to the output CSV file, using the String.join method to concatenate the values with a comma separator.