27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

String, text, numbers, I/O II part<br />

inherit from Object (or anything else, for that matter). To put primitives (byte, short,<br />

int, long, float, double, boolean, char) into something that requires an Object, use<br />

<strong>Java</strong>'s wrapper classes.<br />

The wrapper classes are Double, Integer, Long, Boolean, etc., and are basically an<br />

object "wrapped" around a primitive type. You make a Double object by:<br />

Double d = new Double (myDouble);<br />

and to get the actual value back,<br />

double z = d.doubleValue();<br />

It works the same way for all the rest of the primitive/wrapper pairs.<br />

by Trevor Hill<br />

Q: is there a mod (x, y) function that returns the remainder when x is divided by<br />

y? Something equivalent to fmod(x,y) in C?<br />

Answer: a = x%y;<br />

I'm having trouble figuring out how to convert characters to their ASCII value in<br />

java<br />

Q: I'm having trouble figuring out how to convert characters to their ASCII<br />

value in java. Is there a class like NumberFormat that will do it?<br />

Answer: I can't see any problem here:<br />

char ch = 'A'; // character 'A'<br />

int i = (int)ch; // ASCII value for 'A' (=>65)<br />

Yes. And just be aware that ASCII only runs from 0 through 127. Anything<br />

higher needs to be addressed differently, since <strong>Java</strong> is using Unicode values.<br />

Q: I'm writing a program that makes I/O on a (huge) file.<br />

Due to its size, I have to directly jump to a given line number so that I can make a<br />

readLine(). Is there a fast way to do so without traversing all the previous lines?<br />

Note that I've tried the LineNumberReader class but it only keeps track of line<br />

numbers, and does not allow me to go to a specific position in the stream.<br />

Answer, Part 1:<br />

No, there is no easy way because the exact beginning of a line isn't stored<br />

anywhere. It's determined by the content of the file, and nobody is keeping track of<br />

the line feeds, carriage returns etc.<br />

If the file doesn't change, you could find out the offset of each line relative to the<br />

beginning of the file once in a separate run over the original file and store these<br />

offset values in a binary file that has one long value for each line.<br />

You can then load these long values into an array and access it whenever you need<br />

a line. You then seek to that position and read the line directly. If there are too many<br />

long offsets to be kept in memory, you still have a speed advantage; if you want the<br />

offset of line i (i zero-based, so the first line is 0, the second 1 and so on), you seek<br />

to i * 8 (that's the size of a long) with a RandomAccessFile, load the offset value, go<br />

that position in the data file and load the line.<br />

With intelligent caching, you could avoid quite some disk access.<br />

file:///F|/350_t/350_tips/stings_text__date_numbers_io-II.htm (6 of 7) [2002-02-27 21:19:21]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!