java.io.FileNotFoundException: (No such file or directory) 从 eclipse 运行时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18709334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.FileNotFoundException: (No such file or directory) when running from eclipse
提问by user2644085
I am writing to a file and want console output,
我正在写入文件并想要控制台输出,
// 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);
}
This gives me console output, but it throws the following exception:
这给了我控制台输出,但它抛出以下异常:
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)
The file does exist.
该文件确实存在。
回答by Menios
The file should be in contained within the root of your project.
该文件应包含在项目的根目录中。
When you execute a project in eclipse, the working directory is the most top level of your project.
当你在 eclipse 中执行一个项目时,工作目录是你项目的最顶层。
Right click your project, click New>File, and make a txt file called "TheGame.txt".
右键单击您的项目,单击“新建”>“文件”,然后创建一个名为“TheGame.txt”的 txt 文件。
回答by dhara
// 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;
}