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

前端 未结 2 992
盖世英雄少女心
盖世英雄少女心 2021-02-20 13:55

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 Exceptio         


        
2条回答
  •  孤独总比滥情好
    2021-02-20 14:06

    // 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;
    }
    

提交回复
热议问题