aspose file tools
The moose likes I/O and Streams and the fly likes Reading every line from a file to string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "Reading every line from a file to string" Watch "Reading every line from a file to string" New topic
Author

Reading every line from a file to string

kristian jarvi
Greenhorn

Joined: Jan 19, 2002
Posts: 9
I'm trying to parse a text file into equal length
peaces..and put one space to separate words. if the next text is in the file "eka.txt"
hello kathy
howare
you
doing
the outcome should be
hello
kathy
how a
re yo
u doi
ng
How can i read every line from my text file ("eka.txt")
now it only reads the first line and prints it out like this
hello
kathy
here is the code!
import java.io.*;
import java.util.*;
class Testi{
public static void main(String []args)throws IOException {
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

File inputFile = new File ("eka.txt");
FileReader fis =new FileReader(inputFile);
BufferedReader bis = new BufferedReader(fis);
String test=bis.readLine();
String tmp= "";
String nextLine;
while((nextLine=bis.readLine()!= null)) {
nextLine=nextLine.trim();
int spacefound=0;
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);
}
}
Napa Sreedhar
Ranch Hand

Joined: Jan 29, 2002
Posts: 58
space is treated as a character.

Try this program

import java.util.*;
import java.io.*;
public class Test
{
private int SEPERATOR = 5;

public Test()
{
try
{
this.makeTrim();
}
catch(Exception Exp)
{
System.out.println("Excpeiton raised" + Exp);
}
}
public static void main(String[] args)
{
new Test();
}
public void makeTrim()
throws Exception
{
FileReader fr = new FileReader("c:\\tmp.txt");
BufferedReader br = new BufferedReader(fr);
String next_line = null;
String curr_line = br.readLine();
while (curr_line != null)
{
int start_index = 0;
int last_index = SEPERATOR;
int length = curr_line.length();

// if line length >= 5
if (length >= SEPERATOR)
{
int repeat = length/SEPERATOR;
for (int i = 0; i < repeat; i++)
{
// cut 5 chars
String line = curr_line.substring(start_index, last_index);
System.out.println(line);
start_index = last_index;
last_index = start_index + SEPERATOR;
}
// store remaining chars in next_line
next_line = curr_line.substring(start_index, length);
}
else
{
// store chars in next_line
next_line = curr_line;
}

String temp = null;
if ((temp = br.readLine()) == null)
{
break;
}
curr_line = next_line + temp;
} // while
}
public void log(String msg)
{
System.out.println(msg);
}
}
kristian jarvi
Greenhorn

Joined: Jan 19, 2002
Posts: 9
Thank's Napa I'll try that!
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hello Kristian,
it might be quite late for an answer, but I think you've just mixed up your variables "String test" and "String nextLine". "test" holds your first line while "nextLine" is replaced with each following line.
 
 
subject: Reading every line from a file to string
 
Threads others viewed
Problems using/making method!
Reading txt file where tab is used?
I Need help with file handling
Readin a text file and cuttin the text
how to check if file is empty
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture