How to read a file line by line with Java Stream
up vote
4
down vote
favorite
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
|
show 4 more comments
up vote
4
down vote
favorite
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
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 aboutFiles.readAllLines(Paths.get("c:\temp\sample-1GB.txt"))
combined withString.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
|
show 4 more comments
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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
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
java string java-8 stream
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 aboutFiles.readAllLines(Paths.get("c:\temp\sample-1GB.txt"))
combined withString.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
|
show 4 more comments
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 aboutFiles.readAllLines(Paths.get("c:\temp\sample-1GB.txt"))
combined withString.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
|
show 4 more comments
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]));
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 intermediateString
.
– daniu
Nov 19 at 12:25
add a comment |
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
Thank you, i will take time to read your awnser.
– zak zak
Nov 19 at 12:24
add a comment |
up vote
0
down vote
Since you want to solve this problem and want to learn how streams can be useful in this situation
Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))
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
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
add a comment |
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]));
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 intermediateString
.
– daniu
Nov 19 at 12:25
add a comment |
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]));
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 intermediateString
.
– daniu
Nov 19 at 12:25
add a comment |
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]));
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]));
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 intermediateString
.
– daniu
Nov 19 at 12:25
add a comment |
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 intermediateString
.
– 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
add a comment |
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
Thank you, i will take time to read your awnser.
– zak zak
Nov 19 at 12:24
add a comment |
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
Thank you, i will take time to read your awnser.
– zak zak
Nov 19 at 12:24
add a comment |
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
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
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
add a comment |
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
add a comment |
up vote
0
down vote
Since you want to solve this problem and want to learn how streams can be useful in this situation
Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))
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
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
add a comment |
up vote
0
down vote
Since you want to solve this problem and want to learn how streams can be useful in this situation
Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))
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
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
add a comment |
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
Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))
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
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
Since you want to solve this problem and want to learn how streams can be useful in this situation
Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))
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
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
answered Nov 19 at 12:30
Anshul
2118
2118
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 withString.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