IO Section Index | Page 11
Use the delete() method of the File class.
The PropertyResourceBundle class constructor takes an InputStream (not a Reader). Does this mean that the property files that back the PropertyResourceBundle always have to be encoded in ASCII (Cp1252 really on windows)? How are people (localizers) dealing with that in Japan/Korea/China etc.?
According to the book Core Java 1.1, Advanced Features, this is the case. The work around suggested in the book is to use ListResourceBundle instead. According to Sun's Bug Parade, there is a work...more
Provided you have write access to the file and are using JDK 1.2 or later, the public boolean setLastModified(long time) method of File allows you to modify the timestamp associated with a file.more
The java.io.tmpdir system property defines an appropriate area: String tempPath = System.getProperty("java.io.tmpdir"); File f = new File(tempPath, "test.out"); From Davanum S...more
Create a java.io.File for the directory, then ask for the list of files with one of the following: public java.lang.String list(); public java.lang.String list(java.io.FilenameFilter); public ...more
When reading with a Reader from an ASCII source, how does the Reader know it is ASCII/8-bit instead of Unicode/16-bit data?
Well the Reader really wraps an InputStream which gives byte-level access to the data source. What the Reader (e.g. InputStreamReader) does is to load a correct implementation of sun.io.ByteToCha...more
Check out the java.util.zip.GZIPInputStream and java.util.zip.GZIPOutputStream classes. The following demonstrates compressing a file: import java.io.*; import java.net.*; import java.util.zip.*;...more
The default encoding used by locale/encoding sensitive API in the Java libraries is determined by the System property "file.encoding". This system property is initialized by the JVM star...more
Assuming you have access to the file in question through a web server, you can use the URLConnection methods getContentLength() and getLastModified(). URL fileOfInterest = new URL(...); URLConnec...more
Regexp is a 100% Pure Java Regular Expression package. It can be downloaded from http://jakarta.apache.org/regexp/ For a GPL'd solution, check out the gnu.regexp package. Also note that as of ve...more
How do I detect end of stream in a non-blocking manner when reading a stream through URL/URLConnection, if available() reports nothing to read on end of stream?
In the fully general case, you can't (since the client can just keep sending you data). Given that Java doesn't have asynchronous I/O (yet), you need to design your system to take i...more
Java supports a number of encodings for use with InputStreamReaders and OutputStreamWriters. By using these classes, it's easy to read and write Unicode or any other supported encodings. Here's ...more
Whether you use random access I/O or sequential access really depends upon what you are trying to do. Random access I/O is usually used for fixed-size data records, where you want to overwrite the...more
You can't update the existing JAR file. You have to create a copy with the changes in it, remove the original, and then rename the updated JAR file. The following demonstrates this by adding a fil...more
An option which I have used, and found to be reasonably performant, is to read the files in blocks into byte arrays and use equals() to compare the blocks. Something like this: private final sta...more