java ceka drop column code example
Example: java delete column from csv
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
{
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 );
for ( CSVRecord record : records )
{
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 );
printer.printRecord ( id , name1 , name3 );
}
}
} catch ( InvalidPathException e )
{
e.printStackTrace ();
} catch ( IOException e )
{
e.printStackTrace ();
}
}
}