java.io.FileNotFoundException:(从eclipse运行时没有这样的文件或目录)
我正在写入一个文件,需要控制台输出java.io.FileNotFoundException:(从eclipse运行时没有这样的文件或目录),java,eclipse,filenotfoundexception,Java,Eclipse,Filenotfoundexception,我正在写入一个文件,需要控制台输出 // TODO Create a game engine and call the runGame() method public static void main(String[] args) throws Exception { NewGame myGame = new TheGame().new NewGame(); myGame.runGame(); PrintStream out = new PrintStream(new
// TODO Create a game engine and call the runGame() method
public static void main(String[] args) throws Exception {
NewGame myGame = new TheGame().new NewGame();
myGame.runGame();
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
}
这给了我控制台输出,但它引发了以下异常:
java.io.FileNotFoundException: TheGame.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at game.main(TheGame.java:512)
java.io.FileNotFoundException:TheGame.txt(没有这样的文件或目录)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(FileInputStream.java:138)
在game.main(TheGame.java:512)
该文件确实存在。该文件应包含在项目的根目录中 在eclipse中执行项目时,工作目录是项目的最顶层
右键单击您的项目,单击新建>文件,并创建一个名为“TheGame.txt”的txt文件。@meewoK:谢谢。我仍然觉得奇怪,原来的海报从来没有读过他自己的问题,看到它没有意义,就纠正了它。无意冒犯,但我们都从某个地方开始。。。这是我的第一堂课。无意冒犯,但请记住,编译器是严格且不可原谅的。你的问题有点草率,你会想训练自己对这些问题的容忍度。此外,还发布了一个像样的问题,一个易于阅读和理解的问题,表明您正在认真对待您的问题、本网站和我们的帮助。我期待着看到您的进步。请扩展您的答案,并描述您的代码正在做什么以及为什么,以帮助阅读此问题/答案的其他人。
// Save Image Code
btnsave = (ImageButton) findViewById(R.id.imageButton1);
btnsave.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//String state = Environment.getExternalStorageState();
URL url=null;
try {
url = new URL (testimage);
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
input = url.openStream();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/KalyanPusti_Images");
newDir.mkdirs();
int n = 10000;
Random gen = new Random();
n = gen.nextInt(n);
String fotoname = tittle+".jpg";
File file = new File (newDir, fotoname);
try {
File storagePath = Environment.getExternalStorageDirectory();
FileOutputStream output = new FileOutputStream (file);
try {
byte[] buffer = new byte[15000];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0){
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
public File getTempFile(Context context, String url) {
File file =null;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
return file;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fullimage, menu);
return true;
}