Tuesday, August 23, 2011

Exception handling

- "Effetive Java" item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

- http://marxsoftware.blogspot.com/2010/04/i-recently-wrote-that-chapter-in.html


Thursday, August 11, 2011

BufferredReader


The Reader.ready() and InputStream.available() rarely work as you might like, and I don't suggest you use them. To read a file you should use
String line;
while ((line = reader.readLine()) != null)
    System.out.println("<"+line+">");



http://stackoverflow.com/questions/5244839/dose-bufferedreader-ready-method-ensure-that-readline-method-does-not-return


BufferedReader reader=null;
    try {
        reader = new BufferedReader(new StringReader("ABCD"));

        while (reader.ready()) {
            final String line = reader.readLine();
            System.out.println("<"+line+">");
        } catch (..)
    {
        ...
    }


So a BufferedReader is considered ready simply if the underlying stream is also ready. Since BufferedReader is a wrapper, this underlying stream could be any Reader implementation; hence the semantics of ready() are those declared on the interface:
Returns true if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
So you only really get timing guarantees, i.e. that read() will not block. The result of calling ready() tells you absolutely nothing about the content you'll get back from a read() call, and so cannot be used to elide a null check.

Tuesday, August 2, 2011

file name to java.io.File

String filePath = "com/?/work?/rest/resource/" + fileName; 

java.net.URL fileUrl = getClass().getClassLoader().getResource(filePath);

java.io.File rawFileData = new File (filUrl.toURI());