File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Readin a text file and cuttin the text


Joined: Jan 18, 2002
Posts: 1
this following code cuts the string test
"This is not easy " into equal length peaceslike this:
is n
ot e
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(""))){
spacefound++; }
} }
for(int i=0;i<=(l-tmp.length());i++) tmp+=""+' '; }
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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!!

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.
[ January 18, 2002: Message edited by: kawaii desu ]
I agree. Here's the link:
subject: Readin a text file and cuttin the text
It's not a secret anymore!