Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default Read and modify text file

    Hello everyone, I am still a student and new to JAVA. I would like to write a program that read from a text file and make some modification , and save it into a word document.
    What I mean by modification is : I have a paragraph in the text file as shown below;

    line 1~ "Our records indicate that you have never posted
    line 2~ to our site before! Why not make your first post
    line 3~ today by saying hello to our community
    line 4~ in our Introductions forum."

    As you can see, the alignment of the paragraph is not right and i want all of them in 1 full sentence.
    Please, any help would be greatly appreciated.

    *****
    EDITED : Sorry for my bad interpreting at the first post so let me rephrase the program that I want.

    The program that 1.read and concatenate the texts untill it find the "line break"
    2. skip to next line once it find the "line break" and repeat step 1.

    *****

    Thanks and Regards,
    Hearty
    Last edited by heartysnowy; 11-18-2008 at 08:44 AM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    If it has to do with a "word" document, then Google for "POI".

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    As far as reading it goes. Simply use a BufferedReader and it's readLine method (which strips the newline) and concatenate those reads together.

    Make sure that either the first ends with whitespace, or the next begins with whitespace (String has methods for this). Otherwise, add a space between them when concatenating.

  4. #4
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    This is my code.
    Java Code:
    public class FileTest{	
    
    	public static void main(String args[]){
    		int count = 0, ch;
    		try{
    	
            	File f1 = new File("C:/Documents and Settings/dd/Desktop/outlook.txt");
    			BufferedReader in = new BufferedReader(new FileReader(f1));
    			File f2 = new File("C:/Documents and Settings/dd/Desktop/outlook3.doc");
    			BufferedWriter out = new BufferedWriter(new FileWriter(f2));
    			String s="";
    			int lineCount=0;
    			while ((s=in.readLine())!=null){
    				out.write(s);
    				out.newLine();
    				System.out.println(s);
    				lineCount++;			}
    					
    			System.out.println("Number of lines read = "+lineCount);
    			
    		
    			in.close();  
                 out.close();
    			}
    		
    		catch(IOException e){
    			System.out.println("I/O Error occurred");
    		}
    	}
    }
    I knew how to read text files and export to a new document. But i have no idea know how to combine the lines into 1 line. Can you please support me with codes?

    Thanks and regards,
    Hearty

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by heartysnowy View Post
    I knew how to read text files and export to a new document. But i have no idea know how to combine the lines into 1 line. Can you please support me with codes?
    You mean that you want to know how to add two lines and make a new one?

  6. #6
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    yup exactly, but no all the lines. Just want to add the specific line...e.g

    line 1 ~ words words words
    line 2 ~ words words words
    line 3 ~ "line break"
    line 4 ~ words words words
    line 5 ~ words words words

    As shown above, i would like to add the lines(line1+line2 and line4+line5) that have no line break in between them. Could it be posibily done?

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Like I said, readLine strips the newline so simply do String concatenation with the Strings that readLine returns (i.e. str + str).

    Edit: When bufferedReader returns an "empty String", that's your "line break" that you're talking about.

    Edit Again: So, as I said, simply concatenate the Strings, then do your "write and newline" whenever the next String to be concatenated is an empty String. Also, that thing you're writing is not a word document, regardless of the ending you've given. You are writing a simple text document and forcing the system (if the file associations are setup properly) to use word to open it, but it is still a simple text document.
    Last edited by masijade; 11-17-2008 at 09:29 AM.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes it's possible. But you had to have a well design.

    Using a counter and a logic you can do this.

  9. #9
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    You could always use readline and then String.concat in a for loop

  10. #10
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    Thanks for all the relpy , i was able to concatenate the string but all the text appears in one line. How shall I detect the line which has a line break(refer to #6) , put a line break in a text too and continue processing?

    I use: If (s.equals("")) {
    out.newLine();
    }

    but it is not working. I am a little dumb with code so helps would be gladly appreciated.

  11. #11
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    I'm not sure (since I'm new to I/O), but you could put a space in the blank line and then check if it equals " "

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    what are you trying to do with
    Java Code:
    if (s.equals(""))
    ?

    Aren't you looking for "line break" or something similar, not the empty String, ""?

  13. #13
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    what are you trying to do with
    Java Code:
    if (s.equals(""))
    ?

    Aren't you looking for "line break" or something similar, not the empty String, ""?
    I think that she was guessing that "" meant a line break. She was taking a guess at how to find one.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by heartysnowy View Post
    Thanks for all the relpy , i was able to concatenate the string but all the text appears in one line. How shall I detect the line which has a line break(refer to #6) , put a line break in a text too and continue processing?

    I use: If (s.equals("")) {
    out.newLine();
    }

    but it is not working. I am a little dumb with code so helps would be gladly appreciated.
    Can you show the code segment that you have concatenate and do the test for new line?

  15. #15
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    This is my latest code:

    Java Code:
    	public static void main(String args[]){
    		int count = 0, ch;
    		try{
    	
            	File f1 = new File("C:/Documents and Settings/kyawzt/Desktop/outlook.txt"); 
    			BufferedReader in = new BufferedReader(new FileReader(f1));
    		File f2 = new File("C:/Documents and Settings/kyawzt/Desktop/aus1.doc"); 
    			BufferedWriter out = new BufferedWriter(new FileWriter(f2));
    			
    			String s="";
    			int lineCount=0;
    			
    			
    			while ((s=in.readLine())!=null){     				
    				
    				out.write(s);
    			
    				if ((s=in.readLine()).equals(" ")){
    					out.newLine();
    					out.newLine();	
    					}
    					
    					
    				System.out.println(s);
    				lineCount++;			  
     			}					
    			
    			
    			
    					
    	      System.out.println("Number of lines read = "+lineCount);		
    	      in.close();  
                 out.close();
    			}
    		catch(NullPointerException ee){
    			System.out.println("Null pointer error");
    		}
    			
    		catch(IOException e){
    			System.out.println("I/O Error occurred");
    		}
    	}
    Currently I got a null pointer exception at the "if" condition line.

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    You are reading two lines through every iteration of the loop, and readLine will return null at EOF (enf of file). Change
    Java Code:
    if ((s=in.readLine()).equals(" ")){
    to
    Java Code:
    if (s.equals(" ")){
    That code, however, is not concatenating anything. Try this:
    Java Code:
        String s = "";
        String line = "";
        int lineCount = 0;
    
        while ((s=in.readLine())!=null){     				
            line += s.trim() + " ";
            if (s.trim().length() == 0) {
                out.write(line);
                out.newLine();	
                out.newLine();	
                line = "";
            }
            System.out.println("---" + s + "---");  // I assume only for testing, the --- lets you "see" whitespace
            lineCount++;			  
         }
    Last edited by masijade; 11-18-2008 at 08:37 AM.

  17. #17
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    Yes, i have tried that b4 as you can see from my previous post.
    Even though it shows no error, the problem is that the system doesn't not detect a line break and concatenate all the texts.

  18. #18
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    How is the "line break" defined in your file? From what you posted previously it should be a blank line, in which case the above will work (you have tried the trim, right?). If it is defined differently, then, of course, you need to check for something else, obviously.

    And, nowhere in any of your posts have you tried what I posted here. You have tried some things that are superficially similar, but not this.

    Edit:

    P.S. When I post something don't take a quick look and immediately dismiss it with "I've already tried that". I don't make redundant posts here (unless absolutely necessary because people are refusing to believe what they read) and I post, what I post, for a reason, if you wish to ignore it, then tell me that and I will stop attempting to help you.
    Last edited by masijade; 11-18-2008 at 08:48 AM.

  19. #19
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    thanks alot...it works !! at the first glance i didn't see the 2nd code below...now i just need to add in a few gui and it wil b done soon :P....thanks to all supporters and masi for the codes ^^

    P.S Its not that i didn't tried it..it's bcoz by the time i started writing my previous post , u 'hv just modified ur post (you can see the time different is 1 min). I deeply read,evaluate and follow all the posts here because i knew ppl are helping me. I don't recklessly ignore those who helps me. Anw, dearly thanks for all the help :)
    Last edited by heartysnowy; 11-18-2008 at 09:20 AM.

  20. #20
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    Hi again, is it possible to set the text allignment to 'justify' using java?

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] How do I read from a text file
    By matzahboy in forum New To Java
    Replies: 5
    Last Post: 11-17-2008, 04:47 AM
  2. How to Modify,Delete data in File Txt???
    By hungleon88 in forum Advanced Java
    Replies: 9
    Last Post: 09-24-2008, 03:19 AM
  3. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  4. Replies: 40
    Last Post: 07-04-2008, 08:41 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •