Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-28-2008, 12:10 AM
Member
 
Join Date: Mar 2008
Posts: 24
Azndaddy is on a distinguished road
[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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-28-2008, 01:37 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
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.

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 01:41 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Last line in JTextArea wont display Chris.Brown.SPE Advanced Java 5 04-11-2008 02:52 PM
[SOLVED] Line number: ? Azndaddy New To Java 1 04-04-2008 06:46 AM
[SOLVED] Save the current buffer to file Azndaddy New To Java 2 03-29-2008 08:46 AM
Reading in data from file line by line bluekswing New To Java 1 10-02-2007 01:19 AM
How to delete a file Alpha New To Java 1 05-26-2007 09:11 AM


All times are GMT +3. The time now is 01:28 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org