首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从eclipse运行时java.io.FileNotFoundException:(没有此类文件或目录)?

如何从eclipse运行时java.io.FileNotFoundException:(没有此类文件或目录)?
EN

Stack Overflow用户
提问于 2018-09-25 00:34:26
回答 2查看 0关注 0票数 0

我正在写一个文件,想要控制台输出,

// 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)
EN

回答 2

Stack Overflow用户

发布于 2018-09-25 09:21:27

该文件应该包含在项目根目录中

在Eclipse中执行项目时,工作目录是项目的最顶层

右键单击项目,单击New>File,并创建一个名为“TheGame.txt”的txt文件。

票数 0
EN

Stack Overflow用户

发布于 2018-09-25 10:01:52

// 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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档