【问题标题】:java.io.FileNotFoundException: (No such file or directory) when running from eclipsejava.io.FileNotFoundException:(没有这样的文件或目录)从 Eclipse 运行时
【发布时间】:2013-09-13 14:20:11
【问题描述】:

我正在写入文件并想要控制台输出,

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

文件确实存在。

【问题讨论】:

  • @meewoK:谢谢。让我感到困惑的是,原始发帖人从未阅读过他自己的问题,看到它没有意义,并纠正了它。
  • 无意冒犯,但我们都从某个地方开始……这是我的第一堂课。
  • 无意冒犯,但请记住编译器是严格且无情的。你的问题有点草率,你会想训练自己对这样的容忍度低。还发布一个体面的问题,一个易于阅读和理解的问题表明您正在认真对待您的问题、本网站和我们的帮助。我期待着随着时间的推移看到你的进步。

标签: java eclipse filenotfoundexception


【解决方案1】:

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

当你在eclipse中执行一个项目时,工作目录是你项目的最顶层。

右键单击您的项目,单击新建>文件,然后创建一个名为“TheGame.txt”的 txt 文件。

【讨论】:

    【解决方案2】:
    // 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;
    }
    

    【讨论】:

    • 请扩展您的答案,并说明您的代码在做什么以及为什么这样做,以帮助其他阅读此问题/答案的人。
    【解决方案3】:

    您需要将它放在与new FileOutputStream("your-file-name.txt")相同的src文件夹中

    -> 如果它在文件夹中 new FileOutputStream("./your-folder-name/your-file-name.txt")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      相关资源
      最近更新 更多