0

I have used BufferedReader to read the content from the file but now I want to store the content of the file into an array list where my file contains some text from the pdf so now I wanted to add all those content to the array list

Here I have created array list but now I don't know how to proceed how to go next and then read from while loop so please help me

        ArrayList<String>YourList=new ArrayList<String>();
        FileReader fr = new 
        FileReader("D:\\PDFTOEXCEL\\Extractionfrompdf.txt");
        BufferedReader br = new BufferedReader(fr);

        String s;

        String keyword = arra.get(6);

        while ((s = br.readLine()) != null) {
            if (s.contains(keyword)) {
                s = s.replaceAll(keyword, " ");
                System.out.println(s);
            }
        }
Thelouras
  • 852
  • 1
  • 10
  • 30
Deo
  • 45
  • 8
  • do you want to add lines to an array list only with `keyword` ? – Ruslan Feb 16 '19 at 08:47
  • check this out, I think it will help you. [https://stackoverflow.com/questions/5343689/java-reading-a-file-into-an-arraylist](https://stackoverflow.com/questions/5343689/java-reading-a-file-into-an-arraylist) – abdulwasey20 Feb 16 '19 at 08:48
  • See I want to add That file Reader into an array list – Deo Feb 16 '19 at 08:51

1 Answers1

1

If you can use java8, there is Files.lines method:

String keyword = arra.get(6);
List<String> list = Files.lines(Paths.get(PATH_TO_FILE))   // returns stream of lines from source file
            .filter(s -> s.contains(keyword))   // filter by keyword
            .collect(Collectors.toList());      // add filtered lines to list

list will contain all lines from source file that has keyword

Ruslan
  • 6,090
  • 1
  • 21
  • 36
  • It says cannot find symbol method of (cannot find symbol symbol: method of(String) location: interface Path) – Deo Feb 16 '19 at 09:00
  • I have used but it is giving same error and i am using java 8 – Deo Feb 16 '19 at 09:02
  • @Deo sorry.. method `of` is from java11. Use `Paths.get` instead. Answer updated – Ruslan Feb 16 '19 at 09:05
  • It's giving a same error it suggesting me class after '.' – Deo Feb 16 '19 at 09:05
  • @Ruslan sometimes I wonder if we should just stick to the latest Java relase. What's the point in sticking with Java 8 examples? And sometimes I wonder how we should phrase answers to make them "understandable"... – LppEdd Feb 16 '19 at 09:05
  • @ Ruslan after using get I am getting error cannot find symbol symbol: method get(String) location: interface Path – Deo Feb 16 '19 at 09:08
  • @ Ruslan it is not adding any content present in the file – Deo Feb 16 '19 at 09:13
  • Has I said in my problem statement that I have to add the content which is been there in the text file to an array list and then I have to fetch that word in that array list – Deo Feb 16 '19 at 09:15
  • Yes Keywords are present in the text file – Deo Feb 16 '19 at 09:16
  • @Deo so.. try to comment `filter` line and see what the array list will contain – Ruslan Feb 16 '19 at 09:20
  • how to reuse that lamba @Ruslan?? – Deo Feb 18 '19 at 09:41
  • Because i have to use that keyword in database block so it's giving me an error that lamba must be declared either final or effectively final – Deo Feb 18 '19 at 09:43
  • @Deo right, you can't reassign the variable that is used in lambda expression. So in your case you can pass keyword directly like `.filter(s -> s.contains(arra.get(6)))` – Ruslan Feb 18 '19 at 09:51
  • I'm not sure what database part you mean..You can see more about effectively final https://stackoverflow.com/questions/34865383/variable-used-in-lambda-expression-should-be-final-or-effectively-final – Ruslan Feb 18 '19 at 09:57
  • k but I want to replace the keyword part now so can you help me – Deo Feb 18 '19 at 10:05
  • I want only the text extracted from that pdf part so i tried like this but it is saying that s in not defined String keyword = null; List list = new ArrayList<>(); Files.lines(Paths.get("D:\\PDFTOEXCEL\\Extractionfrompdf.txt")) // returns stream of lines from source file .filter(s-> s.contains(arra.get(6))) .forEach(list::add); keyword.replaceFirst(s," "); System.out.println(list); – Deo Feb 18 '19 at 10:07
  • `s` is defined within lambda (inside `filter` method only). You can't use it outside of it. Why do you need `replaceFirst` ? – Ruslan Feb 18 '19 at 10:15
  • I want only text which is been identified from that text file i.e :[Compte de facturation : 9355941544] my ouput is like this so now i want only right part not COmpte de...... – Deo Feb 18 '19 at 10:20
  • @Deo so i reccomend you to ask a new question. Describe what you are expecting and what do you have for now and post it – Ruslan Feb 18 '19 at 10:51