Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Can't find file?

    Hi, I'm trying to read lines from a text file using file and a scanner objects. According to my Java book this should be right, but it doesn't work.
    I've tried placing the file in multiple directories, including the folder the project is in as well as other locations. I've also done static locations not specified by a variable.


    The file is meant to read names and scores from a list of first and last name and int score line by line. That's not really important however, as I can't even read the file I need to.
    Here's my code:


    Java Code:
       import java.io.*;
       import java.util.*;
       public class TestScoreReader {
       
          public static void main (String[] args)
          { 
          //creates keyboard scanner
             Scanner in = new Scanner(System.in);
             //prints directory file should be in
             System.out.println ("Working directory = " + System.getProperty("user.dir"));
             // prompts for file name
             System.out.println("Enter name of file with one 'firstname lastname %score' entry per line:");
             String fileName = in.nextLine();
             //prompts for name of requested average
             System.out.println("Enter name average is requested of:");
             String name = in.nextLine();
          	
             try 
             {
             /*
                FileInputStream fstream = new FileInputStream(fileName);
                DataInputStream instream = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(instream));
             */
             
             //File inputFile = new File("C:\\"+fileName);
                File inputFile = new File("data.txt");
             
                Scanner fileIn = new Scanner(inputFile);
             
             	
                String rawString;
                String listName;
                int i;
                String scoreString;
                int score;
                int sum=0;
                int scores=0;
                double avg = 0;
             	
             	
             	
             	
             	
             	
                //while (br.readLine() != null)
                // uses test string instead of file load
                
                while (fileIn.hasNextLine()) 
                {
                   //rawString=br.readLine();
                   rawString = fileIn.nextLine();
                	//subsect string
                   i=0;
                   while (!Character.isDigit(rawString.charAt(i)))
                   {
                      i++;// increment counter until a digit is found
                   }
                   listName = rawString.substring(0,i);// trims name to limit of i
                   listName = listName.trim();//takes off any spaces after name
                   scoreString = rawString.substring(i);//takes all numbers i and up, trims, converts to int
                   score = Integer.parseInt(scoreString.trim());
                //if name from the file equals desired name
                   if (listName.equals(name))
                   {
                      sum=sum+score;// add score to sum
                      scores++;// increase count of scores
                   }
                }
             //calculates average
                avg = (double)sum / (double)scores;
             //prints
                System.out.println(avg);
             
             
                //instream.close();
             }
             
                catch (Exception e)
                {
                   e.printStackTrace();
                }
          }
       }
    Thanks much for any help!

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    811
    Rep Power
    6

    Default

    //Just check file exist is printing true or not using
    //inputFile.exists() after this line before passing to scanner.
    File inputFile = new File("data.txt");
    Ramya:cool:

  3. #3
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the response, but even when I'm positive the input file does exist, it still isn't found.

    Any ideas?

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by rockgardenlove View Post
    Any ideas?

    yes, if you define your File with new File("data.txt") the file will be searched in the project root. i made a test and put a file data.txt in my project root and after

    System.out.println(inputFile.exists());

    true was printed.

  5. #5
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    With my file I still get false!?
    Did you use my exact code?
    Here's the stack trace:
    java.io.FileNotFoundException: data.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.util.Scanner.<init>(Scanner.java:636)
    at TestScoreReader.main(TestScoreReader.java:30)

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You might try a couple things. First, make a new file in this manner:
    Java Code:
    File file = new File("myFile.txt");
    And then put the myFile.txt into the same folder as the project. If you are using eclipse or netbeans, just drop it in the project folder itself. If doing this manually, put it in the same folder as the .class files.
    Now try your file.exists();

    If you want the flexibility of opening a file from anywhere, look at the JFileChooser. It allows you to show a chooser, select a file in a file browser, and then get the selected file from the chooser.

  7. #7
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Cannot find file for FileReader
    By Spooge in forum New To Java
    Replies: 11
    Last Post: 10-20-2010, 11:12 PM
  2. Replies: 1
    Last Post: 02-05-2010, 04:53 PM
  3. Replies: 0
    Last Post: 02-05-2010, 03:49 PM
  4. how to find class name when have only jar file?
    By matvey in forum Java Applets
    Replies: 9
    Last Post: 10-13-2009, 01:49 PM
  5. find file with * on the path
    By itaipee in forum New To Java
    Replies: 7
    Last Post: 07-08-2009, 08:05 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
  •