-2

I am trying to open a txt file in NetBeans 12.6 using Java. I placed the txt file in the main project file. When I try to open the file using the File and Scanner method I keep getting the "Unreported exception FileNotFoundException" error. Is there a new place you're supposed to put the txt file with 12.6? I am using a mac so I don't know if that is adding to the problem. It used to work fine when I was using NetBeans 8. Here is my code:

File bookList = new File("BookPrices.txt");//File to be opened
Scanner inputFile = new Scanner(bookList);//Scanner for the file of books
TBA
  • 1,921
  • 4
  • 13
  • 26
K_Cole369
  • 1
  • 1
  • I also tried FileReader with the direct path and that produced the same error – K_Cole369 Apr 10 '22 at 04:14
  • 1. Determine the current "working location" from which the program is been execute, something like `System.getProperty("user.dir")` will work; 2. Move your file to that location. – MadProgrammer Apr 10 '22 at 04:24
  • Netbeans will allow you to set the "working directory", right click the project's node, select "Properties" and look under the "Run" properties. Alternatively you could explore using an embedded resource, but this assumes that the `BookPrices.txt` file won't need to be written to – MadProgrammer Apr 10 '22 at 04:25
  • Your question makes no sense. Your title states _"NetBeans 12.6 not opening txt files"_ (meaning NetBeans is the problem), your question states _"When I try to open the file using the File and Scanner method I keep getting the Unreported exception FileNotFoundException error"_ (meaning you have a runtime exception), and yet you have posted a solution stating _"I needed to put it in a Try/Catch"_, meaning you had a compilation error! Please be much more precise in describing your problem, and follow the advice given in [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – skomisa Apr 10 '22 at 18:06

1 Answers1

0

I figured it out! I needed to put it in a Try/Catch

K_Cole369
  • 1
  • 1