Page 1 of 5 123 ... LastLast
Results 1 to 20 of 87
  1. #1
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default Delete From .txt file

    Hi All

    I have a problem in .txt file. How can i delete some specific line from .txt file.

    Plz help me

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Retrieve the content of that text file,
    Remove the desired lines,
    and save it again.
    freedom exists in the world of ideas

  3. #3
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Thxs for reply

    I am new in java.Can you give me more details.here is code.
    Java Code:
    try {
            			
        				String str ="xeee.txt";
        				File f = new File(str);
        				if(f.exists()){
        					BufferedWriter out = new BufferedWriter(new FileWriter("xeee.txt",true));
        					if(ADDSERVER.getText().equals("")){
        						ADDSERVER.requestFocus();
        					}
        					else{
        						out.write(ADDSERVER.getText());
        						if(f.canWrite()){
        					    //	text.setText("");
        							ADDSERVER.setText("");
        						     out.newLine();
        							//area.append("\n");
        						//	text.requestFocus();
        						}
        						else{
        						
        						}
        						out.close();
        						}
        					}
        					else{
        					
        						text.setText("");
        						//text.requestFocus();
        					}
        				}
        			
        			catch(Exception x){
        				x.printStackTrace();
        			}
    So can you give me more about that..

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    That kind of problem needs a sample input and expected output to be analyze... not the code.

    Here is an algo,

    Since removing such line is the goal,
    Use Scanner class....

    in every line that will be encountered, save it on an array(I prefer to use ArrayList for this)
    When removing such text on that specific line,
    use remove(int index) method.

    Example:
    If you have choosen to remove the 7th line,
    remove(6);
    Since the offset of the ArrayList is zero, you have to subtract the prefered line by one.

    Then save them all again using FileWriter.

    Java Code:
    FileWriter fw = new FileWriter("text.txt");
    int x=0;
    while(x<=arraylist.size()){
           fw.append(arraylist.get(x));
           x++;
    }
    fw.close();
    Last edited by sukatoa; 06-20-2008 at 02:07 PM. Reason: forgot to add variable x to increment
    freedom exists in the world of ideas

  5. #5
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    ok can you give some code of delete the item...
    Last edited by Sarinam; 06-20-2008 at 02:20 PM.

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

    Default

    first you try to your self then you ask for code..
    Last edited by Mir; 06-20-2008 at 02:48 PM.

  7. #7
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Can you give me some code becouse i am not getting

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Can you give me some code becouse i am not getting
    Hey, as mentioned above, first you try and show us what you've done. You know, the more effort you show, often the more help you receive here.

  9. #9
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    I am not geting plz give me some code..

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

  11. #11
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    What you have tried upto now?
    i write and store these valuse in array list.And now i am not abel to delete it.If i got some line of code then i will do it..

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

  13. #13
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    What's the problem. Sukatoa already gives the code for you.
    Hi Eranga

    it is not working..

  14. #14
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Can i show code

  15. #15
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Yes, you can show your code.....
    freedom exists in the world of ideas

  16. #16
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by sukatoa View Post
    Yes, you can show your code.....
    Here is code

    Java Code:
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    
    public class dlo extends Dialog
    {
    
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	private Frame      parent;
    	
    	private Button     OK;
    	private Button     CANCEL;
    	private List       SERVERS;	
    	private TextField  ADDSERVER;	
    	private Button	   ADD;
    	private Button     DELETE;
    	private ArrayList<String> ars;
    	private int pop;
    	private ArrayList<String>	arip = new ArrayList<String>();
    	private String strLine;
    
    	public dlo (Frame parent) 
    	{
    		super(parent, "Ik", true);
    
    		
    				
    		OK = new Button("OK");
    		CANCEL = new Button("CANCEL");
    		SERVERS = new List(10, true);
    		ADDSERVER = new TextField(15);	
    		ADD = new Button("< Add");
    		DELETE = new Button("Delete");
    		
    	 	setLayout(new GridBagLayout());
    		addToBag(this, SERVERS,    0, 0, 1, 3, 1.0,0.0);
    		addToBag(this, ADD,        1, 0, 1, 1, 0.0,0.0);
    		addToBag(this, ADDSERVER,  2, 0, 2, 1, 0.0,0.0);
    		addToBag(this, DELETE,     1, 1, 1, 1, 0.0,0.0);
    		addToBag(this, CANCEL,	   2, 4, 1, 1, 0.0,0.0);
    		addToBag(this, OK,         3, 4, 1, 1, 0.0,0.0);
    		
    		
    		pack();
    
    		Point location = parent.location();
    		Point fatherLocation = location;
    		move(fatherLocation.x + 100,fatherLocation.y + 100);
    		show();
    
    	}
    
    
    	private void addToBag(Container container, Object arg, int x, int y, int w, int h,
    							double weightx, double weighty)
    	{
    		GridBagLayout gbl = (GridBagLayout)container.getLayout();
    		GridBagConstraints c = new GridBagConstraints();
    		Component component;
    
    		c.fill = GridBagConstraints.BOTH;
    		c.gridx = x;
    		c.gridy = y;
    		c.gridwidth = w;
    		c.gridheight = h;
    		c.weightx = weightx;
    		c.weighty = weighty;
    
    		component = (Component)arg;
    
    		container.add(component);
    		gbl.setConstraints(component, c);
    	}
    
    
    
    	public boolean action(Event evt, Object arg) 
    	{
    		if (evt.target instanceof Button) {
    			if (evt.target == OK) {
    				return updateDB();
    			}
    			else if (evt.target == CANCEL) {
    				hide();
    				dispose();
    				return true;
    			}
    			else if (evt.target == ADD) {
    				if (ADDSERVER.getText() != "")  
    					SERVERS.addItem(ADDSERVER.getText());
    				
    			
    				try {
    					
        				String str ="xeee.txt";
        				
        				File f = new File(str);
        				if(f.exists()){
        				//	BufferedWriter out = new BufferedWriter(new FileWriter("xeee.txt",true));
        					 FileWriter fstream = new FileWriter("xeee.txt", true);
        	          	     BufferedWriter out = new BufferedWriter(fstream);
        					    	
        	          //	 int x=0;
        	         // 	 while(x<=arip.size()){
        	          	//        fstream.append(arip.get(x));
        	          	  //      x++;
        	          	// }
        	          	 
        					if(ADDSERVER.getText().equals("")){
        						int x=0; 
        						while(x<=ars.size()){
        							SERVERS.addItem(ars.get(x));
        							
        						       x++;
        						      
        						}
        						 
        						SERVERS.requestFocus();
        					}
        					else{
        						
        						 
        					out.write(ADDSERVER.getText());
        						if(f.canWrite()){
        					    
        						     ADDSERVER.setText("");
        						     out.newLine();
        							
        						}
        						
        						else{
        						
        						}
        						
        						
        						}
        					
        					}
        					
        				}
        			
        			catch(Exception x){
        				x.printStackTrace();
        			}
        			
        		}
    
    					return true;
    			}
    			else if (evt.target == DELETE) {
    			
    						 
    				try{
    				    FileWriter fstream1= new FileWriter("xeee.txt", true);
    				    FileInputStream fstrea = new FileInputStream("xeee.txt");
    				    DataInputStream in = new DataInputStream(fstrea);
    				    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    				    while ((strLine = br.readLine()) != null)   {
    				   
    				    	arip.add(strLine);
    				    	JOptionPane.showMessageDialog(null,arip);
    				    }
    				    int x=0;
    			    	while(x<=arip.size()){
    			    		fstream1.append(arip.get(x));
    			    	       x++;
    			    	}
    			    
    				    
    				    in.close();
    				    }
    					catch (Exception e)
    				    {
    				      System.err.println("Error: " + e.getMessage());
    				    }
    			   
    
    				return true;
    			}
    		
    		
    		return false;
    	}
    
    
    
    	
    	
    	private boolean updateDB()
    	{
    		hide();
    		dispose();
    		return true;
    	}
    	public boolean handleEvent (Event event) 
    	{
    		switch (event.id) {
    		case Event.WINDOW_DESTROY:
    			dispose();
    			return true;
    		default:
    			return super.handleEvent(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

    This is what sukatoa talking about.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class RemoveFileContent {
    
        private ArrayList<String> data;
        private Scanner dataScan;
        
        public RemoveFileContent() {
            data = new ArrayList<String>();
        }
        
        public static void main(String[] args) {
            new RemoveFileContent().readFile();
        }
        
        private void readFile() {
             FileInputStream fis = null;
            try {
                fis = new FileInputStream("files/EditedFile.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                
                String line = null;
                while((line = br.readLine()) != null){
                    System.out.println(line);
                    data.add(line);
                }
            } 
            catch (IOException ex) {
                System.out.println(ex.getMessage());
            }        
            finally {
                try {
                    fis.close();
                } 
                catch (IOException ex) {
                    System.out.println(ex.getMessage());
                }
            }
            removeData();
        }
        
        private void removeData() {
            dataScan = new Scanner(System.in);
            System.out.println("Which line you want to remove: ");
            int i = Integer.parseInt(dataScan.nextLine());
            data.remove(i - 1);
            writeDataBack();
        }
        
        private void writeDataBack() {
            FileWriter fw = null;
            int x = 0;
            try{
                fw = new FileWriter("files/DataToFile.txt");
                BufferedWriter out = new BufferedWriter(fw);
    
                while(x < data.size()) {
                    out.write(data.get(x));
                    out.newLine();
                    x++;
                }
    
                out.close();
            }
            catch(IOException ioex){
                System.out.println(ioex.getLocalizedMessage());
            }        
        }
    
    }

  18. #18
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    This is what sukatoa talking about.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class RemoveFileContent {
    
        private ArrayList<String> data;
        private Scanner dataScan;
        
        public RemoveFileContent() {
            data = new ArrayList<String>();
        }
        
        public static void main(String[] args) {
            new RemoveFileContent().readFile();
        }
        
        private void readFile() {
             FileInputStream fis = null;
            try {
                fis = new FileInputStream("files/EditedFile.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                
                String line = null;
                while((line = br.readLine()) != null){
                    System.out.println(line);
                    data.add(line);
                }
            } 
            catch (IOException ex) {
                System.out.println(ex.getMessage());
            }        
            finally {
                try {
                    fis.close();
                } 
                catch (IOException ex) {
                    System.out.println(ex.getMessage());
                }
            }
            removeData();
        }
        
        private void removeData() {
            dataScan = new Scanner(System.in);
            System.out.println("Which line you want to remove: ");
            int i = Integer.parseInt(dataScan.nextLine());
            data.remove(i - 1);
            writeDataBack();
        }
        
        private void writeDataBack() {
            FileWriter fw = null;
            int x = 0;
            try{
                fw = new FileWriter("files/DataToFile.txt");
                BufferedWriter out = new BufferedWriter(fw);
    
                while(x < data.size()) {
                    out.write(data.get(x));
                    out.newLine();
                    x++;
                }
    
                out.close();
            }
            catch(IOException ioex){
                System.out.println(ioex.getLocalizedMessage());
            }        
        }
    
    }
    it help me aloat to understand but i want to delete some specific test through the texarea input..
    Can you give me concept of it..

  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

    Default

    Display all read line to a text area first, at the same time keep them recorded in ArrayList. Then remove the selected line from the text are and refer the selected item index to remove from the List.

  20. #20
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Display all read line to a text area first, at the same time keep them recorded in ArrayList. Then remove the selected line from the text are and refer the selected item index to remove from the List.
    Sir i am abel to store all record to ArrayList.But to display in textarea How can i do sir.i jave no idia aboput that.Plz help me

Page 1 of 5 123 ... LastLast

Similar Threads

  1. [SOLVED] Delete Current line from file
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 04-06-2012, 08:00 AM
  2. Can we lift the images restriction?
    By Cyorxamp in forum Suggestions & Feedback
    Replies: 30
    Last Post: 07-01-2008, 08:32 AM
  3. Replies: 0
    Last Post: 04-05-2008, 10:13 AM
  4. problem when I try to delete a file
    By tommy in forum Advanced Java
    Replies: 2
    Last Post: 07-31-2007, 02:44 PM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 08:11 AM

Posting Permissions

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