Solved

Issue creating a file using Java

Posted on 2012-03-22
12
674 Views
Last Modified: 2012-04-01
File not found exception is obtained with the following code. I get this only once, on first execution. After that, I do not get this exception at all. Even while I get this exception for the first time, I get the file "ico" created with the specified path. Why does I get this during first time and not after that? How can I resolve this issue?

try {
System.out.println("Icon file :" + iconFile);
File ico = new File(iconFile); //creating a file with the path (because only this is working with absolute path) 
ico.setReadable(true, false);
System.out.println(" Icon path : "+ico);
fis = new FileInputStream(ico);
System.out.println("Iconcache fileinputstream" +fis);
d = new BitmapDrawable(mContext.getResources(),fis);
System.out.println("Iconcache d " + d);
System.out.println("Iconcache getFullResIcon after fileinputstream  ");
fis.close();
}
catch (OutOfMemoryError e) 
{
System.out.println(" outmemory");
e.printStackTrace();
} catch (FileNotFoundException e) {
System.out.println(" filenotfound");
// TODO Auto-generated catch block
e.printStackTrace();
} 

Open in new window



Exception:

437: INFO/System.out(572): Icon file :/data/data/com.android.settings/app_themes/browser.png
01-01 00:04:43.812: INFO/System.out(572):  Icon path : /data/data/com.android.settings/app_themes/browser.png
01-01 00:04:43.812: INFO/System.out(572):  filenotfound

Open in new window


Any help in this regard, which would help me resolve this problem, is much appreciated.

Regards,
Sree
0
Comment
Question by:Sreejith22
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37751926
on which line IOException (filenotfound) is coming?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37751934
it looks like iconFile path may not be valid or accessible.

did you checked this?
http://stackoverflow.com/questions/9049601/saving-bitmap-io-exception
0
 

Author Comment

by:Sreejith22
ID: 37751936
fis = new FileInputStream(ico);
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 37752012
then i guess, either your path is not right in the sense that 'file doesn't not exists as of now'

please check here
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FileInputStream.html

it says that your file need to exist for this API to work

use this example, to check if the file exists or not
http://www.roseindia.net/java/example/java/io/CreateFile.shtml
0
 

Author Comment

by:Sreejith22
ID: 37752042
CODE:

File ico = new File(iconFile); 
ico.setReadable(true, false);
System.out.println("FileExecute " + ico.canExecute());
System.out.println("FileRead " + ico.canRead());
System.out.println("FileWrite " + ico.canWrite());
System.out.println("FileExists " + ico.exists());
System.out.println("FileisFile " + ico.isFile());
System.out.println("FileisDirectory " + ico.isDirectory());

Open in new window



First execution:

FileExecute false
FileRead false
FileWrite false
FileExists true
FileisFile true

Open in new window


Second execution:

FileExecute false
FileRead true
FileWrite false
FileExists true
FileisFile true

Open in new window



As you can see,  on first execution, FileRead permission is false. I presume, this might be causing the issue. How can I resolve this, if it is the real cause?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37752221
0
 
LVL 8

Expert Comment

by:Santhana
ID: 37752326
HI Sreejith22,
I too find some issues related to this setReadable() and setWritable()......

In some versions of JDK have issues in changing the permission....
http://stackoverflow.com/questions/5302269/java-file-setwritable-and-stopped-working-correctly-after-jdk-6u18
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6368018

I have tried the same example by accessing a icon file from the same path...
I tried by manually change the permission  of the file and it was working perfectly to me......
But when i programatically change the permission by using setReadable(true)...It is always returning false to me.....
0
 

Author Comment

by:Sreejith22
ID: 37752384
But why this happens only once is what puzzles me. Also, did you have a solution for that santhanasamy?
0
 
LVL 1

Expert Comment

by:haravallabhan
ID: 37752566
May be for the first time its taking time to read the file. The second time its ready to be read. Is it the First execution after compilation. Are you compiling again for the second try.
0
 
LVL 8

Expert Comment

by:Santhana
ID: 37752576
HI Sreejith22,

I found this nice article by Venabili in EE.Here he summarized  java permission related concepts.....
http://www.experts-exchange.com/Programming/Languages/Java/A_1498-File-permissions-with-Java.html


From the android DOcs

public boolean setReadable (boolean readable, boolean ownerOnly) Since: API Level 9 Manipulates the read permissions for the abstract path designated by this file.
Returns
true if and only if the operation succeeded. If the user does not have permission to change the access permissions of this abstract pathname the operation will fail. If the underlying file system does not support read permission and the value of readable is false, this operation will fail.

This is what i am gettiing from these....
So If the user does not have permission to change the access permissions of this abstract pathname ,it will not allow to change the permission  even through programatically......

I am not facing the senario like you are facing....
For me programatically not able to set the permission to "other if i set remove the read permission manually....
0
 

Accepted Solution

by:
Sreejith22 earned 0 total points
ID: 37770763
No exact solution yet for the issue.
0
 

Author Closing Comment

by:Sreejith22
ID: 37792602
NO solution yet
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Understanding mobile browsers 9 84
eclipse buid path vs tomcat lib path 10 34
ejb example issues 3 18
Missing number of chat in Whatapps icon 5 34
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question