Results 1 to 3 of 3
- 03-27-2008, 11:10 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 24
- Rep Power
- 0
[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
- 03-28-2008, 12:37 PM #2
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.
:oJava 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."); } }Last edited by DonCash; 03-28-2008 at 12:41 PM.
- 04-06-2012, 08:00 AM #3
Member
- Join Date
- Feb 2012
- Location
- Bangalore
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
[SOLVED] Last line in JTextArea wont display
By Chris.Brown.SPE in forum Advanced JavaReplies: 5Last Post: 04-11-2008, 01:52 PM -
[SOLVED] Line number: ?
By Azndaddy in forum New To JavaReplies: 1Last Post: 04-04-2008, 05:46 AM -
[SOLVED] Save the current buffer to file
By Azndaddy in forum New To JavaReplies: 2Last Post: 03-29-2008, 07:46 AM -
Reading in data from file line by line
By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM -
How to delete a file
By Alpha in forum New To JavaReplies: 1Last Post: 05-26-2007, 08:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks