java.io.FileNotFoundException: (No such file or directory) when running from eclipse
Asked Answered
Z

3

11

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.

Zoller answered 10/9, 2013 at 1:12 Comment(3)
@meewoK: thank you. It still boggles my mind that the original poster never read his own question, saw that it didn't make sense, and corrected it.Puffery
no offense, but we all start somewhere... and it's my first class.Zoller
No offense taken, but please remember that compilers are strict and unforgiving. Your question was a bit on the sloppy side, and you will want to train yourself to have low tolerance for such. Also posting a decent question, one that's easy to read and understand shows that you are taking your problem, this site and our help seriously. I look forward to watching your improvement over time.Puffery
D
17

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.

Right click your project, click New>File, and make a txt file called "TheGame.txt".

Danu answered 10/9, 2013 at 1:14 Comment(0)
T
1
// 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;
}
Tension answered 9/3, 2014 at 13:52 Comment(1)
Please expand your answer, with a description of what your code is doing any why, to assist others who read this question/answer.Haematopoiesis
Y
0

You need to place it inside the src folder that's same as new FileOutputStream("your-file-name.txt")

-> and if it is inside a folder new FileOutputStream("./your-folder-name/your-file-name.txt")

Yesman answered 23/2, 2022 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.