Java splitting too large text file enhance performance

Multi tool use
I have a too large data.txt
file containing data as follows:
a = line0
line1
line2
€
b = line0
line1
line2
€
...
z = line0
line1
I am still beginner with java, I wrote this code to split the data.txt
into multiple .txt
files named a.txt , b.txt etc...
it splits the data.txt
at each €n
character.
It works perfectly, but it is so slow, I noticed, as if it reads everything at once and makes its mission, then at once the result .txt files are created and showed at once in the destination folder.
How could I make my code faster, for example when it is finished with the first part then it should create the result a.txt file immediatly then the second, third and so on...
Thank in advance
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.io.*;
public class Main
{
public static void main(String args)
{
File file = new File("/path/path2/file.txt");
try{
Scanner scanner = new Scanner(file);
String str = "";
while(scanner.hasNext()){
str+=scanner.nextLine()+"n";
}
String charac = "€";
String end = "end of file";
for(int i = 0; i < (str.split(charac).length)-1; i++){
String name = str.split(charac)[i].split(" = ")[0];
String out= "/path/path2/path3/Folder/"+name+".txt";
FileWriter fw = new FileWriter(out);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(str.split(charac)[i]+"n"+end);
bw.close();
}
System.out.println("Splitting is finished");
}
catch(Exception e){}
}
}
java performance file
bumped to the homepage by Community♦ 17 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a too large data.txt
file containing data as follows:
a = line0
line1
line2
€
b = line0
line1
line2
€
...
z = line0
line1
I am still beginner with java, I wrote this code to split the data.txt
into multiple .txt
files named a.txt , b.txt etc...
it splits the data.txt
at each €n
character.
It works perfectly, but it is so slow, I noticed, as if it reads everything at once and makes its mission, then at once the result .txt files are created and showed at once in the destination folder.
How could I make my code faster, for example when it is finished with the first part then it should create the result a.txt file immediatly then the second, third and so on...
Thank in advance
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.io.*;
public class Main
{
public static void main(String args)
{
File file = new File("/path/path2/file.txt");
try{
Scanner scanner = new Scanner(file);
String str = "";
while(scanner.hasNext()){
str+=scanner.nextLine()+"n";
}
String charac = "€";
String end = "end of file";
for(int i = 0; i < (str.split(charac).length)-1; i++){
String name = str.split(charac)[i].split(" = ")[0];
String out= "/path/path2/path3/Folder/"+name+".txt";
FileWriter fw = new FileWriter(out);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(str.split(charac)[i]+"n"+end);
bw.close();
}
System.out.println("Splitting is finished");
}
catch(Exception e){}
}
}
java performance file
bumped to the homepage by Community♦ 17 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Your program is slow because it load the full file in memory before splitting it (str+=scanner.nextLine()+"n";
). You can do the split inside the first loop (where your are reading the file)
– gervais.b
Dec 11 '18 at 8:38
What do you mean exactly, should I move the while block with its statement str+=scanner.nextLine()+"n"; into the for-loop as first statement?
– Khaled
Dec 11 '18 at 10:38
you should move the whole splitting logic inside the first loopwhile(scanner.hasNext())
– gervais.b
Dec 11 '18 at 11:18
and use a StringBuilder object rather than string concatenation
– pcoates
Dec 19 '18 at 23:06
add a comment |
I have a too large data.txt
file containing data as follows:
a = line0
line1
line2
€
b = line0
line1
line2
€
...
z = line0
line1
I am still beginner with java, I wrote this code to split the data.txt
into multiple .txt
files named a.txt , b.txt etc...
it splits the data.txt
at each €n
character.
It works perfectly, but it is so slow, I noticed, as if it reads everything at once and makes its mission, then at once the result .txt files are created and showed at once in the destination folder.
How could I make my code faster, for example when it is finished with the first part then it should create the result a.txt file immediatly then the second, third and so on...
Thank in advance
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.io.*;
public class Main
{
public static void main(String args)
{
File file = new File("/path/path2/file.txt");
try{
Scanner scanner = new Scanner(file);
String str = "";
while(scanner.hasNext()){
str+=scanner.nextLine()+"n";
}
String charac = "€";
String end = "end of file";
for(int i = 0; i < (str.split(charac).length)-1; i++){
String name = str.split(charac)[i].split(" = ")[0];
String out= "/path/path2/path3/Folder/"+name+".txt";
FileWriter fw = new FileWriter(out);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(str.split(charac)[i]+"n"+end);
bw.close();
}
System.out.println("Splitting is finished");
}
catch(Exception e){}
}
}
java performance file
I have a too large data.txt
file containing data as follows:
a = line0
line1
line2
€
b = line0
line1
line2
€
...
z = line0
line1
I am still beginner with java, I wrote this code to split the data.txt
into multiple .txt
files named a.txt , b.txt etc...
it splits the data.txt
at each €n
character.
It works perfectly, but it is so slow, I noticed, as if it reads everything at once and makes its mission, then at once the result .txt files are created and showed at once in the destination folder.
How could I make my code faster, for example when it is finished with the first part then it should create the result a.txt file immediatly then the second, third and so on...
Thank in advance
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.io.*;
public class Main
{
public static void main(String args)
{
File file = new File("/path/path2/file.txt");
try{
Scanner scanner = new Scanner(file);
String str = "";
while(scanner.hasNext()){
str+=scanner.nextLine()+"n";
}
String charac = "€";
String end = "end of file";
for(int i = 0; i < (str.split(charac).length)-1; i++){
String name = str.split(charac)[i].split(" = ")[0];
String out= "/path/path2/path3/Folder/"+name+".txt";
FileWriter fw = new FileWriter(out);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(str.split(charac)[i]+"n"+end);
bw.close();
}
System.out.println("Splitting is finished");
}
catch(Exception e){}
}
}
java performance file
java performance file
asked Dec 10 '18 at 23:56
KhaledKhaled
384
384
bumped to the homepage by Community♦ 17 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 17 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Your program is slow because it load the full file in memory before splitting it (str+=scanner.nextLine()+"n";
). You can do the split inside the first loop (where your are reading the file)
– gervais.b
Dec 11 '18 at 8:38
What do you mean exactly, should I move the while block with its statement str+=scanner.nextLine()+"n"; into the for-loop as first statement?
– Khaled
Dec 11 '18 at 10:38
you should move the whole splitting logic inside the first loopwhile(scanner.hasNext())
– gervais.b
Dec 11 '18 at 11:18
and use a StringBuilder object rather than string concatenation
– pcoates
Dec 19 '18 at 23:06
add a comment |
Your program is slow because it load the full file in memory before splitting it (str+=scanner.nextLine()+"n";
). You can do the split inside the first loop (where your are reading the file)
– gervais.b
Dec 11 '18 at 8:38
What do you mean exactly, should I move the while block with its statement str+=scanner.nextLine()+"n"; into the for-loop as first statement?
– Khaled
Dec 11 '18 at 10:38
you should move the whole splitting logic inside the first loopwhile(scanner.hasNext())
– gervais.b
Dec 11 '18 at 11:18
and use a StringBuilder object rather than string concatenation
– pcoates
Dec 19 '18 at 23:06
Your program is slow because it load the full file in memory before splitting it (
str+=scanner.nextLine()+"n";
). You can do the split inside the first loop (where your are reading the file)– gervais.b
Dec 11 '18 at 8:38
Your program is slow because it load the full file in memory before splitting it (
str+=scanner.nextLine()+"n";
). You can do the split inside the first loop (where your are reading the file)– gervais.b
Dec 11 '18 at 8:38
What do you mean exactly, should I move the while block with its statement str+=scanner.nextLine()+"n"; into the for-loop as first statement?
– Khaled
Dec 11 '18 at 10:38
What do you mean exactly, should I move the while block with its statement str+=scanner.nextLine()+"n"; into the for-loop as first statement?
– Khaled
Dec 11 '18 at 10:38
you should move the whole splitting logic inside the first loop
while(scanner.hasNext())
– gervais.b
Dec 11 '18 at 11:18
you should move the whole splitting logic inside the first loop
while(scanner.hasNext())
– gervais.b
Dec 11 '18 at 11:18
and use a StringBuilder object rather than string concatenation
– pcoates
Dec 19 '18 at 23:06
and use a StringBuilder object rather than string concatenation
– pcoates
Dec 19 '18 at 23:06
add a comment |
1 Answer
1
active
oldest
votes
As said in the comment, you program is slow because you load the full file in memory. To change that you should keep only your first loop where you read but also write and split into files.
while(scanner.hasNext()){
str+=scanner.nextLine()+"n";
if ( mustSplit(str) ) {
writeToAnotherFile(str);
str = "";
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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: "196"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
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%2fcodereview.stackexchange.com%2fquestions%2f209399%2fjava-splitting-too-large-text-file-enhance-performance%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As said in the comment, you program is slow because you load the full file in memory. To change that you should keep only your first loop where you read but also write and split into files.
while(scanner.hasNext()){
str+=scanner.nextLine()+"n";
if ( mustSplit(str) ) {
writeToAnotherFile(str);
str = "";
}
}
add a comment |
As said in the comment, you program is slow because you load the full file in memory. To change that you should keep only your first loop where you read but also write and split into files.
while(scanner.hasNext()){
str+=scanner.nextLine()+"n";
if ( mustSplit(str) ) {
writeToAnotherFile(str);
str = "";
}
}
add a comment |
As said in the comment, you program is slow because you load the full file in memory. To change that you should keep only your first loop where you read but also write and split into files.
while(scanner.hasNext()){
str+=scanner.nextLine()+"n";
if ( mustSplit(str) ) {
writeToAnotherFile(str);
str = "";
}
}
As said in the comment, you program is slow because you load the full file in memory. To change that you should keep only your first loop where you read but also write and split into files.
while(scanner.hasNext()){
str+=scanner.nextLine()+"n";
if ( mustSplit(str) ) {
writeToAnotherFile(str);
str = "";
}
}
answered Dec 11 '18 at 11:20
gervais.bgervais.b
1,072410
1,072410
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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%2fcodereview.stackexchange.com%2fquestions%2f209399%2fjava-splitting-too-large-text-file-enhance-performance%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
nQ1,kNIfV,sF,2LEpWTJ9SL7DIe29euEOxYLdgq1Xzy FgNLBOLvosJRItC,637z8Yo,eejmGbOOk,KnbUitjfi4tB3zCg,l
Your program is slow because it load the full file in memory before splitting it (
str+=scanner.nextLine()+"n";
). You can do the split inside the first loop (where your are reading the file)– gervais.b
Dec 11 '18 at 8:38
What do you mean exactly, should I move the while block with its statement str+=scanner.nextLine()+"n"; into the for-loop as first statement?
– Khaled
Dec 11 '18 at 10:38
you should move the whole splitting logic inside the first loop
while(scanner.hasNext())
– gervais.b
Dec 11 '18 at 11:18
and use a StringBuilder object rather than string concatenation
– pcoates
Dec 19 '18 at 23:06