3

I am currently in the process of studying Java. I am interested in how to read text files the most efficient way. I read that it is possible to use a Stream object (is "object" in this context correct?) to do that and gave it a shot already. I am able to print out every line from the text document using the following code:

private static void ReadFile(String filePath) {
    Stream<String> readFileStream = null;
    try {
        readFileStream = Files.lines(Paths.get(filePath));

    } catch (IOException e) {
        e.printStackTrace();
    }
    readFileStream.forEach(System.out::println);
}

I would like to do something using each line. For example I would like to put a bunch of names into my text document and have Java print out "Hello, NAME" for each name. How do I do that? How do I access a line individually, do I have to put the list into an array first in order to iterate through it?

Is it correct to say that we created a Stream object named readFileStream? I want to make sure that my terminology is right. Moreover, why do we add after Stream? I know that <> are used for lists but I do not understand them in this context.

Thanks in advance.

Inialk
  • 73
  • 1
  • 3
  • 1
    http://stackoverflow.com/a/13741943/5703530 If I'm not mistaken, you cannot modify a file while you are reading it. Here is an example on how to store lines and modify them later. And as for this '<>' take a look at generics https://docs.oracle.com/javase/tutorial/java/generics/types.html – Merve Sahin Oct 27 '16 at 21:46

4 Answers4

4

It is simple to access the lines individually: you just need to expound this expression: readFileStream.forEach(System.out::println);

And turn it into something like:

readFileStream.forEach( line -> {
            System.out.println(line.toUpperCase());
        });

In my illustration I turn each line to upper case. You can do all kind of processing once you have the line individually.

alainlompo
  • 4,414
  • 4
  • 32
  • 41
1

This will read one line at a time

    File file = new File("text.txt");

    try(BufferedReader br = new BufferedReader(new FileReader(file))){
        String line;
        while((line = br.readLine())!=null){
            System.out.println(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
Corey Bernard
  • 48
  • 1
  • 1
  • 7
0
Files.lines(Paths.get(filePath))
   .filter(line -> line.startsWith(“J“))
   .forEach(line -> System.out.printf(“Hello, %s%n“, line));

So you were almost there. Read up on Streams. Streams are a oneshot iteration, so a variable is superfluous. I have added a filtering, but one could as well sort and such.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

There is a good library provided by Apache : commons-io. Latest version is 2.5.

Here is the maven dependency for it:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

This library has very good methods to read files.

Check this method:

List<String> listOfLines = FileUtils.readLines(file, encoding);

It reads the file into a list of String.

Abhay Jain
  • 86
  • 5