Dimitris Vyzovitis wrote:
> 
> Aaron Gaudio wrote:
> 
> > Isn't this a little over-complicated? Couldn't one just open up the file
> > like any other normal file in Java and then read from it? I admit I
> > haven't tried it myself.
> >
> 
> /proc contents are special files. They always appear to have size 0  but you
> can always cat them for the current snapshot.
> So, opening as a normal file will not do (try lessing them, you'll know what I
> mean) ;-}
> 
> Dimitris

 I decided to play with this. Here is a quick class I whipped up to see
what was going on.

import java.io.*;

public class Read {
    public static void main(String args[]) {
        new Read(args[0]);
    }

    public Read(String s) {
        try {
        //First method
            BufferedReader bufferedreader = new BufferedReader(new
FileReader(s));
            for(String s1 = null; (s1 = bufferedreader.readLine()) != null;)
                System.out.println(s1);
            bufferedreader.close();
        //Second method
            System.out.println("Second Try");
            File f = new File(s);
            long llen = f.length();
            int len = (int)(llen & 0x7FFFFFFF);
            if (len != llen)
                throw new IOException("File too big to read into a single object");
            byte[] b = new byte[len];
            DataInputStream ds = new DataInputStream(new FileInputStream(f));
            ds.readFully(b);
            System.out.println (new String(b));  
            
        }
        catch(Throwable e) {
            e.printStackTrace();
        }
    }
}

 Please, no complaints on form, just trying to get a quick n dirty
example to show what's being described. Now, heres a log:

[skellner@asylum] ~/projects> echo "Hello There" > testing
[skellner@asylum] ~/projects> java Read testing
Hello There
Second Try
Hello There

[skellner@asylum] ~/projects> java Read /proc/loadavg
0.01 0.06 0.07 2/79 9467
Second Try

[skellner@asylum]
~/projects>                                                   


 So it looks like while yes, it is a 0 length file if looked at in that
way, it behaves normally to FileReaders. And yes, everything behaves the
same on multi-line files. So if you wanna read /proc files, try the
first method. And I'm sure there are other ways as well.

-- 
Sean Kellner
[EMAIL PROTECTED]

Reply via email to