Java- CSV / Delete column in csv file

前端 未结 2 466
北海茫月
北海茫月 2021-01-27 00:45

I try to delete a column in csv file in Java.

for example, I have this csv file

ID   name1   name2   name3
1    hello   hell    hel
2    try     tr               


        
相关标签:
2条回答
  • 2021-01-27 00:57

    The only way to delete a column in a CSV file is to remove the header and the information of this column in the whole file, that is for each row of the file. Even if you use a third party library it will do this internally.

    0 讨论(0)
  • 2021-01-27 01:06

    Read in each column value of each row, and write out only the desired columns while skipping the undesired column value.

    Example app using Apache Commons CSV library

    Here is an example app demonstrating the use of Apache Commons CSV to read the input file, then write to the output file, skipping over the unwanted column values.

    Notice the use of try-with-resources syntax to automatically close our file reader and writer objects. See Tutorial by Oracle.

    RFC 4180 refers to the written standard defining the Comma-Separated Values (CSV) format.

    Make a file named input.csv.

    ID,name1,name2,name3
    1,hello,hell,hel
    2,try,tr,t
    3,browser,ro,br
    

    Java app.

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

    Console output.

    id: 1 | name1: hello | name2: hell | name3: hel

    id: 2 | name1: try | name2: tr | name3: t

    id: 3 | name1: browser | name2: ro | name3: br

    Resulting file named output.csv.

    ID,name1,name3
    1,hello,hel
    2,try,t
    3,browser,br
    
    0 讨论(0)
提交回复
热议问题