can someone give a code example of deleting data to a text file in GUI?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
can someone give a code example of deleting data to a text file in GUI?
What have you tried? What examples have you found searching the Internet that you don't understand or need help modifying for your use?
You give us a broken example, and we'll help you fix it.
here's my code
i want to delete a data to a txtfile that i typed to my textfield..pls help me ty..
word = ("");
word = inputOutputField.getText().trim();
try{
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line = null;
while ((line = br.readLine()) != null) {
if (!line.trim().equals(word)) {
}
br.close();
}
}catch (IOException E) {
System.out.println("Error" + E);
Please post your code in code tags. You can learn how here.
Comment your code as or before you write it.
Your code:
identifies the desired word to delete and stores it in the variable word.
reads a file, "test.txt", a line at a time
if the line read is NOT the same as the word to be deleted, do nothing
otherwise, do nothing
What you need to do is replace the "do nothings" above with what you'd like to do. For example, if you want to delete the line that contains "word", then don't save that line to a collection (an array?) of lines read. Otherwise, save the line to the collection:
When ready, write the resulting collection of saved lines back to the file from which they came, effectively deleting the words that were not saved to the collection.while ( ( line = br.readLine() ) != null ) { // check for lines NOT equal to the target word if ( !line.trim().equals( word ) ) { // when found, store the line to a collection like an array } else { // no code required to skip saving the line } }