Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-17-2008, 09:40 AM
heartysnowy's Avatar
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
heartysnowy is on a distinguished road
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 09:44 AM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-17-2008, 09:56 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,393
Rep Power: 3
masijade is on a distinguished road
Default
If it has to do with a "word" document, then Google for "POI".
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-17-2008, 09:59 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,393
Rep Power: 3
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-17-2008, 10:11 AM
heartysnowy's Avatar
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
heartysnowy is on a distinguished road
Default
This is my code.
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-17-2008, 10:15 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-17-2008, 10:20 AM
heartysnowy's Avatar
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
heartysnowy is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-17-2008, 10:25 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,393
Rep Power: 3
masijade is on a distinguished road
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 10:29 AM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-17-2008, 10:26 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Yes it's possible. But you had to have a well design.

Using a counter and a logic you can do this.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-17-2008, 10:24 PM
Member
 
Join Date: Nov 2008
Posts: 64
Rep Power: 0
matzahboy is on a distinguished road
Default
You could always use readline and then String.concat in a for loop
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-18-2008, 04:27 AM
heartysnowy's Avatar
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
heartysnowy is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-18-2008, 04:34 AM
Member
 
Join Date: Nov 2008
Posts: 64
Rep Power: 0
matzahboy is on a distinguished road
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 " "
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 11-18-2008, 04:37 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,447
Rep Power: 8
Fubarable is on a distinguished road
Default
what are you trying to do with
Code:
if (s.equals(""))
?

Aren't you looking for "line break" or something similar, not the empty String, ""?
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 11-18-2008, 04:43 AM
Member
 
Join Date: Nov 2008
Posts: 64
Rep Power: 0
matzahboy is on a distinguished road
Default
Originally Posted by Fubarable View Post
what are you trying to do with
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 11-18-2008, 08:45 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 11-18-2008, 09:20 AM
heartysnowy's Avatar
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
heartysnowy is on a distinguished road
Default
This is my latest code:

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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 11-18-2008, 09:32 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,393
Rep Power: 3
masijade is on a distinguished road
Default
You are reading two lines through every iteration of the loop, and readLine will return null at EOF (enf of file). Change
Code:
if ((s=in.readLine()).equals(" ")){
to
Code:
if (s.equals(" ")){
That code, however, is not concatenating anything. Try this:
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 09:37 AM.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 11-18-2008, 09:38 AM
heartysnowy's Avatar
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
heartysnowy is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 11-18-2008, 09:45 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,393
Rep Power: 3
masijade is on a distinguished road
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 09:48 AM.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 11-18-2008, 10:12 AM
heartysnowy's Avatar
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
heartysnowy is on a distinguished road
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 10:20 AM.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 11-19-2008, 04:50 AM
heartysnowy's Avatar
Member
 
Join Date: Nov 2008
Posts: 37
Rep Power: 0
heartysnowy is on a distinguished road
Default
Hi again, is it possible to set the text allignment to 'justify' using java?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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] How do I read from a text file matzahboy New To Java 5 11-17-2008 05:47 AM
How to Modify,Delete data in File Txt??? hungleon88 Advanced Java 9 09-24-2008 04:19 AM
find and replace text from a text file gezzel New To Java 2 09-19-2008 05:04 PM
How to Read data from text file and calculate the values? janeansley New To Java 40 07-04-2008 09:41 AM
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 10:13 AM


All times are GMT +2. The time now is 01:25 AM.



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