Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: Editing a text File

  1. #1
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Editing a text File

    I have a txt file containing 600.000+ words (one word each line). I want to edit it so that words having more then 8 letters will be deleted.

    Here is my code:
    Java Code:
    public static void main(String[] args) {
           
            try {
                FileInputStream fstream = new FileInputStream("words.txt");
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                StringBuilder fileContent = new StringBuilder();
                while ((strLine = br.readLine()) != null) {
                    if(strLine.length()<8) 
                    {fileContent.append(strLine+" \n");}
                }
    
                // Now fileContent will have updated content , which will be writen in second txt file
                FileWriter fstreamWrite = new FileWriter("words2.txt");
                BufferedWriter out = new BufferedWriter(fstreamWrite);
                out.write(fileContent.toString());
                out.close();
                in.close();
            } catch (Exception e) {System.err.println("Error: " + e.getMessage());}    
        }
    My code works fine, but the new file has the words separated only by a space (" "). I want them to be one word on each line.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Editing a text File

    one word on each line.
    Be sure to write out a line separator after each word.

    What program are you viewing the output file in?
    Wordpad honors \n, notepad requires \r\n
    Last edited by Norm; 04-29-2013 at 10:40 PM.
    alexix likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Re: Editing a text File

    Thank you. didn't know about \r

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Editing a text File

    Better is to use the System property:
    Java Code:
     String lineSep = System.getProperties().getProperty("line.separator");
    Then it should work on different OSs
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Editing a text File

    Or just ignore Notepad, as it's about the only thing that can't handle '\n' these days.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Editing a text file
    By alexix in forum New To Java
    Replies: 1
    Last Post: 02-12-2013, 12:31 AM
  2. Simple Text File Editing in Java help
    By DMarsh12 in forum New To Java
    Replies: 1
    Last Post: 10-22-2011, 09:55 PM
  3. Editing text files
    By NickBru in forum New To Java
    Replies: 4
    Last Post: 12-05-2010, 06:27 PM
  4. Text editing
    By right2001 in forum New To Java
    Replies: 1
    Last Post: 04-10-2009, 07:03 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
  •