Results 1 to 3 of 3

Thread: print line

  1. #1
    kazitula is offline Member
    Join Date
    Oct 2007
    Posts
    10
    Rep Power
    0

    Lightbulb 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...

    Java 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); 
      }
     } 
    }

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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()....
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    sabbirhs is offline Member
    Join Date
    Jan 2008
    Posts
    6
    Rep Power
    0

    Default

    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.

Similar Threads

  1. how to print output on same line in 'while loop'?
    By acidblue in forum New To Java
    Replies: 5
    Last Post: 12-13-2007, 02:30 AM
  2. Print Area
    By Riftwalker in forum Advanced Java
    Replies: 0
    Last Post: 11-28-2007, 07:28 PM
  3. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM
  4. Print XML tag
    By DonCash in forum XML
    Replies: 2
    Last Post: 08-07-2007, 06:02 PM
  5. Print A Pdf
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 05:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •