简体   繁体   中英

how can i put a filter on a CSV File stored in an Arraylist…Using Java

Iam reading a Csv file and want to put a filter on that arraylist in which the whole Csvfile is stored... I'm new to Java Can anyone Correct me where m going wrong...

public static void main(String[] args) throws IOException {

    String csvFile = "File.csv";
    BufferedReader br = null;
    String line ="";
    ArrayList<CSVRead> alist=new ArrayList<CSVRead>();
    try {
        br = new BufferedReader(new FileReader(csvFile));

       while((line = br.readLine()) != null)
       {
            String StoringArray[] = line.split(",");

            CSVRead Cs1 = new CSVRead((StoringArray[0]),(StoringArray[1]),(StoringArray[2]),(StoringArray[3]),(StoringArray[4]),(StoringArray[5]), (StoringArray[6]), (StoringArray[7]),(StoringArray[8]),(StoringArray[9]),(StoringArray[10]),(StoringArray[11]),(StoringArray[12]),(StoringArray[13]), (StoringArray[14]),(StoringArray[15]),(StoringArray[16]),(StoringArray[17]));

            alist.add(Cs1);                     
       } 
        alist.forEach(Cs1 -> System.out.println("\t" + Cs1));

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
        catch(IOException e){
            e.printStackTrace();

    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }}

Try it

public  static void main(String[] args) throws IOException {
String csvFile = "File.csv";
BufferedReader br = null;
String line ="";
ArrayList<String> alist=new ArrayList<String>();
try {
    br = new BufferedReader(new FileReader(csvFile));

   while((line = br.readLine()) != null)
   {
        String StoringArray[] = line.split(",");
        for (String i : StoringArray){
         alist.add(i);                     
         }
   } 
    System.out.println(alist); // print all list values 

} catch (FileNotFoundException e) {
    e.printStackTrace();
}
    catch(IOException e){
        e.printStackTrace();

} finally {
    if (br != null) {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM