正在引发Java Filenotfound异常
尽管我的文件位于我所述的确切目录中,但仍会为我的代码引发FileNotFound异常。我还尝试了正在引发Java Filenotfound异常,java,io,filenotfoundexception,Java,Io,Filenotfoundexception,尽管我的文件位于我所述的确切目录中,但仍会为我的代码引发FileNotFound异常。我还尝试了…新文件(“euler8.txt”)没有成功。我的代码如下: private static void euler8() throws IOException { int current; int largest=0; int c =0; ArrayList<Integer> bar = new ArrayList<Integer&
…新文件(“euler8.txt”)代码>没有成功。我的代码如下:
private static void euler8() throws IOException
{
int current;
int largest=0;
int c =0;
ArrayList<Integer> bar = new ArrayList<Integer>(0);
File infile = new File("C:/Users/xxxxxxxx/workspace/Euler1/euler8.txt");
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(infile),
Charset.forName("UTF-8")));
try
{
while((c = reader.read()) != -1)
{
bar.add(c);
}
}
finally{reader.close();}
for(int i=0; i<bar.size(); i++)
{
current = bar.get(i) * bar.get(i+1) * bar.get(i+2) * bar.get(i+3) * bar.get(i+4);
if(largest<current)
largest = current;
}
}
private static void euler8()引发IOException
{
电流;
int最大=0;
int c=0;
ArrayList条=新的ArrayList(0);
File infle=新文件(“C:/Users/xxxxxxxx/workspace/Euler1/euler8.txt”);
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(
新文件输入流(infle),
字符集forName(“UTF-8”);
尝试
{
而((c=reader.read())!=-1)
{
加上(c);
}
}
最后{reader.close();}
对于(int i=0;i正斜杠工作正常,并且是首选斜杠,因为它们在任何平台上工作(相对路径优于绝对路径)。请确保您的路径按指定的方式存在,并验证您对指向该文件的目录具有读取权限。例如,如果您以其他用户身份运行java程序,则可能对“myuser”文件夹没有读取权限。如果所有目录都不存在,则此代码将无法工作,因此我假定(希望我是对的)你有一个打字错误,或者缺少一个文件夹
我通常更喜欢对父目录进行java.io.File
引用,然后在后续的文件引用中将其用作父目录,即:
File dir = new File("parentDir");
File inFile = new File(dir, "fileName");
另外,java.io.File
有一个返回true或false的exists()
方法,其后续的mkdir()
,mkdirs()
,以及createNewFile()
如果实际创建了请求的文件,则返回true或false
这就是说,我将您的代码修改为以下内容,并在我的机器上执行;但我不知道您试图运行的数据是什么
int current;
int largest = 0;
int c = 0;
ArrayList<Integer> bar = new ArrayList<Integer>(0);
File dir = new File("C:/Users/myuser/workspace/Euler1");
if(!dir.exists()){
dir.mkdirs();
}
File infile = new File(dir, "euler8.txt");
if(!infile.exists()){
infile.createNewFile();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(infile),
Charset.forName("UTF-8")));
try {
while ((c = reader.read()) != -1) {
bar.add(c);
}
} finally {
reader.close();
}
for (int i = 0; i < bar.size(); i++) {
current = bar.get(i) * bar.get(i + 1) * bar.get(i + 2) * bar.get(i + 3) * bar.get(i + 4);
if (largest < current) {
largest = current;
}
}
int电流;
int最大=0;
int c=0;
ArrayList条=新的ArrayList(0);
File dir=新文件(“C:/Users/myuser/workspace/Euler1”);
如果(!dir.exists()){
dir.mkdirs();
}
文件填充=新文件(dir,“euler8.txt”);
如果(!infle.exists()){
createNewFile();
}
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(
新文件输入流(infle),
字符集forName(“UTF-8”);
试一试{
而((c=reader.read())!=-1){
加上(c);
}
}最后{
reader.close();
}
对于(int i=0;i
反斜杠是不必要的。我运行了以下程序:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class Test {
public static void main(String[] args) throws IOException {
File infile = new File("C:/Users/pats/workspace/test/euler8.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(infile), Charset.forName("UTF-8")));
try {
String s;
while ((s = reader.readLine()) != null) {
System.out.println(s);
}
} finally {
reader.close();
}
}
}
非常相似,它打印了我文件的内容
我认为您需要检查文件是否存在,以及您是否有权访问它。除了建议的其他内容外,您可以检查您是否存在此问题(我们在实验室中已经看到):具有两倍扩展名的文件。换句话说,请确保确实调用了euler8.txt
,而不是euler8.txt
,例如,因为对于隐藏的扩展名,文件资源管理器将显示第一个扩展名,但如果您不记得它应该隐藏扩展名,那么一开始可能不会让您感到奇怪。您尝试打印文件的绝对路径,并可能将该输出与新文件的绝对路径(“.”)进行比较,以查看路径中是否有错误?文件名的大小写是否正确?请尝试添加System.out.println(infle.exists())
在声明infle
后立即执行。如果返回为false,则说明文件路径有问题-继续返回目录,直到返回为true。如果返回为true…??尝试使用。/Euler1/euler8.txt
作为文件路径。。/Euler1/euler8.txt
无效。从@Jam给出的图片中可以看出相同的错误es Roberts,我想你是对的。文件名显示为'euler8.txt',但它应该只显示'euler8',因为扩展名在Windows 7中是单独指定的。@JavaNewbie_M107我没有注意到这张图片。看起来是这样。希望OP检查回来。为了帮助…在我的Windows开发机器上,我关闭了该选项,因为在Windows中重命名文件的简单方法经常让我遇到这个问题,但扩展名的可见性不足。@Theodoroschatziganakis WINNER