Results 1 to 7 of 7
Thread: Can't find file?
- 02-08-2011, 08:33 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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:
Thanks much for any help!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(); } } }
- 02-08-2011, 08:55 AM #2
//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:
- 02-08-2011, 03:58 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Thanks for the response, but even when I'm positive the input file does exist, it still isn't found.
Any ideas?
- 02-08-2011, 04:28 PM #4
- 02-08-2011, 04:39 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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)
- 02-08-2011, 10:53 PM #6
You might try a couple things. First, make a new file in this manner:
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.Java Code:File file = new File("myFile.txt");
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.
- 02-09-2011, 03:30 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Cannot find file for FileReader
By Spooge in forum New To JavaReplies: 11Last Post: 10-20-2010, 11:12 PM -
JBOSS DeploymentException : Not able to find module file : .war in a .ear file
By shikha3g in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 02-05-2010, 04:53 PM -
JBOSS DeploymentException : Not able to find module file : .war in a .ear file
By shikha3g in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 02-05-2010, 03:49 PM -
how to find class name when have only jar file?
By matvey in forum Java AppletsReplies: 9Last Post: 10-13-2009, 01:49 PM -
find file with * on the path
By itaipee in forum New To JavaReplies: 7Last Post: 07-08-2009, 08:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks