0

Right now, I'm simply attempting to read the contents of a file stored in the same directory as my Java classes, and access the length. However, whenever the correct file name is passed to create a new File object, its length comes back as being zero. I assume that this is because the file cannot be found for some reason.

My file structure is as follows:

-src
--hw1 (package)
---TextSwap.java
---letters.txt

I try to create a file object with File file = new File(filename);, where filename is equal to letters.txt.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190

1 Answers1

0
FileWriter fw=new FileWriter("letters.txt");

Or try

FileReader fr=null;
try
{
    fr = new FileReader("letters.txt");
}
catch (FileNotFoundException fe)
{
    System.out.println("File not found");
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 18 '21 at 17:06