Byte and Char differences in Java Posted by Swapnil Pathare on Jul 10

For I/O newbies in Java, the big question comes is which of the methods are to be used for I/O read/write. Anyway, unlike some other languages, Java insists on using the “explicit” way of coding:

Code for Python (Write to file)

f=open('scratch','wb')
for i in xrange(1000000):
f.write(str(i))
f.close()

Code for Java (Write to file)

File f = new File("scratch");
PrintWriter ps = new PrintWriter(new OutputStreamWriter
(new FileOutputStream(f)));
for (int i = 0; i < 1000000; i++) {
ps.print(String.valueOf(i));
}
ps.close();

Discounting the mandatory include statements for java.io, class name, try catch block for any and all I/O operations, we are still left with around 4 classes to be used for 1 write operation to a file.

So the question newbie posts in a forum is: I am writing to a file, but I’m not sure whether I should use BufferedOutputStream or BufferedWriter

The reply generally holds this: If you are inserting chars, use a Writer, else use the Stream

This is fairly accurate, but for a slightly deeper understanding let us break down the modules

When to use Writer object

Streams transfer bytes. Period.

So when you want to transfer any binary data, you use streams. You open the file, wrap it with FileInputStream, take out the data and process it. When you save it back, you make use of the FileOutputStream. Similar classes to be used for transferring though other streams.

In case you would like to use some text-friendly methods for your ascii text, you need chars or a char[] array. To get this, you convert bytes to chars while reading (and similarly chars to bytes while writing), with a simple wrapper, which happens to be InputStreamReader for reading and OutputStreamWriter for writing.

To have data formatting conversion done by Java, wrap the Writer objects in a PrintWriter

To increase performance, you wrap the objects in a buffer. Java uses internally allocated memory space to provide efficient read/write operations. The working of the buffer is transparent to the programmer, except for few methods like flush()

(Any) OutputStream => (wrap in) BufferedOutputStream

(Any) InputStream => (wrap in) BufferedInputStream

(Any) Writer => (wrap in) BufferedWriter

(Any) Reader => (wrap in) BufferedReader

The Buffer classes also provide some friendly methods: readLine() in BufferedReader and write(String, int, int) in BufferedWriter. These are more of a convenience which can be coded anyway in 2-3 lines. In case your app can’t afford to wait for the buffer to get full before transmitting or receiving data, it is always better to avoid using Buffers than to use and flush() them throughout your program.

Good Practices: Install Graphics Drivers Posted by Swapnil Pathare on Jul 8

A while ago I was at a colleague’s desk, coaxing him to read my last post (yes, that’s what this blog has come to) when I saw his screen go through a long session of repaint every time he attempted scrolling the browser window.

Most users don’t recognize this (they generally term it as My Computer is Too Slow), some can’t identify the reason. The basic and extremely easy solution to turn off that irritating screen repaint is to update your graphics drivers.

I wondered why, my colleague, knowing this solution, was too pooped to update his graphics drivers. The answer lay in an obscure (and much confusing) horror story about his driver installations in his Compaq notebook. Make sure you take a complete Windows Driver CD from your vendor, I always say :)

Anyhoo, that’s neither here nor there. Where was I… yeah, driver installation. So if you don’t install your graphics card’s driver, Windows hops on to use a generic pure-software graphic rendering (which works with all cards). So, while your graphics card gulps electricity (even the onboard cards do. They all live. They all have a heart), the Windows machine refuses to use any of its capabilities, instead slowing down your work and providing additional stress to your eyes with the frequent screen repaints.

So just update those drivers for a trouble-free life. Reduce screen repaints. Utilize your card. Rest your eyes. As a bonus, the StandBy option will be available once vga drivers are installed, so you can save precious minutes of battery life on your notebooks. Since the Hibernate option is an (extremely useful) extension of StandBy, there you go, you can Hibernate the machine without any problems (as long as you have a few hundred MBs of free disk space).

What is DRM in one word Posted by Swapnil Pathare on Jul 8

The Related Links section of the topic “Phoning Home” on Wikipedia shows what DRM mostly relates to :)

See also

« Previous Entries