Results 1 to 3 of 3
  1. #1
    Azndaddy is offline Member
    Join Date
    Mar 2008
    Posts
    24
    Rep Power
    0

    Default [SOLVED] Delete Current line from file

    Hi, can some one help me out
    First of all, how do i call a command from a class
    eg. my class is called Util and it contain all the necessary information to readFile and writeFile etc.

    I want this class to call a command, so when i type delete in the console it would delete the current line in the file that has been open. the code for this i have at the moment are:

    public class Delete extends Command2
    {

    public Delete(Buffer buffer)
    {
    super("delete", buffer);
    }

    public void execute()
    {
    Util util = new Util();
    --> don't know what to do afterward
    }
    }

    in the public void execute()
    {
    }
    i want to be able to delete a current line from a file, but don't know how to implement it

    help would be great

    kind regards

  2. #2
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Hey Azndaddy,

    I wrote you a quick example class showing how I would do it.
    Hopefully you can use this to expand on your own work.

    The program reads in a txt file, processes it line by line and after each line asks you if you want to delete it or not. If you press enter it will proceed to the next line. If you type 'delete' it will delete the current line.

    The output is saved to a new file.

    Java Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Writer;
    
    public class deletelineClass {
    
    public static void main(String[] args) throws Exception {
    		
    // Creates file to write to
    Writer output = null;
    output = new BufferedWriter(new FileWriter("output_path/output.txt"));
    String newline = System.getProperty("line.separator");
    		
    // Read in a file & process line by line
    FileInputStream in = new FileInputStream("file_path/file.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    				
    while ((strLine = br.readLine()) != null) {
    			
    System.out.println(strLine);
    			
    // Open up standard input from command
    BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in)); 
    String command = null; 
    		      
    System.out.println("Delete line?");
    		      
    try { 
    command = br2.readLine(); 
    if (command.equals("delete")){
    System.out.println("Line Deleted.");
    System.out.println("");
    }else{
    // Write non deleted lines to file
    output.write(strLine);
    output.write(newline);
    }
    		    	 
    } catch (IOException ioe) { 
    System.out.println("IO error reading command line input");
    System.exit(1); 
    }
    			
    }
    output.close();
    System.out.println("End of file. DonCash is the man.");
      }
    }
    :o
    Last edited by DonCash; 03-28-2008 at 12:41 PM.

  3. #3
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: [SOLVED] Delete Current line from file

    What if we want to delete exactly the second line of a file at first attempt only? can we do this?

Similar Threads

  1. [SOLVED] Last line in JTextArea wont display
    By Chris.Brown.SPE in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 01:52 PM
  2. [SOLVED] Line number: ?
    By Azndaddy in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 05:46 AM
  3. [SOLVED] Save the current buffer to file
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 03-29-2008, 07:46 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 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
  •