Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why won't it read file?

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why won't it read file?

    Hello so for this problem i have to read a text file but for some reason it won't let me do it.

    this is my code:


    import java.io.BufferedReader; 
    import java.io.FileReader; 
    import java.io.IOException; 
    import java.util.Scanner; 
     
    public class Assignment102 { 
     
        public static void main(String[] args) { 
            try { 
                // Initialize min and max 
                int min = Integer.MAX_VALUE; 
                int max = Integer.MIN_VALUE; 
     
                // Construct a string with the name of the input file. You 
                // can use this to construct an appropriate Reader object. 
                String filename = "tai8.txt"; 
     
                FileReader fis = new FileReader(filename);
                BufferedReader b = new BufferedReader(fis);
     
                String line;
                while ((line = b.readLine()) != null)
                {
                    int number = Integer.parseInt(line.trim());
                    if (number < min) min = number;
                    if (number > max) max = number;
                }
     
                b.close();
                fis.close();
     
                // Print the results 
                System.out.println("Minimum value: " + min); 
                System.out.println("Maximum value: " + max); 
            } 
            catch(Exception ioe) { 
                ioe.printStackTrace(); 
            } 
        } 
     
    }

    when i compile it i get this error message:

    java.io.FileNotFoundException: tai8.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:120)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:79)
    at java.io.FileReader.<init>(FileReader.java:41)
    at assignment.pkg10.pkg2.Assignment102.main(Assignmen t102.java:29)

    i'm not sure how to put the text file in the same directory as the java file. maybe that's what the issue is? can someone please help me with this? Thank you.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why won't it read file?

    You need to specify the absolute or relative path to the file. What you have done thus far in the source code is specify the relative path pointing to a file in the same directory as the java class file - move the text file into the same directory. You state you don't know how - this is more of an OS problem than a java problem, and I suggest solving this prior to anything else, as navigating an OS is the foundation to using a computer.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why won't it read file?

    How do I do that? I don't know how to place the text file in the same directory as the java file.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why won't it read file?

    Quote Originally Posted by tai8 View Post
    How do I do that? I don't know how to place the text file in the same directory as the java file.
    You've never moved a file on your computer before? Drag the file icon of one and drop it into the directory of the other.

Similar Threads

  1. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  2. [SOLVED] Can I have two scanners to the same file? (I want to read through one file 2 times)
    By beginnerprogrammer in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 20th, 2011, 11:23 AM
  3. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. how to read file name inside .gz file....
    By kathir0301 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 6th, 2010, 10:06 AM