Peter Penzov
Peter Penzov

Reputation: 1648

Read values from files in different folders

I would like to read the values into these folders:

/sys/devices/virtual/thermal/thermal_zone0/temp:68000
/sys/devices/virtual/thermal/thermal_zone3/temp:50000
/sys/devices/virtual/thermal/thermal_zone5/temp:24900
/sys/devices/virtual/thermal/thermal_zone7/temp:62000
/sys/devices/virtual/thermal/thermal_zone8/temp:65000
/sys/devices/virtual/thermal/thermal_zone9/temp:78000

I tested this code:

public void listFolders() throws IOException
    {
        File directory = new File("/sys/devices/virtual/thermal");

        File[] fList = directory.listFiles();

        for (File file : fList)
        {
            if (file.isDirectory() && file.getName().startsWith("thermal_zone"))
            {
                File[] listFiles = file.listFiles();
                for (File file1 : listFiles)
                {
                        byte[] fileBytes = null;
                        if (file1.exists())
                        {
                            try
                            {
                                fileBytes = Files.readAllBytes(file1.toPath());
                            }
                            catch (AccessDeniedException e)
                            {
                            }

                            if (fileBytes.length > 0)
                            {
                                System.out.println(">>>>> " + fileBytes);
                            }                            
                    }
                }
            }
        }
    }

But I get null result when I test the code.

Can you help me to fix the code?

Also can you help me to optimize the code for performance?

Upvotes: 1

Views: 90

Answers (2)

codingenious
codingenious

Reputation: 8653

In this statement,

if (file1.isFile() && file.getName().startsWith("temp"))

after && use file1.getName()

Should work!!!

Also, it will print byte object's hashcode, not bytes, for that you need to iterate through array and print. Look here for refernce

Eg : System.out.println(Arrays.toString(fileBytes))

Upvotes: 2

aberry
aberry

Reputation: 447

Instead of

if (file1.isFile() && file.getName().startsWith("temp"))

Correct it to

if (file1.isFile() && file1.getName().startsWith("temp"))

Now you will see bytes printed.

Upvotes: 2

Related Questions