Java -Spalte aus CSV löschen
package work.basil.example;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class App
{
public static void main ( String[] args )
{
System.out.println ( "Hello World!" );
App app = new App ();
app.demo ();
}
private void demo ( )
{
try
{
// Read CSV file.
Path pathInput = Paths.get ( "/Users/basilbourque/input.csv" );
Path pathOutput = Paths.get ( "/Users/basilbourque/output.csv" );
try (
final BufferedReader reader = Files.newBufferedReader ( pathInput , StandardCharsets.UTF_8 ) ;
final CSVPrinter printer = CSVFormat.RFC4180.withHeader ( "ID" , "name1" , "name3" ).print ( pathOutput , StandardCharsets.UTF_8 ) ;
)
{
Iterable < CSVRecord > records = CSVFormat.RFC4180.withFirstRecordAsHeader ().parse ( reader );
// We expect these headers: ID,name1,name2,name3
for ( CSVRecord record : records )
{
// Read.
Integer id = Integer.valueOf ( record.get ( "ID" ) );
String name1 = record.get ( "name1" );
String name2 = record.get ( "name2" );
String name3 = record.get ( "name3" );
System.out.println ( "id: " + id + " | name1: " + name1 + " | name2: " + name2 + " | name3: " + name3 );
// Write.
printer.printRecord ( id , name1 , name3 );
}
}
} catch ( InvalidPathException e )
{
e.printStackTrace ();
} catch ( IOException e )
{
e.printStackTrace ();
}
}
}
Dangerous Dogfish