Why can't I read from / proc in Java using Commons IO FileUtils, but can I do it using a simple FileInputStream?

I am having trouble reading files /proc/%d/statusing the Java copyFiles () method (source code below). I found a workaround using a similar readProc () method.

Now I wonder what the problem was. The output files were created, but each file had 0 bytes (in / proc / all files are 0 bytes, since this is not a standard file system). FileUtils from the Apache Commons I / O library.

I tried to do the same with java.nio - again, an IOException throws that the attributes are incorrect for each file.

I removed the part of the code related to parsing exceptions, etc.

Why does this work with FileInputStream but not with FileUtils.copyFile()?

public void copyFiles() {
    final File dir = new File("/proc");
    final String[] filedirArray = dir.list();
    long counter = 0;
    for(String filedir : filedirArray) {
            final File checkFile = new File(dir, filedir);
            if (checkFile.isDirectory()) {
                    try {
                            Integer.parseInt(filedir);
                            File srcFile = new File(checkFile, "stat");
                            File dstFile = new File("/home/waldekm/files/stat" + "." + Long.toString(counter++));
                            try {                                    
                                FileUtils.copyFile(srcFile, dstFile);
                            } catch (IOException e1) {}
                    } catch (NumberFormatException e) {
                            // not a number, do nothing
                    }                        
            }
    }
}

public static void readProc(final String src, final String dst) {
    FileInputStream in = null;
    FileOutputStream out = null;

    File srcFile = new File(src);
    File dstFile = new File(dst);

    try {
        in = new FileInputStream(srcFile);
        out = new FileOutputStream(dstFile);
        int c;
        while((c = in.read()) != -1) {
            out.write(c);
        }
    } catch (IOException e1) {
 }  finally {

        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e1) {}
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e1) {}
    }
+1
source share
3 answers

The reason is because the operating system reports the file size as zero.

On my machine it man 2 statsays the following:

"For most files in a directory, it /proc stat()does not return the file size in the field st_size; instead, the field returns with a value of 0."

(The system call statwill be what the JVM uses to find out what file size is.)

+2
source

, proc, ( ) Process Android. FORMAT , / proc,

int PROC_SPACE_TERM = (int)' ';
int PROC_OUT_LONG = 0x2000

public static final int[] PROCESS_STATS_FORMAT = new int[] {
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM|PROC_OUT_LONG,                  // 13: utime
    PROC_SPACE_TERM|PROC_OUT_LONG                   // 14: stime
};

long buf[] = new long[2];

try {
    int pid = 1000; // Assume 1000 is a valid pid for a process. 
    Method mReadProcFile = 
              Process.class.getMethod("readProcFile", String.class,
                                      int[].class, String[].class,   
                                      long[].class, float[].class);
    mReadProcFile.invoke(null, "/proc/" + pid + "/stat", 
                         PROCESS_STATS_FORMAT, null, buf, null);

    return buf;

} catch(NoSuchMethodException e) {
    Log.e(TAG, "Error! Could not get access to JNI method - readProcFile");
} catch (InvocationTargetException e) {
Log.e(TAG, "Error! Could not invoke JNI method - readProcFile");
} catch (IllegalAccessException e) {
    Log.e(TAG, "Error! Illegal access while invoking JNI method - readProcFile");
}

return null;
+1

I see that you are creating a FileInputStream to read the / proc file. Instead, I suggest creating a FileReader object. FileInputStream is disabled due to the lack of file length for / proc files, but FileReader does not.

-1
source

All Articles