Page 1 of 3 123 LastLast
Results 1 to 20 of 42
  1. #1
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default [SOLVED] Remove All Line from File

    Hi all

    How can i remove all line from file..

    plz Help me

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  3. #3
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    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..

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

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What's the meaning of this,

    Java Code:
             if (! file.exists() )
                 {
           	 boolean success = file.delete();
             }
    your logic is wrong here. You have to check the deletion is success or not. Can you see what's the error is.

  5. #5
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    What's the meaning of this,

    Java Code:
             if (! file.exists() )
                 {
           	 boolean success = file.delete();
             }
    your logic is wrong here. You have to check the deletion is success or not. Can you see what's the error is.

    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

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  7. #7
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    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.
    Sorry don't fire.I am adding there line
    Java Code:
    Without deleted all button,i am adding these button
    .

    Plz don't fire sorry..So plz help me

  8. #8
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Mir View Post
    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.
    Java Code:
    Without deleted all button
    ,i am adding these button
    this was my last post..So plz don't fire

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    File deletion should be like this. Try to choose the correct way,

    Java 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 deletion try to write a file. The way you use is wrong.


  10. #10
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    File deletion should be like this. Try to choose the correct way,

    Java 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 deletion try to write a file. The way you use is wrong.

    Thxs sir last post is to misunderstanding with us...Sorry once again..
    Sir in the code what is happening..When i click the delete button then file is deleted.But when i call the new button then old file is back..Can i send you new button code..

    Plz help me..

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    After deleting see there is the deleted file is still exist or not.

    Anyway, send the code here to see.

  12. #12
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    After deleting see there is the deleted file is still exist or not.

    Anyway, send the code here to see.
    ok this is code

    Java Code:
    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());
            	    }
    
    }
    plz help me
    Last edited by Mir; 07-16-2008 at 07:27 AM.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  14. #14
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    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

    Java Code:
    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());
    		}
    	}
    }
    Plz help me sir

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  16. #16
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    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.
    Yes sir it is correct click event..

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  18. #18
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    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.
    Sir created and deletion is success but when i call new button then old file come with contents...So plz tell me where i wrong and wht modification need this code.

    Plz help me

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  20. #20
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Simple. That means your new button calls a method to add data to the file too.
    Yes sir you are right but what's a sloution for this...Plz tell me Plz plz plz...

Page 1 of 3 123 LastLast

Similar Threads

  1. Remove duplicate lines from a text file
    By Dirt.Diver in forum New To Java
    Replies: 15
    Last Post: 06-25-2008, 02:08 PM
  2. read the file from different line number
    By vaskarbasak in forum Advanced Java
    Replies: 3
    Last Post: 06-02-2008, 01:31 PM
  3. Replies: 0
    Last Post: 01-20-2008, 06:07 AM
  4. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM
  5. How to remove Control Characters from an input file?
    By renjan in forum Advanced Java
    Replies: 0
    Last Post: 08-01-2007, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •