简体   繁体   中英

Program not reading txt file

I am a beginner Java student, working on our first class assignment. In this assignment, I need to read a txt file, and fill an array with its contents, first space in the array per line.

My professor gave us code to do this, but I keep getting an error that the file cannot be read each time I try. I am using Netbeans 8, on a Mac, and the file States.Fall2014.txt is located in the src folder, with all of my java classes.

Exception in thread "main" java.io.FileNotFoundException: States.Fall2014.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at main.main(main.java:21)
Java Result: 1

Here is the code I have. I have only included the code that pertains to opening the file, as I'm sure you have no wish to be spammed with the other classes. The commented code during the trimming is to echo print, to make sure the file is being read in properly (not currently needed since the file isn't being read in at all).

import java.io.*;

public class main {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String args[]) throws IOException {
        StateCollection Sdriver = new StateCollection(50);
         //Sdriver = new StateCollection(50);
        //Creates object of collection class
         FileReader fr= new FileReader("States.Fall2014.txt");
         BufferedReader br1 = new BufferedReader (fr);
          String inputString;  
      String stateName;
          String stateCapital;
          String stateAbbrev;
          int statePop;
          String stateRegion;
          int stateRegionNum;

          inputString = br1.readLine();
            while (inputString != null)
         {
           stateName = inputString.substring(1, 15).trim();
           //System.out.println("stateName read in was: " + stateName);
           stateCapital = inputString.substring(16, 30).trim();
           //System.out.println(“stateCapital read in was: “ + stateCapital);
           stateAbbrev = inputString.substring(31, 32).trim();
           //System.out.println(“stateAbbrev read in was: “ + stateAbbrev);
           statePop = Integer.parseInt(inputString.substring(33, 40));
           //System.out.println(“statePop read in was: “ + statePop);
           stateRegion = inputString.substring(41, 55).trim();
           //System.out.println(“stateRegion read in was: “ + stateRegion);
           stateRegionNum = Integer.parseInt(inputString.substring(56));
           //System.out.println(“stateRegionNum read in was: “ + stateRegionNum);
           //Code to create object
            inputString = br1.readLine();    // read next input line.
         }
         br1.close();     //Close input file being read

Make sure that the TXT file is in the right folder/area.

You shouldn't have it with your class, as the other answer states, you need it in the root folder.

将文件上移一级,与src文件夹相同。

src目录不是(不一定)该.class文件所在的目录。请确保在类路径中有States.Fall2014.txt。

Change

FileReader fr= new FileReader("States.Fall2014.txt");

to

FileReader fr= new FileReader("src/States.Fall2014.txt");

or move the file up one level to the project directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM