Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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
        {
            // 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 ();
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: change string to char array 
Java :: how to get color from resource android 
Java :: hadoop-master error java_home is not set and could not be found 
Java :: find maximum in array java 
Java :: wrapper classes in java 
Java :: Java How to use Set? 
Java :: java android edit text set value 
Java :: change color of drawable android studio 
Java :: java interface attributes 
Java :: how to convert primitive int to Integer in java 
Java :: how to format a double in java to 2 decimal places 
Java :: java string length validation regex 
Java :: how to center a window in java 
Java :: how to delete character in string java 
Java :: Converting String Array to an Integer Array 
Java :: java repository sql find not in list 
Java :: array arraylist java 
Java :: polymorphism in oop 
Java :: check stack empty java 
Java :: java class jar determine 
Java :: reverseString 
Java :: display two dimension array java 
Java :: java list to array 
Java :: apache csv get headers 
Java :: how to make arraylist character 
Java :: what is lambda expression in java 
Java :: is java compiled or interpreted 
Java :: Powermockito static method call 
Java :: convert java to python 
Java :: java string from byte array 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =