简体   繁体   中英

How do I get my Java program to read the text file I want?

I'm new to Java and coding in general and I just can't get what I messed up here. I'm using Eclipse and I'm trying to get it to read from a simple text file that has two numbers in it "54 0.0" written just like that. I have the text file "ClimateData" saved right next to the this java file in the src folder and it still won't run the program. I don't know what I've done wrong

import java.util.Scanner;
import java.io.*;

public class ClimateSummary
{
public static void main (String [] args) throws FileNotFoundException
{

    Scanner textFile = new Scanner (new File("ClimateData"));

    int inputTemperature = textFile.nextInt();
    double inputPercipitation = textFile.nextDouble();

    if (inputTemperature > 90)  {
        System.out.print("It's miserably hot out man, ");

    }   else if (inputTemperature < 90 && inputTemperature > 80)    {
        System.out.print("It's kind of hot, ");

    }   else if (inputTemperature < 80 && inputTemperature > 60)    {
        System.out.print("It's pretty nice out, ");

    }   else if (inputTemperature < 60 && inputTemperature > 32)    {
        System.out.print("It's a bit cold out, ");

    }   else if (inputTemperature < 32) {
        System.out.print("It's miserably c-c-co-cold bro, ");
    }

    if (inputPercipitation > .1)    {
        System.out.print("and it's a little rainy.");

    }   else if (inputPercipitation <= .1)  {
        System.out.print("it's not rainy.");
    }

    textFile.close();

 }
}

I get this error:

Exception in thread "main" java.io.FileNotFoundException: ClimateData (The system  cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at ClimateSummary.main(ClimateSummary.java:11)

Try this to see where Java is looking for your file:

System.out.println(new File("ClimateData").getAbsolutePath());

Then move the file to wherever Java is looking.

Alternatively, you can use the getResource() methods to look for the file relative to your class files. More info on that approach here: getResourceAsStream() vs FileInputStream

you are getting the file not found error because the file is in the src folder you have to put "src/" in front of the path. Also you must put the file extension after it so it looks like this "src/ClimateData.txt". That should fix it.

将其移出src文件夹并移至项目根目录,因为这是任何启动的默认currend目录。

Yoy必须检查您的路径,并记住在Java中(如果您在Windows中),则应这样编写路径:

C:\\my folder\\mydocument.myextension

Scanner textFile = new Scanner (new File("ClimateData"));

Correction for your line is : Give file path specifying the source folder.

Scanner textFile = new Scanner (new File("src/ClimateData"));

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