質問

I have a simple text file and for some reason, it cannot be found. I don't see anything wrong with the code because I got it from a site, and I am starting to think that I didn't place the text file in the right place. Any suggestion please?

Code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MainFavorites {
    public static void main(String[] args) throws IOException {
        /**
        * finds pathway to the file
        */
        //      File file = new File("icecreamTopping.txt");
        //      System.out.println(file.getCanonicalPath());

        BufferedReader reader = null;
        ArrayList <String> myFileLines = new ArrayList <String>();
        try {
            String sCurrentLine;
            reader = new BufferedReader(new FileReader("icecreamTopping.txt"));
            while ((sCurrentLine = reader.readLine()) != null) {
                //System.out.println(sCurrentLine);
                myFileLines.add(sCurrentLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.print(e.getMessage());
        } finally {
            try {
                if (reader != null)reader.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
                ex.printStackTrace();
            }
        }
        int numElements = myFileLines.size();
        System.out.println ("there are n lines in the file:" + numElements);

        for (int counter = numElements-1; counter >= 0; counter--) {
            String mylineout = myFileLines.get(counter);
            System.out.println (mylineout);
        }
    }
}

File content:

1- Blueberry 
2- Banana Buzz
3- Cookie Batter

My stack trace is this:

java.io.FileNotFoundException: C:\Users\homar_000\workspace\RankFavorites\icecreamTopping.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at MainFavorites.main(MainFavorites.java:28)
役に立ちましたか?

解決 2

Found out what was the problem. It was unnecessary for me to put the file extension so I removed the .txt because when I kept it, it read it as "icecreamTopping.txt.txt"

他のヒント

Replace below line

reader = new BufferedReader(new FileReader("icecreamTopping.txt"));

with

reader = new BufferedReader(new FileReader("resources/icecreamTopping.txt"));

and put the file under resources folder that resides parallel to src folder.


Sample code:

Reading a file abc.txt from resources folder

reader = new BufferedReader(new FileReader("resources/abc.txt"));

Here is the project structure

enter image description here


Try below code to find out where it is pointing to file icecreamTopping.txt.

 File f=new File("icecreamTopping.txt");
 System.out.println(f.getAbsolutePath());

After getting the absolute path, just place the file there.


--EDIT--

As per your last comments, put icecreamTopping.txt file in the project RankFavorites directly as shown in below snapshot and It will definitely solve your problem.

enter image description here

Move the text file to java/main/resources folder if you are using Maven or to classes folder, so that it will be in classpath. Then use the following line of code to get the file path. Replace MyClass with your class name. Use the path as argument in FileReader.

String path = MyClass.class.getClassLoader().getResource("text_file.txt").getPath();

BufferedReader reader = new BufferedReader(new FileReader(path));

Try to use File class to detect your file in storage:

File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

BufferedReader br = new BufferedReader(new FileReader(file));
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


JSONParser parser = new JSONParser();
ObjectMapper mapper = new ObjectMapper();

try {
    
    Object obj = parser.parse(new FileReader("/home/sahan/Desktop/data.json"));
    ObjectNode objNode = mapper.convertValue(obj, ObjectNode.class);
    System.out.println(objNode);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (ParseException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top