Remove specific line from txt file
up vote
0
down vote
favorite
Hello i need to delete a specific line from text file after ID search p.g. ID=2
students.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman
And after search and delete to get:
students.txt
1,Giannis,Oreos,Man
3,Maria,Oaka,Woman
But is not working properly
Code so far:
@FXML
TextField ID2;
@FXML
public void UseDelete() throws IOException {
File inputFile = new File("src/inware/students.txt");
File tempFile = new File("src/inware/studentsTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = ID2.getText();
String currentLine;
while ((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if (trimmedLine.equals(lineToRemove)) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile);
}
java javafx text-files
|
show 3 more comments
up vote
0
down vote
favorite
Hello i need to delete a specific line from text file after ID search p.g. ID=2
students.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman
And after search and delete to get:
students.txt
1,Giannis,Oreos,Man
3,Maria,Oaka,Woman
But is not working properly
Code so far:
@FXML
TextField ID2;
@FXML
public void UseDelete() throws IOException {
File inputFile = new File("src/inware/students.txt");
File tempFile = new File("src/inware/studentsTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = ID2.getText();
String currentLine;
while ((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if (trimmedLine.equals(lineToRemove)) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile);
}
java javafx text-files
What is the result that are you seeing which is incorrect?
– Ryan Pierce Williams
Nov 11 at 11:15
DoesID2.getText()
equal2,Maria,Karra,Woman
? Its name implies it would only contain2
.
– Andy Turner
Nov 11 at 11:16
Yes, Andy Turner i have text field and i can put the ID p.g.ID=2
– Fotic Pap
Nov 11 at 11:18
Ryan Pierce Williams i want to take the 2nd txt form (without 2nd line if i give ID=2
)
– Fotic Pap
Nov 11 at 11:20
The code is not delete anything now, just createstudentsTemp.txt
with the same lines
– Fotic Pap
Nov 11 at 11:23
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hello i need to delete a specific line from text file after ID search p.g. ID=2
students.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman
And after search and delete to get:
students.txt
1,Giannis,Oreos,Man
3,Maria,Oaka,Woman
But is not working properly
Code so far:
@FXML
TextField ID2;
@FXML
public void UseDelete() throws IOException {
File inputFile = new File("src/inware/students.txt");
File tempFile = new File("src/inware/studentsTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = ID2.getText();
String currentLine;
while ((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if (trimmedLine.equals(lineToRemove)) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile);
}
java javafx text-files
Hello i need to delete a specific line from text file after ID search p.g. ID=2
students.txt
1,Giannis,Oreos,Man
2,Maria,Karra,Woman
3,Maria,Oaka,Woman
And after search and delete to get:
students.txt
1,Giannis,Oreos,Man
3,Maria,Oaka,Woman
But is not working properly
Code so far:
@FXML
TextField ID2;
@FXML
public void UseDelete() throws IOException {
File inputFile = new File("src/inware/students.txt");
File tempFile = new File("src/inware/studentsTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = ID2.getText();
String currentLine;
while ((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if (trimmedLine.equals(lineToRemove)) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile);
}
java javafx text-files
java javafx text-files
asked Nov 11 at 11:14
Fotic Pap
95
95
What is the result that are you seeing which is incorrect?
– Ryan Pierce Williams
Nov 11 at 11:15
DoesID2.getText()
equal2,Maria,Karra,Woman
? Its name implies it would only contain2
.
– Andy Turner
Nov 11 at 11:16
Yes, Andy Turner i have text field and i can put the ID p.g.ID=2
– Fotic Pap
Nov 11 at 11:18
Ryan Pierce Williams i want to take the 2nd txt form (without 2nd line if i give ID=2
)
– Fotic Pap
Nov 11 at 11:20
The code is not delete anything now, just createstudentsTemp.txt
with the same lines
– Fotic Pap
Nov 11 at 11:23
|
show 3 more comments
What is the result that are you seeing which is incorrect?
– Ryan Pierce Williams
Nov 11 at 11:15
DoesID2.getText()
equal2,Maria,Karra,Woman
? Its name implies it would only contain2
.
– Andy Turner
Nov 11 at 11:16
Yes, Andy Turner i have text field and i can put the ID p.g.ID=2
– Fotic Pap
Nov 11 at 11:18
Ryan Pierce Williams i want to take the 2nd txt form (without 2nd line if i give ID=2
)
– Fotic Pap
Nov 11 at 11:20
The code is not delete anything now, just createstudentsTemp.txt
with the same lines
– Fotic Pap
Nov 11 at 11:23
What is the result that are you seeing which is incorrect?
– Ryan Pierce Williams
Nov 11 at 11:15
What is the result that are you seeing which is incorrect?
– Ryan Pierce Williams
Nov 11 at 11:15
Does
ID2.getText()
equal 2,Maria,Karra,Woman
? Its name implies it would only contain 2
.– Andy Turner
Nov 11 at 11:16
Does
ID2.getText()
equal 2,Maria,Karra,Woman
? Its name implies it would only contain 2
.– Andy Turner
Nov 11 at 11:16
Yes, Andy Turner i have text field and i can put the ID p.g.
ID=2
– Fotic Pap
Nov 11 at 11:18
Yes, Andy Turner i have text field and i can put the ID p.g.
ID=2
– Fotic Pap
Nov 11 at 11:18
Ryan Pierce Williams i want to take the 2nd txt form (without 2nd line if i give ID=
2
)– Fotic Pap
Nov 11 at 11:20
Ryan Pierce Williams i want to take the 2nd txt form (without 2nd line if i give ID=
2
)– Fotic Pap
Nov 11 at 11:20
The code is not delete anything now, just create
studentsTemp.txt
with the same lines– Fotic Pap
Nov 11 at 11:23
The code is not delete anything now, just create
studentsTemp.txt
with the same lines– Fotic Pap
Nov 11 at 11:23
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
If you want to remove a line by a line number I guess you can do a change to your code like this. You can give the int value of Id to the lineToRemove
variable instead of my hard coded value
import java.io.*;
public class A {
public static void main(String args) throws IOException {
new A().useDelete();
}
public void useDelete() throws IOException {
File inputFile = new File("src/inware/students.txt");
File tempFile = new File("src/inware/studentsTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
int lineToRemove = 2;
String currentLine;
int count = 0;
while ((currentLine = reader.readLine()) != null) {
count++;
if (count == lineToRemove) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
}
}
First of all Thanks for the help! It work, but is not replace thestudent.txt
file just create a new onestudentsTemp.txt
with the changes.
– Fotic Pap
Nov 11 at 12:29
@FoticPap Its how your code is written. But if you want to replace the student file, you just have to delete and replace the name of it. I have edited the code with changes. Use this edited one
– Sand
Nov 11 at 12:53
int lineToRemove = 2;
this cant work for me, because i need to check the string number i gave (gives me bug) so if i put ID=2
delete the 2nd line ok but if after i want to delete 1st line ID=1
delete the 3rd
– Fotic Pap
Nov 11 at 13:09
Any way i found solution to my problem i changeint lineToRemove = 2;
toint lineToRemove = Integer.parseInt(ID2.getText());
. Thanks for the help
– Fotic Pap
Nov 11 at 16:39
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
accepted
If you want to remove a line by a line number I guess you can do a change to your code like this. You can give the int value of Id to the lineToRemove
variable instead of my hard coded value
import java.io.*;
public class A {
public static void main(String args) throws IOException {
new A().useDelete();
}
public void useDelete() throws IOException {
File inputFile = new File("src/inware/students.txt");
File tempFile = new File("src/inware/studentsTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
int lineToRemove = 2;
String currentLine;
int count = 0;
while ((currentLine = reader.readLine()) != null) {
count++;
if (count == lineToRemove) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
}
}
First of all Thanks for the help! It work, but is not replace thestudent.txt
file just create a new onestudentsTemp.txt
with the changes.
– Fotic Pap
Nov 11 at 12:29
@FoticPap Its how your code is written. But if you want to replace the student file, you just have to delete and replace the name of it. I have edited the code with changes. Use this edited one
– Sand
Nov 11 at 12:53
int lineToRemove = 2;
this cant work for me, because i need to check the string number i gave (gives me bug) so if i put ID=2
delete the 2nd line ok but if after i want to delete 1st line ID=1
delete the 3rd
– Fotic Pap
Nov 11 at 13:09
Any way i found solution to my problem i changeint lineToRemove = 2;
toint lineToRemove = Integer.parseInt(ID2.getText());
. Thanks for the help
– Fotic Pap
Nov 11 at 16:39
add a comment |
up vote
1
down vote
accepted
If you want to remove a line by a line number I guess you can do a change to your code like this. You can give the int value of Id to the lineToRemove
variable instead of my hard coded value
import java.io.*;
public class A {
public static void main(String args) throws IOException {
new A().useDelete();
}
public void useDelete() throws IOException {
File inputFile = new File("src/inware/students.txt");
File tempFile = new File("src/inware/studentsTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
int lineToRemove = 2;
String currentLine;
int count = 0;
while ((currentLine = reader.readLine()) != null) {
count++;
if (count == lineToRemove) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
}
}
First of all Thanks for the help! It work, but is not replace thestudent.txt
file just create a new onestudentsTemp.txt
with the changes.
– Fotic Pap
Nov 11 at 12:29
@FoticPap Its how your code is written. But if you want to replace the student file, you just have to delete and replace the name of it. I have edited the code with changes. Use this edited one
– Sand
Nov 11 at 12:53
int lineToRemove = 2;
this cant work for me, because i need to check the string number i gave (gives me bug) so if i put ID=2
delete the 2nd line ok but if after i want to delete 1st line ID=1
delete the 3rd
– Fotic Pap
Nov 11 at 13:09
Any way i found solution to my problem i changeint lineToRemove = 2;
toint lineToRemove = Integer.parseInt(ID2.getText());
. Thanks for the help
– Fotic Pap
Nov 11 at 16:39
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If you want to remove a line by a line number I guess you can do a change to your code like this. You can give the int value of Id to the lineToRemove
variable instead of my hard coded value
import java.io.*;
public class A {
public static void main(String args) throws IOException {
new A().useDelete();
}
public void useDelete() throws IOException {
File inputFile = new File("src/inware/students.txt");
File tempFile = new File("src/inware/studentsTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
int lineToRemove = 2;
String currentLine;
int count = 0;
while ((currentLine = reader.readLine()) != null) {
count++;
if (count == lineToRemove) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
}
}
If you want to remove a line by a line number I guess you can do a change to your code like this. You can give the int value of Id to the lineToRemove
variable instead of my hard coded value
import java.io.*;
public class A {
public static void main(String args) throws IOException {
new A().useDelete();
}
public void useDelete() throws IOException {
File inputFile = new File("src/inware/students.txt");
File tempFile = new File("src/inware/studentsTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
int lineToRemove = 2;
String currentLine;
int count = 0;
while ((currentLine = reader.readLine()) != null) {
count++;
if (count == lineToRemove) {
continue;
}
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
}
}
edited Nov 11 at 12:52
answered Nov 11 at 11:42
Sand
7259
7259
First of all Thanks for the help! It work, but is not replace thestudent.txt
file just create a new onestudentsTemp.txt
with the changes.
– Fotic Pap
Nov 11 at 12:29
@FoticPap Its how your code is written. But if you want to replace the student file, you just have to delete and replace the name of it. I have edited the code with changes. Use this edited one
– Sand
Nov 11 at 12:53
int lineToRemove = 2;
this cant work for me, because i need to check the string number i gave (gives me bug) so if i put ID=2
delete the 2nd line ok but if after i want to delete 1st line ID=1
delete the 3rd
– Fotic Pap
Nov 11 at 13:09
Any way i found solution to my problem i changeint lineToRemove = 2;
toint lineToRemove = Integer.parseInt(ID2.getText());
. Thanks for the help
– Fotic Pap
Nov 11 at 16:39
add a comment |
First of all Thanks for the help! It work, but is not replace thestudent.txt
file just create a new onestudentsTemp.txt
with the changes.
– Fotic Pap
Nov 11 at 12:29
@FoticPap Its how your code is written. But if you want to replace the student file, you just have to delete and replace the name of it. I have edited the code with changes. Use this edited one
– Sand
Nov 11 at 12:53
int lineToRemove = 2;
this cant work for me, because i need to check the string number i gave (gives me bug) so if i put ID=2
delete the 2nd line ok but if after i want to delete 1st line ID=1
delete the 3rd
– Fotic Pap
Nov 11 at 13:09
Any way i found solution to my problem i changeint lineToRemove = 2;
toint lineToRemove = Integer.parseInt(ID2.getText());
. Thanks for the help
– Fotic Pap
Nov 11 at 16:39
First of all Thanks for the help! It work, but is not replace the
student.txt
file just create a new one studentsTemp.txt
with the changes.– Fotic Pap
Nov 11 at 12:29
First of all Thanks for the help! It work, but is not replace the
student.txt
file just create a new one studentsTemp.txt
with the changes.– Fotic Pap
Nov 11 at 12:29
@FoticPap Its how your code is written. But if you want to replace the student file, you just have to delete and replace the name of it. I have edited the code with changes. Use this edited one
– Sand
Nov 11 at 12:53
@FoticPap Its how your code is written. But if you want to replace the student file, you just have to delete and replace the name of it. I have edited the code with changes. Use this edited one
– Sand
Nov 11 at 12:53
int lineToRemove = 2;
this cant work for me, because i need to check the string number i gave (gives me bug) so if i put ID=2
delete the 2nd line ok but if after i want to delete 1st line ID=1
delete the 3rd– Fotic Pap
Nov 11 at 13:09
int lineToRemove = 2;
this cant work for me, because i need to check the string number i gave (gives me bug) so if i put ID=2
delete the 2nd line ok but if after i want to delete 1st line ID=1
delete the 3rd– Fotic Pap
Nov 11 at 13:09
Any way i found solution to my problem i change
int lineToRemove = 2;
to int lineToRemove = Integer.parseInt(ID2.getText());
. Thanks for the help– Fotic Pap
Nov 11 at 16:39
Any way i found solution to my problem i change
int lineToRemove = 2;
to int lineToRemove = Integer.parseInt(ID2.getText());
. Thanks for the help– Fotic Pap
Nov 11 at 16:39
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%2f53248156%2fremove-specific-line-from-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
What is the result that are you seeing which is incorrect?
– Ryan Pierce Williams
Nov 11 at 11:15
Does
ID2.getText()
equal2,Maria,Karra,Woman
? Its name implies it would only contain2
.– Andy Turner
Nov 11 at 11:16
Yes, Andy Turner i have text field and i can put the ID p.g.
ID=2
– Fotic Pap
Nov 11 at 11:18
Ryan Pierce Williams i want to take the 2nd txt form (without 2nd line if i give ID=
2
)– Fotic Pap
Nov 11 at 11:20
The code is not delete anything now, just create
studentsTemp.txt
with the same lines– Fotic Pap
Nov 11 at 11:23