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);
}









share|improve this question






















  • 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










  • 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















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);
}









share|improve this question






















  • 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










  • 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













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);
}









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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










  • 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










  • 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


















  • 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










  • 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
















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












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);
}
}





share|improve this answer























  • 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










  • 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













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%2f53248156%2fremove-specific-line-from-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



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);
}
}





share|improve this answer























  • 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










  • 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

















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);
}
}





share|improve this answer























  • 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










  • 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















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);
}
}





share|improve this answer














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);
}
}






share|improve this answer














share|improve this answer



share|improve this answer








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










  • 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




















  • 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










  • 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


















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




















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%2f53248156%2fremove-specific-line-from-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







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation