-
print line
hi
how can i print a sentence in different lines? like... "this is for the test of java"
so if i want to print this in the following way...
this
is
for
the
test
of
java
here is my code...
Code:
import java.io.*;
public class FileReading
{
// Main method
public static void main (String args[])
{
// Stream to read file
try
{
// Open an input stream
FileInputStream fileinput = new FileInputStream ("c:\\readfile.txt");
// Read a line of text
// System.out.println( new DataInputStream(fileinput).readLine() );
DataInputStream in = new DataInputStream(fileinput);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
// Close our input stream
fileinput.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}
}
}
-
Well, basically, unless there is a method in API that I am unaware of ... you should treat the whitespace character differently than it is normally processed. For example, when you read the character, treat it as a newline. You probably can guess this... but if do you know this, you should be able to come with the way to do it. Also, you read in the data line by line.. so maybe you should use... read() or something like... readChar()....
-
there are some string fuction which take care about spaces and \n, i think youshould look at those. other then you can read string by string rather then line by line.