Read CSV file column by column

前端 未结 8 1690
萌比男神i
萌比男神i 2020-11-29 04:31

I want to read specific columns from a multi column csv file and print those columns in other csv file using Java. Any help please? Following is my code to print each token

8条回答
  •  眼角桃花
    2020-11-29 05:07

    Finds all files in folder and write that data to ArrayList row.

    Initialize

    ArrayList> row=new ArrayList>();
    BufferedReader br=null;
    

    For Accessing row

    for(ArrayList data:row){
    data.get(col no); 
    }
    or row.get(0).get(0) // getting first row first col
    

    Functions that reads all files from folders and concatenate them row.

    static void readData(){
    String path="C:\\Users\\Galaxy Computers\\Desktop\\Java project\\Nasdaq\\";
    File files=new File(path);
    String[] list=files.list();
    
    try {
            String sCurrentLine;
           char check;
           for(String filename:list){ 
            br = new BufferedReader(new FileReader(path+filename));
            br.readLine();//If file contains uneccessary first line.
            while ((sCurrentLine = br.readLine()) != null) {
    
               row.add(splitLine(sCurrentLine));
            }
            }
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        } 
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
    
       static ArrayList splitLine(String line){
       String[] ar=line.split(",");
       ArrayList d=new ArrayList();
       for(String data:ar){
        d.add(data);
       }
    
       return d; 
       } 
    

提交回复
热议问题