1

my CSV file looks like this:

Id;date;code;type;category;name;position;formula
10;;;2010-02-01;;000010;P;W;NormalDays;10;#formelTest
55;;;2050-05-02;;000055;D;C;SpecificDays;55;#formelTest2
60;;;2301-08-03;;000060;A;C;NotNormalDays;60;#formelBlablblabla
75;;;2012-01-08;;000075;P;W;VulgaryDays;75;@formellbalbalbalbababa

I would like to read from it lines by line only from only first column id and last column formula. How can I do this? Thanks for help.

The code:

File file = new File("Filename.csv");
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
for (String line : lines)
   { 
     String[] array = line.split(",");
     System.out.println(array[0]);
   }
6
  • Do you have a sample code of what have you do? Commented Jul 14, 2017 at 10:55
  • File file = new File("Filename.csv"); List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8); for (String line : lines) { String[] array = line.split(","); System.out.println(array[0]); } Commented Jul 14, 2017 at 10:57
  • Simple read file. I dont have idea how can I read specific columns... Commented Jul 14, 2017 at 10:57
  • 1
    It would be better to edit your question and add the code, for StackOverflow question guidelines Commented Jul 14, 2017 at 10:58
  • 1
    And why are you creating an array of lines inside the loop? Commented Jul 14, 2017 at 11:02

2 Answers 2

3

Completing your code:

File file = new File("Filename.csv"); 
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8); 
for (String line : lines) { 
   String[] array = line.split(";"); 
   System.out.println(array[0]+" "+array[array.length-1]); 
}

You will notice that I changed the comma to a semi-colon as the split separator and added a bit to the println, which will be the last item in your split array.

I hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it helps :) Thanks
0
String filename = "matches.csv";
    File  file = new File(filename);
    try {
        Scanner sc = new Scanner(file);
        while (sc.hasNext()) {
            String data = sc.next();
            String[] values = data.split(",");
            System.out.println(data);
        }
        sc.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.