0

I am trying to use lists for my first time, I have a txt file that I am searching in it about string then I must write the result of searching in new file.

Check the image attachedenter image description here

My task is to retrieve the two checked lines of the input file to the output files.

And this is my code:

import java.io.*;
import java.util.Scanner;

public class TestingReport1 {
public static void main(String[] args) throws Exception {

    File test = new File("E:\\test2.txt");
    File Result = new File("E:\\Result.txt");

    Scanner scanner = new Scanner(test);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if(line.contains("Visit Count")|| line.contains("Title")) {
           System.out.println(line);
        }
    }
}
}

What should I do?!

Edit: How can I write the result of this code into text file? Edit2:

Now using the following code:

    public static void main(String[] args) throws Exception {
    // TODO code application logic here

    File test = new File("E:\\test2.txt");
    FileOutputStream Result = new FileOutputStream("E:\\Result.txt");

    Scanner scanner = new Scanner(test);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if(line.contains("Visit Count")|| line.contains("Title")) {
           System.out.println(line);
           Files.write(Paths.get("E:\\Result.txt"), line.getBytes(), StandardOpenOption.APPEND);
       }
    }
}

I got the result back as Visit Count:1 , and I want to get this number back as integer, Is it possible?

Sarah Mohamed
  • 33
  • 1
  • 6
  • i mean that looks good? Just change the if statement to... `if(line.contains("Visit Count") || line.contains("Title")) { System.out.println(line);}` – 3kings Oct 22 '16 at 20:42
  • Title "Use Lists in Java" is really misleading and unclear - it could also refer to the List class in Java. Try to make your title specific. – Aleksandar Stefanović Oct 22 '16 at 20:42
  • @AleksandarStefanović I want to use lists to write back both lines in another file! – Sarah Mohamed Oct 22 '16 at 20:45
  • i think your little could simply talk about retrieving information from a File with scanner, because i don't know why List is the big man here, unless you want to store the information in a List and retrieve later, simple the tittle is big for it – Seek Addo Oct 22 '16 at 20:46
  • You don't use a list to write data to a file. Lists are only structures used in running programms. And for writing data to a file there are a lot of tutorials in the internet. Maybe searching something like "Java writing data to a file" helps you? – Mein Name Oct 22 '16 at 20:51
  • Sarah you're very close, you just need to change `System.out.println` to some `writeToFile`-style call, see here: http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java – Jameson Oct 22 '16 at 20:55

1 Answers1

1

Have a look at Files, especially readAllLines as well as write. Filter the input between those two method calls, that's it:

// Read.
List<String> input = Files.readAllLines(Paths.get("E:\\test2.txt"));
// Filter.
String output = input.stream()
        .filter(line -> line.matches("^(Title.*|Visit Count.*)"))
        .collect(Collectors.joining("\n"));
// Write.
Files.write(Paths.get("E:\\Result.txt"), output.getBytes());
beatngu13
  • 7,201
  • 6
  • 37
  • 66