0

I'm trying to cat files in Android sysfs, and first I'd like to check if the files exist, and if they are accessible. I'm trying the first step with the following Java code:

String filePath = "/sys/some/path/to/some/file";
try {
  File file = getApplicationContext().getFileStreamPath(filePath);
  if (file.exists()) {
    // do something
  } else {
    // do something else
  }
} catch (IOException e) {
        e.printStackTrace();
}

At run time, I get a java.lang.IllegalArgumentException: File /sys/some/path/to/some/file contains a path separator

What am I doing wrong, and how can I check if a file in sysfs is readable?

user1118764
  • 9,255
  • 18
  • 61
  • 113
  • Follow this thread [java.lang.IllegalArgumentException: contains a path separator](http://stackoverflow.com/questions/5963535/java-lang-illegalargumentexception-contains-a-path-separator) – vivek jha Feb 17 '17 at 07:53
  • OK, that thread suggests using a FileInputStream instead of File object. How then can I check for read access to a FileInputStream object? – user1118764 Feb 17 '17 at 08:12

2 Answers2

0

Try this code -

  String fileDirectory = "/sys/some/path/to/some/";
  String fileName = "file";
        try
        {
            File file = new File(fileDirectory, fileName );

            if (file.Exists()) {                    
                // do something
            }
            else
            {
                // do something else
            }
        }
        catch (IOException e)
        {

        }
Vinutha YS
  • 133
  • 1
  • 7
0

I ended up using vivek's suggestion to use FileInputStream, and then checking for exceptions

user1118764
  • 9,255
  • 18
  • 61
  • 113