Rewrite a specific line in a txt file











up vote
0
down vote

favorite












I was trying to rewrite a line that contains student details in a txt file. There will be a list of students' detail in the file, for example:




  • Name1,10

  • Name2,20

  • Name3,30


I tried to rewrite Name2,20 to Name2,13 using a BufferedReader to find the line with Name2. And a BufferedWriter to replace the line with new text, but it turns out the code will write my whole txt file to null.



Here's my code:



String lineText;
String newLine = "Name,age";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(path,false));
while ((lineText = br.readLine()) != null){
System.out.println(">" + lineText);
String studentData = lineText.split(",");
if(studentData[0].equals(Name2)){
bw.write(newLine);
}
System.out.println(lineText);
}
br.close();
bw.close();

}
catch (IOException e) {
e.printStackTrace();
}


Can anyone please tell me how to rewrite a specific line in txt file?










share|improve this question






















  • The only way (well, not strictly speaking, but realistically) to change the middle of a text file is to rewrite the entire file. But you need to write to another location and then copy that back over the original file when you're done.
    – Boris the Spider
    Nov 8 at 18:03












  • docs.oracle.com/javase/tutorial/essential/io/rafs.html
    – PM 77-1
    Nov 8 at 18:03










  • The old and new line have the same length? If not, you'll have to rewrite the whole file
    – Robert Kock
    Nov 8 at 18:05










  • To rewrite the whole file does that means I need to create a new file?
    – Inverse0
    Nov 9 at 16:53















up vote
0
down vote

favorite












I was trying to rewrite a line that contains student details in a txt file. There will be a list of students' detail in the file, for example:




  • Name1,10

  • Name2,20

  • Name3,30


I tried to rewrite Name2,20 to Name2,13 using a BufferedReader to find the line with Name2. And a BufferedWriter to replace the line with new text, but it turns out the code will write my whole txt file to null.



Here's my code:



String lineText;
String newLine = "Name,age";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(path,false));
while ((lineText = br.readLine()) != null){
System.out.println(">" + lineText);
String studentData = lineText.split(",");
if(studentData[0].equals(Name2)){
bw.write(newLine);
}
System.out.println(lineText);
}
br.close();
bw.close();

}
catch (IOException e) {
e.printStackTrace();
}


Can anyone please tell me how to rewrite a specific line in txt file?










share|improve this question






















  • The only way (well, not strictly speaking, but realistically) to change the middle of a text file is to rewrite the entire file. But you need to write to another location and then copy that back over the original file when you're done.
    – Boris the Spider
    Nov 8 at 18:03












  • docs.oracle.com/javase/tutorial/essential/io/rafs.html
    – PM 77-1
    Nov 8 at 18:03










  • The old and new line have the same length? If not, you'll have to rewrite the whole file
    – Robert Kock
    Nov 8 at 18:05










  • To rewrite the whole file does that means I need to create a new file?
    – Inverse0
    Nov 9 at 16:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I was trying to rewrite a line that contains student details in a txt file. There will be a list of students' detail in the file, for example:




  • Name1,10

  • Name2,20

  • Name3,30


I tried to rewrite Name2,20 to Name2,13 using a BufferedReader to find the line with Name2. And a BufferedWriter to replace the line with new text, but it turns out the code will write my whole txt file to null.



Here's my code:



String lineText;
String newLine = "Name,age";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(path,false));
while ((lineText = br.readLine()) != null){
System.out.println(">" + lineText);
String studentData = lineText.split(",");
if(studentData[0].equals(Name2)){
bw.write(newLine);
}
System.out.println(lineText);
}
br.close();
bw.close();

}
catch (IOException e) {
e.printStackTrace();
}


Can anyone please tell me how to rewrite a specific line in txt file?










share|improve this question













I was trying to rewrite a line that contains student details in a txt file. There will be a list of students' detail in the file, for example:




  • Name1,10

  • Name2,20

  • Name3,30


I tried to rewrite Name2,20 to Name2,13 using a BufferedReader to find the line with Name2. And a BufferedWriter to replace the line with new text, but it turns out the code will write my whole txt file to null.



Here's my code:



String lineText;
String newLine = "Name,age";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(path,false));
while ((lineText = br.readLine()) != null){
System.out.println(">" + lineText);
String studentData = lineText.split(",");
if(studentData[0].equals(Name2)){
bw.write(newLine);
}
System.out.println(lineText);
}
br.close();
bw.close();

}
catch (IOException e) {
e.printStackTrace();
}


Can anyone please tell me how to rewrite a specific line in txt file?







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 18:01









Inverse0

1




1












  • The only way (well, not strictly speaking, but realistically) to change the middle of a text file is to rewrite the entire file. But you need to write to another location and then copy that back over the original file when you're done.
    – Boris the Spider
    Nov 8 at 18:03












  • docs.oracle.com/javase/tutorial/essential/io/rafs.html
    – PM 77-1
    Nov 8 at 18:03










  • The old and new line have the same length? If not, you'll have to rewrite the whole file
    – Robert Kock
    Nov 8 at 18:05










  • To rewrite the whole file does that means I need to create a new file?
    – Inverse0
    Nov 9 at 16:53


















  • The only way (well, not strictly speaking, but realistically) to change the middle of a text file is to rewrite the entire file. But you need to write to another location and then copy that back over the original file when you're done.
    – Boris the Spider
    Nov 8 at 18:03












  • docs.oracle.com/javase/tutorial/essential/io/rafs.html
    – PM 77-1
    Nov 8 at 18:03










  • The old and new line have the same length? If not, you'll have to rewrite the whole file
    – Robert Kock
    Nov 8 at 18:05










  • To rewrite the whole file does that means I need to create a new file?
    – Inverse0
    Nov 9 at 16:53
















The only way (well, not strictly speaking, but realistically) to change the middle of a text file is to rewrite the entire file. But you need to write to another location and then copy that back over the original file when you're done.
– Boris the Spider
Nov 8 at 18:03






The only way (well, not strictly speaking, but realistically) to change the middle of a text file is to rewrite the entire file. But you need to write to another location and then copy that back over the original file when you're done.
– Boris the Spider
Nov 8 at 18:03














docs.oracle.com/javase/tutorial/essential/io/rafs.html
– PM 77-1
Nov 8 at 18:03




docs.oracle.com/javase/tutorial/essential/io/rafs.html
– PM 77-1
Nov 8 at 18:03












The old and new line have the same length? If not, you'll have to rewrite the whole file
– Robert Kock
Nov 8 at 18:05




The old and new line have the same length? If not, you'll have to rewrite the whole file
– Robert Kock
Nov 8 at 18:05












To rewrite the whole file does that means I need to create a new file?
– Inverse0
Nov 9 at 16:53




To rewrite the whole file does that means I need to create a new file?
– Inverse0
Nov 9 at 16:53












1 Answer
1






active

oldest

votes

















up vote
-1
down vote













Easiest way is to read the entire file, and store it in a variable. Replacing the line in question while reading the current file.



Something like:



String lineText;
String newLine = "Name,age";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(path,false));
String currentFileContents = "";
while ((lineText = br.readLine()) != null){
System.out.println(">" + lineText);
String studentData = lineText.split(",");
if(studentData[0].equals("Name2")){
currentFileContents += newLine;
} else {
currentFileContents += lineText;
}
}

bw.write(currentFileContents);
br.close();
bw.close();

} catch (IOException e) {
e.printStackTrace();
}





share|improve this answer





















  • Reading the whole file into memory is not the answer.
    – Boris the Spider
    Nov 8 at 18:32






  • 1




    Emmm it still set my whole file to null
    – Inverse0
    Nov 9 at 16:51











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%2f53213613%2frewrite-a-specific-line-in-a-txt-file%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








up vote
-1
down vote













Easiest way is to read the entire file, and store it in a variable. Replacing the line in question while reading the current file.



Something like:



String lineText;
String newLine = "Name,age";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(path,false));
String currentFileContents = "";
while ((lineText = br.readLine()) != null){
System.out.println(">" + lineText);
String studentData = lineText.split(",");
if(studentData[0].equals("Name2")){
currentFileContents += newLine;
} else {
currentFileContents += lineText;
}
}

bw.write(currentFileContents);
br.close();
bw.close();

} catch (IOException e) {
e.printStackTrace();
}





share|improve this answer





















  • Reading the whole file into memory is not the answer.
    – Boris the Spider
    Nov 8 at 18:32






  • 1




    Emmm it still set my whole file to null
    – Inverse0
    Nov 9 at 16:51















up vote
-1
down vote













Easiest way is to read the entire file, and store it in a variable. Replacing the line in question while reading the current file.



Something like:



String lineText;
String newLine = "Name,age";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(path,false));
String currentFileContents = "";
while ((lineText = br.readLine()) != null){
System.out.println(">" + lineText);
String studentData = lineText.split(",");
if(studentData[0].equals("Name2")){
currentFileContents += newLine;
} else {
currentFileContents += lineText;
}
}

bw.write(currentFileContents);
br.close();
bw.close();

} catch (IOException e) {
e.printStackTrace();
}





share|improve this answer





















  • Reading the whole file into memory is not the answer.
    – Boris the Spider
    Nov 8 at 18:32






  • 1




    Emmm it still set my whole file to null
    – Inverse0
    Nov 9 at 16:51













up vote
-1
down vote










up vote
-1
down vote









Easiest way is to read the entire file, and store it in a variable. Replacing the line in question while reading the current file.



Something like:



String lineText;
String newLine = "Name,age";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(path,false));
String currentFileContents = "";
while ((lineText = br.readLine()) != null){
System.out.println(">" + lineText);
String studentData = lineText.split(",");
if(studentData[0].equals("Name2")){
currentFileContents += newLine;
} else {
currentFileContents += lineText;
}
}

bw.write(currentFileContents);
br.close();
bw.close();

} catch (IOException e) {
e.printStackTrace();
}





share|improve this answer












Easiest way is to read the entire file, and store it in a variable. Replacing the line in question while reading the current file.



Something like:



String lineText;
String newLine = "Name,age";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(path,false));
String currentFileContents = "";
while ((lineText = br.readLine()) != null){
System.out.println(">" + lineText);
String studentData = lineText.split(",");
if(studentData[0].equals("Name2")){
currentFileContents += newLine;
} else {
currentFileContents += lineText;
}
}

bw.write(currentFileContents);
br.close();
bw.close();

} catch (IOException e) {
e.printStackTrace();
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 18:14









bobbyrne01

2,09343891




2,09343891












  • Reading the whole file into memory is not the answer.
    – Boris the Spider
    Nov 8 at 18:32






  • 1




    Emmm it still set my whole file to null
    – Inverse0
    Nov 9 at 16:51


















  • Reading the whole file into memory is not the answer.
    – Boris the Spider
    Nov 8 at 18:32






  • 1




    Emmm it still set my whole file to null
    – Inverse0
    Nov 9 at 16:51
















Reading the whole file into memory is not the answer.
– Boris the Spider
Nov 8 at 18:32




Reading the whole file into memory is not the answer.
– Boris the Spider
Nov 8 at 18:32




1




1




Emmm it still set my whole file to null
– Inverse0
Nov 9 at 16:51




Emmm it still set my whole file to null
– Inverse0
Nov 9 at 16:51


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213613%2frewrite-a-specific-line-in-a-txt-file%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()