Results 1 to 4 of 4
  1. #1
    Mleclerc182 is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default How to print an input file?

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;


    public class TestBookCollection {
    public static void main(String args[]){
    File books = new File ("C:\\books.txt");
    System.out.println(books);
    }
    }


    Output: C:\books.txt

    I know that it is input correctly but how do I get the content within the actual file to print?
    Last edited by Norm; 09-17-2012 at 02:43 AM. Reason: Not Advanced. Moved to New

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: How to print an input file?

    how do I get the content within the actual file
    You need to read the lines from the file so you can print them.
    The Scanner class is easy to use for text files. Define an instance of Scanner with the File and use its method to read the lines from the file.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How to print an input file?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Cool Re: How to print an input file?

    ----------------------------------------------------------------------------
    Initially, read the file and contents'
    ----------------------------------------------------------------------------
    '
    Java Code:
    FileReader fx=new FileReader("D:/output.txt");
    BufferedReader bx=new BufferedReader(fx);
    '
    ----------------------------------------------------------------------------
    Declare a String and put contents in String
    ----------------------------------------------------------------------------
    '
    Java Code:
    String holdcontents;
    holdcontents=bx.readLine();
    '

    ----------------------------------------------------------------------------
    Display the String'
    ----------------------------------------------------------------------------
    '
    Java Code:
    System.out.print(holdcontents);
    '

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2012, 08:05 PM
  2. Print Parts Of An Array [User Input]
    By PrimalScientist in forum New To Java
    Replies: 22
    Last Post: 08-30-2011, 11:09 AM
  3. Replies: 12
    Last Post: 03-28-2011, 09:30 PM
  4. Replies: 3
    Last Post: 02-13-2011, 08:54 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Tags for this Thread

Posting Permissions

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