How to read a file line by line with Java Stream











up vote
4
down vote

favorite
1












I'm trying to read a long file line by line and trying in the same time to extract some information from the line.



Here in an example of what i'm doing:



import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Stream;

public class ReadFile_Files_Lines {

public static void main(String pArgs) throws IOException {
String fileName = "c:\temp\sample-1GB.txt";
File file = new File(fileName);

try (Stream<String> linesStream = Files.lines(file.toPath())) {
linesStream.forEach(line -> {
System.out.println(line);
});
}
}
}


One line in my file is devided into three part :



10 1010101 15


I'm looking to read those three informations every time.
Like :



String str1 = line[0];
String str2 = line[1];
String str3 = line[2];


The solution i'm looking for will be better if i should not convert the stream to a collection.
I will use those 3 String to create a graph data structure, something like :



createGraphe(str1,str2,str3);`


I know that i can send the full String, but as i'm learning Stream I'm interested to know how to extract those informations.



Thank you.










share|improve this question




















  • 2




    Your question is not clear to me. What do you want to do with the three elements? Don't store it if you don't want to
    – user7
    Nov 19 at 12:03










  • What about Files.readAllLines(Paths.get("c:\temp\sample-1GB.txt")) combined with String.split(" "); for the values of a single line?
    – deHaar
    Nov 19 at 12:03










  • @deHaar This will have an impact of loading the full file content into memory.
    – user7
    Nov 19 at 12:05










  • @user7 I know... Maybe it is not that much... ;-) The word long is not really specific. But yes, there may be far better ideas.
    – deHaar
    Nov 19 at 12:06










  • @deHaar From the filename, I'd guess about 1GB ;)
    – daniu
    Nov 19 at 12:07















up vote
4
down vote

favorite
1












I'm trying to read a long file line by line and trying in the same time to extract some information from the line.



Here in an example of what i'm doing:



import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Stream;

public class ReadFile_Files_Lines {

public static void main(String pArgs) throws IOException {
String fileName = "c:\temp\sample-1GB.txt";
File file = new File(fileName);

try (Stream<String> linesStream = Files.lines(file.toPath())) {
linesStream.forEach(line -> {
System.out.println(line);
});
}
}
}


One line in my file is devided into three part :



10 1010101 15


I'm looking to read those three informations every time.
Like :



String str1 = line[0];
String str2 = line[1];
String str3 = line[2];


The solution i'm looking for will be better if i should not convert the stream to a collection.
I will use those 3 String to create a graph data structure, something like :



createGraphe(str1,str2,str3);`


I know that i can send the full String, but as i'm learning Stream I'm interested to know how to extract those informations.



Thank you.










share|improve this question




















  • 2




    Your question is not clear to me. What do you want to do with the three elements? Don't store it if you don't want to
    – user7
    Nov 19 at 12:03










  • What about Files.readAllLines(Paths.get("c:\temp\sample-1GB.txt")) combined with String.split(" "); for the values of a single line?
    – deHaar
    Nov 19 at 12:03










  • @deHaar This will have an impact of loading the full file content into memory.
    – user7
    Nov 19 at 12:05










  • @user7 I know... Maybe it is not that much... ;-) The word long is not really specific. But yes, there may be far better ideas.
    – deHaar
    Nov 19 at 12:06










  • @deHaar From the filename, I'd guess about 1GB ;)
    – daniu
    Nov 19 at 12:07













up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





I'm trying to read a long file line by line and trying in the same time to extract some information from the line.



Here in an example of what i'm doing:



import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Stream;

public class ReadFile_Files_Lines {

public static void main(String pArgs) throws IOException {
String fileName = "c:\temp\sample-1GB.txt";
File file = new File(fileName);

try (Stream<String> linesStream = Files.lines(file.toPath())) {
linesStream.forEach(line -> {
System.out.println(line);
});
}
}
}


One line in my file is devided into three part :



10 1010101 15


I'm looking to read those three informations every time.
Like :



String str1 = line[0];
String str2 = line[1];
String str3 = line[2];


The solution i'm looking for will be better if i should not convert the stream to a collection.
I will use those 3 String to create a graph data structure, something like :



createGraphe(str1,str2,str3);`


I know that i can send the full String, but as i'm learning Stream I'm interested to know how to extract those informations.



Thank you.










share|improve this question















I'm trying to read a long file line by line and trying in the same time to extract some information from the line.



Here in an example of what i'm doing:



import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Stream;

public class ReadFile_Files_Lines {

public static void main(String pArgs) throws IOException {
String fileName = "c:\temp\sample-1GB.txt";
File file = new File(fileName);

try (Stream<String> linesStream = Files.lines(file.toPath())) {
linesStream.forEach(line -> {
System.out.println(line);
});
}
}
}


One line in my file is devided into three part :



10 1010101 15


I'm looking to read those three informations every time.
Like :



String str1 = line[0];
String str2 = line[1];
String str3 = line[2];


The solution i'm looking for will be better if i should not convert the stream to a collection.
I will use those 3 String to create a graph data structure, something like :



createGraphe(str1,str2,str3);`


I know that i can send the full String, but as i'm learning Stream I'm interested to know how to extract those informations.



Thank you.







java string java-8 stream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 12:12

























asked Nov 19 at 12:00









zak zak

320111




320111








  • 2




    Your question is not clear to me. What do you want to do with the three elements? Don't store it if you don't want to
    – user7
    Nov 19 at 12:03










  • What about Files.readAllLines(Paths.get("c:\temp\sample-1GB.txt")) combined with String.split(" "); for the values of a single line?
    – deHaar
    Nov 19 at 12:03










  • @deHaar This will have an impact of loading the full file content into memory.
    – user7
    Nov 19 at 12:05










  • @user7 I know... Maybe it is not that much... ;-) The word long is not really specific. But yes, there may be far better ideas.
    – deHaar
    Nov 19 at 12:06










  • @deHaar From the filename, I'd guess about 1GB ;)
    – daniu
    Nov 19 at 12:07














  • 2




    Your question is not clear to me. What do you want to do with the three elements? Don't store it if you don't want to
    – user7
    Nov 19 at 12:03










  • What about Files.readAllLines(Paths.get("c:\temp\sample-1GB.txt")) combined with String.split(" "); for the values of a single line?
    – deHaar
    Nov 19 at 12:03










  • @deHaar This will have an impact of loading the full file content into memory.
    – user7
    Nov 19 at 12:05










  • @user7 I know... Maybe it is not that much... ;-) The word long is not really specific. But yes, there may be far better ideas.
    – deHaar
    Nov 19 at 12:06










  • @deHaar From the filename, I'd guess about 1GB ;)
    – daniu
    Nov 19 at 12:07








2




2




Your question is not clear to me. What do you want to do with the three elements? Don't store it if you don't want to
– user7
Nov 19 at 12:03




Your question is not clear to me. What do you want to do with the three elements? Don't store it if you don't want to
– user7
Nov 19 at 12:03












What about Files.readAllLines(Paths.get("c:\temp\sample-1GB.txt")) combined with String.split(" "); for the values of a single line?
– deHaar
Nov 19 at 12:03




What about Files.readAllLines(Paths.get("c:\temp\sample-1GB.txt")) combined with String.split(" "); for the values of a single line?
– deHaar
Nov 19 at 12:03












@deHaar This will have an impact of loading the full file content into memory.
– user7
Nov 19 at 12:05




@deHaar This will have an impact of loading the full file content into memory.
– user7
Nov 19 at 12:05












@user7 I know... Maybe it is not that much... ;-) The word long is not really specific. But yes, there may be far better ideas.
– deHaar
Nov 19 at 12:06




@user7 I know... Maybe it is not that much... ;-) The word long is not really specific. But yes, there may be far better ideas.
– deHaar
Nov 19 at 12:06












@deHaar From the filename, I'd guess about 1GB ;)
– daniu
Nov 19 at 12:07




@deHaar From the filename, I'd guess about 1GB ;)
– daniu
Nov 19 at 12:07












3 Answers
3






active

oldest

votes

















up vote
5
down vote



accepted










You can map to an array by splitting each line, then call the method you want.



Files.lines(filePath)
.map(l -> l.split(" "))
.forEach(a -> createGraphe(a[0], a[1], a[2]));





share|improve this answer





















  • Thank you. it's what i was looking, the spilt methd. can you please confirm that this solution is better than converting the stream to a Collection of String.
    – zak zak
    Nov 19 at 12:24












  • @zakzak That depends on how your graph creation function looks like, but I guess it's better since it doesn't have to store the intermediate String.
    – daniu
    Nov 19 at 12:25


















up vote
2
down vote













The method lines() you are using already does what you expect it to do.



Java 8 has added a new method called lines() in Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read first 100 lines then rest of the lines will not be loaded into memory, which results in better performance.



This is slightly different than Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream e.g. forEach(), count() etc. By using count() method you can actually count a number of lines in files or number of empty lines by filtering empty lines.



Reference: https://javarevisited.blogspot.com/2015/07/3-ways-to-read-file-line-by-line-in.html






share|improve this answer





















  • Thank you, i will take time to read your awnser.
    – zak zak
    Nov 19 at 12:24


















up vote
0
down vote













Since you want to solve this problem and want to learn how streams can be useful in this situation





  1. Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))


  2. Reading this file line by line : lines.map(line -> line.split(pattern)) , by splitting the line and you will get three sections from the line


  3. Passing the arguments obtained into the function : forEach(arg -> createGraphe(arg[0], arg[1], arg[2]);


I hope this is pretty elaborate for your answer if you want to achieve this






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374186%2fhow-to-read-a-file-line-by-line-with-java-stream%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    You can map to an array by splitting each line, then call the method you want.



    Files.lines(filePath)
    .map(l -> l.split(" "))
    .forEach(a -> createGraphe(a[0], a[1], a[2]));





    share|improve this answer





















    • Thank you. it's what i was looking, the spilt methd. can you please confirm that this solution is better than converting the stream to a Collection of String.
      – zak zak
      Nov 19 at 12:24












    • @zakzak That depends on how your graph creation function looks like, but I guess it's better since it doesn't have to store the intermediate String.
      – daniu
      Nov 19 at 12:25















    up vote
    5
    down vote



    accepted










    You can map to an array by splitting each line, then call the method you want.



    Files.lines(filePath)
    .map(l -> l.split(" "))
    .forEach(a -> createGraphe(a[0], a[1], a[2]));





    share|improve this answer





















    • Thank you. it's what i was looking, the spilt methd. can you please confirm that this solution is better than converting the stream to a Collection of String.
      – zak zak
      Nov 19 at 12:24












    • @zakzak That depends on how your graph creation function looks like, but I guess it's better since it doesn't have to store the intermediate String.
      – daniu
      Nov 19 at 12:25













    up vote
    5
    down vote



    accepted







    up vote
    5
    down vote



    accepted






    You can map to an array by splitting each line, then call the method you want.



    Files.lines(filePath)
    .map(l -> l.split(" "))
    .forEach(a -> createGraphe(a[0], a[1], a[2]));





    share|improve this answer












    You can map to an array by splitting each line, then call the method you want.



    Files.lines(filePath)
    .map(l -> l.split(" "))
    .forEach(a -> createGraphe(a[0], a[1], a[2]));






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 12:19









    daniu

    6,57521634




    6,57521634












    • Thank you. it's what i was looking, the spilt methd. can you please confirm that this solution is better than converting the stream to a Collection of String.
      – zak zak
      Nov 19 at 12:24












    • @zakzak That depends on how your graph creation function looks like, but I guess it's better since it doesn't have to store the intermediate String.
      – daniu
      Nov 19 at 12:25


















    • Thank you. it's what i was looking, the spilt methd. can you please confirm that this solution is better than converting the stream to a Collection of String.
      – zak zak
      Nov 19 at 12:24












    • @zakzak That depends on how your graph creation function looks like, but I guess it's better since it doesn't have to store the intermediate String.
      – daniu
      Nov 19 at 12:25
















    Thank you. it's what i was looking, the spilt methd. can you please confirm that this solution is better than converting the stream to a Collection of String.
    – zak zak
    Nov 19 at 12:24






    Thank you. it's what i was looking, the spilt methd. can you please confirm that this solution is better than converting the stream to a Collection of String.
    – zak zak
    Nov 19 at 12:24














    @zakzak That depends on how your graph creation function looks like, but I guess it's better since it doesn't have to store the intermediate String.
    – daniu
    Nov 19 at 12:25




    @zakzak That depends on how your graph creation function looks like, but I guess it's better since it doesn't have to store the intermediate String.
    – daniu
    Nov 19 at 12:25












    up vote
    2
    down vote













    The method lines() you are using already does what you expect it to do.



    Java 8 has added a new method called lines() in Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read first 100 lines then rest of the lines will not be loaded into memory, which results in better performance.



    This is slightly different than Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream e.g. forEach(), count() etc. By using count() method you can actually count a number of lines in files or number of empty lines by filtering empty lines.



    Reference: https://javarevisited.blogspot.com/2015/07/3-ways-to-read-file-line-by-line-in.html






    share|improve this answer





















    • Thank you, i will take time to read your awnser.
      – zak zak
      Nov 19 at 12:24















    up vote
    2
    down vote













    The method lines() you are using already does what you expect it to do.



    Java 8 has added a new method called lines() in Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read first 100 lines then rest of the lines will not be loaded into memory, which results in better performance.



    This is slightly different than Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream e.g. forEach(), count() etc. By using count() method you can actually count a number of lines in files or number of empty lines by filtering empty lines.



    Reference: https://javarevisited.blogspot.com/2015/07/3-ways-to-read-file-line-by-line-in.html






    share|improve this answer





















    • Thank you, i will take time to read your awnser.
      – zak zak
      Nov 19 at 12:24













    up vote
    2
    down vote










    up vote
    2
    down vote









    The method lines() you are using already does what you expect it to do.



    Java 8 has added a new method called lines() in Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read first 100 lines then rest of the lines will not be loaded into memory, which results in better performance.



    This is slightly different than Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream e.g. forEach(), count() etc. By using count() method you can actually count a number of lines in files or number of empty lines by filtering empty lines.



    Reference: https://javarevisited.blogspot.com/2015/07/3-ways-to-read-file-line-by-line-in.html






    share|improve this answer












    The method lines() you are using already does what you expect it to do.



    Java 8 has added a new method called lines() in Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read first 100 lines then rest of the lines will not be loaded into memory, which results in better performance.



    This is slightly different than Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream e.g. forEach(), count() etc. By using count() method you can actually count a number of lines in files or number of empty lines by filtering empty lines.



    Reference: https://javarevisited.blogspot.com/2015/07/3-ways-to-read-file-line-by-line-in.html







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 12:10









    Pushpesh Kumar Rajwanshi

    2,7231821




    2,7231821












    • Thank you, i will take time to read your awnser.
      – zak zak
      Nov 19 at 12:24


















    • Thank you, i will take time to read your awnser.
      – zak zak
      Nov 19 at 12:24
















    Thank you, i will take time to read your awnser.
    – zak zak
    Nov 19 at 12:24




    Thank you, i will take time to read your awnser.
    – zak zak
    Nov 19 at 12:24










    up vote
    0
    down vote













    Since you want to solve this problem and want to learn how streams can be useful in this situation





    1. Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))


    2. Reading this file line by line : lines.map(line -> line.split(pattern)) , by splitting the line and you will get three sections from the line


    3. Passing the arguments obtained into the function : forEach(arg -> createGraphe(arg[0], arg[1], arg[2]);


    I hope this is pretty elaborate for your answer if you want to achieve this






    share|improve this answer

























      up vote
      0
      down vote













      Since you want to solve this problem and want to learn how streams can be useful in this situation





      1. Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))


      2. Reading this file line by line : lines.map(line -> line.split(pattern)) , by splitting the line and you will get three sections from the line


      3. Passing the arguments obtained into the function : forEach(arg -> createGraphe(arg[0], arg[1], arg[2]);


      I hope this is pretty elaborate for your answer if you want to achieve this






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Since you want to solve this problem and want to learn how streams can be useful in this situation





        1. Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))


        2. Reading this file line by line : lines.map(line -> line.split(pattern)) , by splitting the line and you will get three sections from the line


        3. Passing the arguments obtained into the function : forEach(arg -> createGraphe(arg[0], arg[1], arg[2]);


        I hope this is pretty elaborate for your answer if you want to achieve this






        share|improve this answer












        Since you want to solve this problem and want to learn how streams can be useful in this situation





        1. Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))


        2. Reading this file line by line : lines.map(line -> line.split(pattern)) , by splitting the line and you will get three sections from the line


        3. Passing the arguments obtained into the function : forEach(arg -> createGraphe(arg[0], arg[1], arg[2]);


        I hope this is pretty elaborate for your answer if you want to achieve this







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 12:30









        Anshul

        2118




        2118






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374186%2fhow-to-read-a-file-line-by-line-with-java-stream%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$