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?
java
add a comment |
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?
java
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
add a comment |
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?
java
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
java
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
add a comment |
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
add a comment |
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();
}
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
add a comment |
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();
}
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
add a comment |
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();
}
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
add a comment |
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();
}
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();
}
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
add a comment |
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
add a comment |
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.
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%2f53213613%2frewrite-a-specific-line-in-a-txt-file%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
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