Results 1 to 20 of 28
Thread: Read and modify text file
- 11-17-2008, 08:40 AM #1
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,
HeartyLast edited by heartysnowy; 11-18-2008 at 08:44 AM.
- 11-17-2008, 08:56 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
If it has to do with a "word" document, then Google for "POI".
- 11-17-2008, 08:59 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 11-17-2008, 09:11 AM #4
This is my code.
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?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"); } } }
Thanks and regards,
Hearty
- 11-17-2008, 09:15 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-17-2008, 09:20 AM #6
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?
- 11-17-2008, 09:25 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 11-17-2008, 09:26 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes it's possible. But you had to have a well design.
Using a counter and a logic you can do this.
- 11-17-2008, 09:24 PM #9
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
You could always use readline and then String.concat in a for loop
- 11-18-2008, 03:27 AM #10
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-18-2008, 03:34 AM #11
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
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 " "
-
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, ""?
- 11-18-2008, 03:43 AM #13
Member
- Join Date
- Nov 2008
- Posts
- 67
- Rep Power
- 0
- 11-18-2008, 07:45 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-18-2008, 08:20 AM #15
This is my latest code:
Currently I got a null pointer exception at the "if" condition line.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"); } }
- 11-18-2008, 08:32 AM #16
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
You are reading two lines through every iteration of the loop, and readLine will return null at EOF (enf of file). Change
toJava Code:if ((s=in.readLine()).equals(" ")){
That code, however, is not concatenating anything. Try this:Java Code:if (s.equals(" ")){
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.
- 11-18-2008, 08:38 AM #17
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.
- 11-18-2008, 08:45 AM #18
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 11-18-2008, 09:12 AM #19
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.
- 11-19-2008, 03:50 AM #20
Similar Threads
-
[SOLVED] How do I read from a text file
By matzahboy in forum New To JavaReplies: 5Last Post: 11-17-2008, 04:47 AM -
How to Modify,Delete data in File Txt???
By hungleon88 in forum Advanced JavaReplies: 9Last Post: 09-24-2008, 03:19 AM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM -
How to Read data from text file and calculate the values?
By janeansley in forum New To JavaReplies: 40Last Post: 07-04-2008, 08:41 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks