Results 1 to 3 of 3
  1. #1
    eden83 is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default How to read content from a file

    hi, not sure if i'm in the right place. i need some guide here. how do i read the content from a file. The name of file will different when created therefore not sure if able to use bufferedreader as it require a static filename to read the content. thanks in advance.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The name of file will different when created therefore not sure if able to use bufferedreader as it require a static filename to read the content

    I guess you are creating the BufferedReader instance with something like

    Java Code:
    String filename;
    // ...
    filename = ...
    // ...
    BufferedReader in = new BufferedReader(new FileReader(filename));

    To read the file the String filename must have same value or other: otherwise how can the computer figure out what file is to be read? It doesn't have to be a string literal (if that is what you mean by "static"), it can come from user input, or be based on the date or any other way you can dream up to construct a string.

    Java Code:
    String filename;
    Scanner scan = new Scanner(System.in);
    // ...
    System.out.print("What file would you like to read today?  ");
    filename = scan.readLine();
    BufferedReader in = new BufferedReader(new FileReader(filename));

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Google "File Handling in java." Also, The I/O (Input/Ouput) tutorial by Sun is very insightful. Here is the link: Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Similar Threads

  1. Read PDF Content
    By Deepa in forum New To Java
    Replies: 3
    Last Post: 11-03-2009, 11:55 AM
  2. Read text content from pdf using pdfbox
    By cssatheesh in forum Advanced Java
    Replies: 0
    Last Post: 09-22-2009, 09:54 PM
  3. how to read content of .xls file
    By kirtichopra2003 in forum Advanced Java
    Replies: 10
    Last Post: 09-11-2009, 02:03 PM
  4. Read the content of file from a specified path
    By shiva in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 08:31 AM
  5. Replies: 0
    Last Post: 02-11-2009, 10:53 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
  •