Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Reading CSV File from Apache Commons IO Library

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 1.65k
    Comment on it

    Apache FileUtils class provides General file manipulation utilities, All the methods in this class that read a stream are buffered internally. This means that there is no cause to use a BufferedInputStream or BufferedReader.

    Below written code will read String from .csv file, the file name will be provided as a argument in main method..

    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.commons.io.IOUtils;
    
    public class ReadingCSVFileDemo {
    
    
        public static void main(String[] arg)throws IOException
    
        {
            List<String> existFileContent=new ArrayList<String>();
                String newvalue=null;
                FileReader reader = new FileReader(new File(arg[0]));
    
                List<String> lines = IOUtils.readLines(reader);
                    try{
                    for (String value : lines) {
    
                        if(value!=null){
                        //newvalue=value.split(",");
                        existFileContent.add(value);
                        }
                            }System.out.println("size of List"+ existFileContent.size());
                            System.out.println("element in a list"+ existFileContent);
    
            // below finally block we are closing a stream ignoring nulls and exceptions                
            }finally{IOUtils.closeQuietly(reader);}
            }
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: