Results 1 to 7 of 7

Thread: text file

  1. #1
    java_kiddy is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default text file

    Java Code:
    package fileinput;
    import java.io.*;
    /**
     *
     * @autho
     */
    public class Main {
    
        public Main () {
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
         try
         {
             FileInputStream fstream =
                     new FileInputStream("data.txt");
             DataInputStream in = new DataInputStream(fstream);
             BufferedReader br = new BufferedReader(new InputStreamReader(in));
             String strLine;
    
             while((strLine = br.readline()) != null)
             {
                 System.out.println(strLine);
    
             }
             in.close();
         }
         catch(Exception e)
         {
             System.err.println("Error:" + e.getMessage() );
        }
        }
    }
    i am trying to load a text file which is in my C: drive, but dont know what to enter for C://, plus the

    Java Code:
    while((strLine = br.readline()) != null)
    is stating Cant find Symbol. Symbol ReadLine() location class java.io.BufferedReader

    i take it this error is showing as it cant find the text file ? i have Data in the Text file when the program runs the file opens and whats inside the file should show up in the program.

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

    Default

    is stating Cant find Symbol. Symbol ReadLine() location class java.io.BufferedReader

    What it's really saying is "Can't find Symbol. Symbol readline() location class java.io.BufferedReader". (In general copy, paste and post these messages so we can see them exactly as they are.)

    This is because there is no readline() method in the BufferedReader class. Have a read of the BufferedReader API documentation and see what the method is really called.

  3. #3
    java_kiddy is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    how to i add the dir of the text file into the

    Java Code:
    FileInputStream fstream =
                     new FileInputStream("data.txt");
    after i run it
    debug:
    Error:data.txt (The system cannot find the file specified)
    BUILD SUCCESSFUL (total time: 8 seconds)

    to fix the BufferedReader what should i enter?
    I am using NetBean

    cannot find symbol
    symbol: method readline()
    location: class java.io.BufferedReader
    --
    (Alt-Enter shows hints)

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

    Default

    Did you follow the link? It mentions 9 methods, one of which is the correct spelling for readline().

    One way of specifying the file location is new FileInputStream("dir/data.txt") (note the use of a foward slash). More flexibly you could use the FileInputStream constructor that takes a File argument.

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

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

    Default

    Again! I won't claim that it's a case of great minds thinking alike. But it certainly looks like a case of a less great one unwilling to think at all.

    @OP: you will be doing us all - yourself included - a favour if you are upfront about cross posting and provide a link (on both sites) as Norm has done for you. As with any conversation, people like to know what's being said.

    And it is a conversation. So not only is it worth checking out a link if two people provide it, but it's also good form to reply to them if, again independently, they ask you a question.

  7. #7
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Is there a difference if we use scanner?

    try{
    Scanner sc=new Scanner(new File("Path/filename.txt"));


    while(sc.hasNext()){
    String line=sc.nextLine();
    System.out.println(line);

    }
    sc.close();
    }
    catch (Exception e) {
    System.out.print("File not found");
    }

    }

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2010, 10:18 AM
  2. Replies: 3
    Last Post: 06-08-2010, 08:10 PM
  3. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  4. Replies: 8
    Last Post: 11-06-2009, 06:52 AM
  5. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM

Posting Permissions

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