Results 1 to 4 of 4
  1. #1
    amroosobhy is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default how to remove word words from txt file?

    Hello ,

    I have txt file containing alot of lines , each line contains of name and email address so I want to write program that removes all the names but remain the addresses as it is .

    The probleml is my program is deleting every thing at the file . It is in NetBeans environment as the following :

    public class Main {

    public static void main(String[] args) throws FileNotFoundException {

    File file = new File("t.txt");
    PrintWriter pw = new PrintWriter(file);
    Scanner in = new Scanner(new FileInputStream(file));
    int lineNumber = 0;
    while (in.hasNextLine())
    {
    lineNumber++;
    String line = in.nextLine();
    int x = line.lastIndexOf(' ');

    line.replaceFirst(line.substring(0, x),
    "");
    pw.print(line);
    }

    in.close();
    }

    }

    thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default

    I want to write program that removes all the names but remain the addresses as it is
    A couple of ways to edit a file. The safest is to read from the original, write out the selected/new text to a new temp file. Close both files. Rename/delete the original file and rename the temp file to the original file's name. Its save because you always have a full version of the data.
    Another way is to save all of the new records in memory and then write out those records over the top of the original file. This isn't as safe because if there is a problem writing out the new records and the program terminates, data can be lost.

  3. #3
    amroosobhy is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks Norm;
    but my idea in clearer way I have txt file in this contains the names of my contacts and thier emails as follows:
    name email@email.com
    name1 email1@email1.com
    etc...

    so I want to extract the emails only without names .
    could u please help me in this ? and my code - as atrial may contains errors -
    is in previous .
    thanks

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default

    I want to extract the emails only without names .
    Are you having problems with your code? Please explain.
    Showing example of what was read in and written out would help. Use a small input file for testing.
    Also add some comments if the output is not what you want explaining what is wrong with it.

Similar Threads

  1. Finding words (and more) in another file
    By Lund01 in forum Java Applets
    Replies: 5
    Last Post: 09-30-2010, 02:03 PM
  2. Read in words from a text file that contains a story
    By Pleenen in forum Advanced Java
    Replies: 2
    Last Post: 05-27-2010, 05:34 AM
  3. How to remove 2 last lines in a text file?
    By Marius in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 04:54 PM
  4. Replies: 7
    Last Post: 11-19-2008, 03:09 PM
  5. remove a portion from a file
    By alon2580 in forum New To Java
    Replies: 13
    Last Post: 08-25-2008, 02:45 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
  •