Bhargav Panchal
Bhargav Panchal

Reputation: 1189

How can I check isFile() or isDirectory() Function in Java in Ubuntu?

In ubuntu, isFile() and isDirectory() don't work perfectly. I used this code to find out if something is a file or directory:

boolean fileName= file.getName().lastIndexOf('.') == -1;

But the problem is that I made a folder named bhargav.panchal. With the above function, this folder is considered as a file, not a folder.

File file=new File("/home/asd/My_Shared_File/bhargav.panchal");

if(file.exists()){
    if(!file.isDirectory()) {
        Toast.makeText(activity, "This is File", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(activity, "This is Directory", Toast.LENGTH_SHORT).show();
    }
} else {
    Toast.makeText(activity, "File or Directory doesn't exist.", Toast.LENGTH_SHORT).show();
}

In this condition, the isDirectory() and isFile() methods always return false.

Upvotes: 0

Views: 2227

Answers (2)

No, you are wrong somewhere. On the contrary you cant access external environment with DVM.

reason : it is not possible for applications to interfere with each other based on the OS level security and Dalvik VMs are confined to a single OS process, Dalvik itself is not concerned with runtime security. Although Dalvik is not relied upon for security, it is interesting to note that most of the standard Java security classes remain in the Android distribution. These include the java.lang.SecurityManager and some of the classes in the java.security package. In standard Java environments, the SecurityManger plays the role analogous to the OS process-level security in Android. The SecurityManager typically controls access to resources external to the JVM such as files, processes, and the network. In the Android distribution, the standard security framework is apparently present for applications to use within their own application space but is neither fully implemented nor configured (no java.policy files are present) for interprocess security.

Upvotes: 3

Vyacheslav Shylkin
Vyacheslav Shylkin

Reputation: 9801

Replace

       if(file.isDirectory()){
           Toast.makeText(activity, "This is File", Toast.LENGTH_SHORT).show();                    
       } else {
           Toast.makeText(activity, "This is Directory", Toast.LENGTH_SHORT).show();
       }

on this

        if(!file.isDirectory()){
            Toast.makeText(activity, "This is File", Toast.LENGTH_SHORT).show();                    
        } else {
            Toast.makeText(activity, "This is Directory", Toast.LENGTH_SHORT).show();
        }

Upvotes: 1

Related Questions