IO Section Index | Page 5
How do I read a zip file over the network from a URL through a ZipInputStream?
This piece uncompresses every entry of a ZIP file and puts them into a Vector.
ZipEntry anEntry = null;
Vector entriesList = new Vector();
try
{
URL aURL = new URL ("http://localhost/wwwr...more
How do you filter the result of the list() method of the File object?
Create a new class (there are people that consider this a waste and use anonymous classes) that implements the FilenameFilter.
This is an example for filtering all the XML files in a directory:
im...more
How do I display the Euro symbol?
You can use the Unicode symbol:
u20AC
Keep in mind that when you use System.out, it strips the high order byte from the output. In other words, you can't use System.out.println("u20AC") to get a...more
How do I get a listing of all the files in a directory and its sub-directories?
To recursively get all files under a specific directory, just keep calling listFiles() on the File that is returned, if it is a directory (isDirectory() returns true).
Here is what it looks like.....more
How do I create a zip file and add more than one entry to the zip file?
You need to use the facilities of java.util.zip.ZipOutputStream and java.util.zip.ZipEntry. Here's a simple example that makes a zip file and writes three tiny text files into it.
import java.i...more
Is there an easy way of counting line numbers? Or do you have to go through the entire file?
You have to go through the entire file.
try {
LineNumberReader lnr = new LineNumberReader(new FileReader(filename));
String s;
while ((s = lnr.readLine()) != null);
System.out.println("L...more
What is piped I/O used for?
The piped I/O streams are for inter-thread communication. They manage the synchronization across thread boundaries of the buffer.
See Is there any way to communicate between two classes... for an ...more
Why aren't printing-related topics covered in the I/O FAQ? It seems like an I/O issue.
Printing is done with AWT classes and thus covered in the AWT FAQ. See How do I print a multi-page text document? for one example, though the API has changed with every release of Java.more
What are the practical differences, in terms of performance and functionality, between ZipInputStream/ZipOutputStream and GZIPInputStream/GZIPOutputStream?
The compression algorithm is the same (Lempel-Ziv derivative LZ-77-deflate). The difference is that GZIP format is used to compress a single file, and ZIP is used to compress many files in a singl...more
I'm creating a zip file using the java.util.zip package. The created zip file has to be spilt, if it exceeds a size limit. How can I do this splitting of the file?
Java has no built-in support for disk or file spanning.
However, it should be easy to do this on your own.
I would suggest subclassing FileOutputStream
to perform checks on the length of the data ...more
My C program opens a pipe and gets a file descriptor. Then it start a JVM with JNI and sends the file descriptor to the Java side. Is it now possible for me to write data to this pipe using the file descriptor from the Java code?
Unfortunately, the java.io class that handles file descriptors (java.io.FileDescriptor) doesn't allow you to construct a new one from an integer (which is really what you want).
So, the only thin...more
How do I set a system property when using an executable jar (one with an entry of Main-Class: ClassName)?
Just like with executing a program just use the the -D flag
along with the -jar flag. Here is my test case:
public class property {
public static void main(String[] args) {
System.ou...more
What's the serialver syntax to get the serialVersionUID of an array?
You need to pass in the array "type" after the [ character, and before a ; character. If the type is a class, add the letter L and the fully qualified class name. If the type is a primitive, the t...more
How can I implement the Unix "cksum" command in Java?
How can I implement the Unix "cksum" command in Java?
I'm using a CheckedInputStream and creating a new instance of
CRC32 to pass it, but I don't get the same checksum value as
cksum give me.more
Is there any method in Java that will convert hexadecimal characters to binary characters?
You can use Integer.parseInt() or Long.parseLong()
to convert a hexadecimal representation to a 4-byte int or an 8-byte
long, respectively. For example, the Java class file magic number 0xCAFEBAB...more