aspose file tools*
The moose likes Beginning Java and the fly likes Readin a text file and cuttin the text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Readin a text file and cuttin the text" Watch "Readin a text file and cuttin the text" New topic
Author

Readin a text file and cuttin the text

kristian
Greenhorn

Joined: Jan 18, 2002
Posts: 1
this following code cuts the string test
"This is not easy " into equal length peaceslike this:
this
is n
ot e
asy
How do i have to change the code so that I can read the text from a file (for example "help.txt"),file can have several text lines and the result should be the same as above

String test=" This is not easy"; test=test.trim();
int spacefound=0;
String tmp="";
int l=test.indexOf(" ");
for(int i=0;i<test.length();i++){
char c=test.charAt(i);
if(c!=' ') tmp+=""+c;
if(c==' ' && (spacefound<1) && !(tmp.equals(""))){
tmp+=""+c;
spacefound++; }
if(tmp.length()==l){
System.out.println(tmp);
tmp="";
spacefound=0;
} }
if(tmp.length()<l){
for(int i=0;i<=(l-tmp.length());i++) tmp+=""+' '; }
System.out.println(tmp);
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
kristian
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!


Dave
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47

This will read one line at a time. You can do a while loop to get all the line. when the return value is null, it is end of file.
Ping
[ January 18, 2002: Message edited by: kawaii desu ]
 
Consider Paul's rocket mass heater.
 
subject: Readin a text file and cuttin the text