Hi all
How can i remove all line from file..
plz Help me
Printable View
Hi all
How can i remove all line from file..
plz Help me
Do you want to delete the content of a file?
Simple way to do it is, delete the file while copping the file name. And then create a new file with the copied file name.
ok i use this but i am geting problem.when i delete the file then it is showing deleted but when i call the new button then all file content show.Here is code which is i use..
Plz tell me where i wrong..
Code:private void deleteAllFromFile()
{
file = new File ("px.txt");
if (! file.exists() )
{
boolean success = file.delete();
}
file.delete();
try{
// Create file
FileWriter fstream = new FileWriter("px.txt");
BufferedWriter out = new BufferedWriter(fstream);
// out.write("Hello Java");
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
What's the meaning of this,
your logic is wrong here. You have to check the deletion is success or not. Can you see what's the error is.Code:if (! file.exists() )
{
boolean success = file.delete();
}
See error is nothing.What happen when i click on delete button then delete file and it is showing deleted.But when i click on New button then file deleted content showing.And i don't want to show these content.These code is given by you.Without deleted all button,i am adding these button
I don't give you any code related to deletion man. And also don't criticize others who helping you, if you don't know what are you doing. Stop all nonsense from here, and don't try to get it in my NOT TO ANSWER list.
File deletion should be like this. Try to choose the correct way,
After deletion try to write a file. The way you use is wrong.Code:private void deleteAllFromFile() {
File file = new File ("px.txt");
if(file.exists()) {
boolean success = file.delete();
if(!success) {
System.out.println("File deletion failed.");
}
}
}
After deleting see there is the deleted file is still exist or not.
Anyway, send the code here to see.
ok this is code
plz help meCode:
private void writeFile() {
FileWriter fw = null;
try {
fw = new FileWriter("px.txt");
BufferedWriter bufWriter = new BufferedWriter(fw);
for(int i = 0; i < data.size(); i++) {
bufWriter.write(data.get(i).toString());
bufWriter.newLine();
}
bufWriter.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
private void recordData(String str) {
if(data.isEmpty()) {
data.add(str);
}
else if(isDuplicate(str)) {
data.add(str);
}
}
private boolean isDuplicate(String str) {
String temp;
for(int i = 0; i < data.size(); i++) {
temp = data.get(i).toString();
if(!str.equals(temp)) {
isFind = true;
}
else {
return false;
}
}
return isFind;
}
private void populateData() {
for(int i = 0; i < data.size(); i++) {
SERVERS.add(data.get(i));
}
}
private void addUserData() {
SERVERS.clear();
populateData();
// String temp = ADDSERVER.getText();
String temp=JOptionPane.showInputDialog("ENTER name");
if(!temp.equals(""))
{
SERVERS.addItem(temp);
recordData(temp);
}
writeFile();
}
private void removeUserDataALL() {
//String temp = SERVERS.getSelectedItem();
int[] temp = SERVERS.getSelectedIndexes();
SERVERS.clear();
for(int i = temp.length-1; i>=0; i--)
//data.remove(data.get(i));
data.remove(temp[i]);
populateData();
writeFile();
}
private void deleteAllFromFile()
{
file = new File ("px.txt");
if (! file.exists() )
{
boolean success = file.delete();
}
file.delete();
try{
// Create file
FileWriter fstream = new FileWriter("px.txt");
BufferedWriter out = new BufferedWriter(fstream);
// out.write("Hello Java");
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
Is that your file creating code is fine? Write a simple application and see.
yes it is fine,i write a simple application for create a file.it is creating file.code is here
Plz help me sirCode:import java.io.*;
class FileWrite
{
public static void main(String args[])
{
try{
// Create file
FileWriter fstream = new FileWriter("px.txt");
BufferedWriter out = new BufferedWriter(fstream);
//out.write("Hello Java");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Ok, now you have confidence that your code is working. So on the delete button click you have to call the deleteAllFromFile() method. Check that you have call the correct method on the click event.
If so, what you say is couldn't happened. Since file created and deletion is success, you can't get the same file again, if you don't call/execute any other method that affected to write data to the file.
Simple. That means your new button calls a method to add data to the file too.