Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
现在,我只是试图读取与我的 Java 类存储在同一目录中的文件的内容,并访问长度。但是,每当传递正确的文件名以创建新File对象时,它的长度就会返回为零。我认为这是因为由于某种原因找不到该文件。
File
我的文件结构如下:
-src --hw1 (package) ---TextSwap.java ---letters.txt
我尝试创建一个文件对象 File file = new File(filename);,其中filename等于letters.txt。
File file = new File(filename);
filename
letters.txt
FileWriter fw=new FileWriter("letters.txt");
或者试试
FileReader fr=null; try { fr = new FileReader("letters.txt"); } catch (FileNotFoundException fe) { System.out.println("File not found"); }