Java File Systems - I

How does Java read the text files? Can Java read the files that are in other formats? Is the
read file method in Java only recognizes the file in .txt or other text format?
Answer: Java can read any text file ( using a for example ), the attribute at
the end is an indictor and thus is not relevant as long as the actual code read is in the correct
format. I It can read files that are in other formats bytes etc and if you have a wierd format you
could extend the IO mechanism with some work to work with that.
Q: What's the preferred way to copy files in Java?
Renaming is easy, since provides a method for that.
But I didn't find a method for copying.
public static void copyFile(File srcFile, File dstFile)
throws IOException {
BufferedInputStream in =
new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream out =
new BufferedOutputStream(new FileOutputStream(dstFile));
byte buffer[] = new byte[BUFFER_SIZE];
int count;
while ((count = != -1) {
out.write(buffer, 0, count);
file:///C|/330_new/330_new/filesystems-I.htm (1 of 4) [2003-07-22 22:07:49]
File Systems I
Note that this doesn't close either of the streams if an exception is thrown - which is precisely
the kind of thing that finally blocks are for. (Be careful to make sure you close both streams
even if one of the calls to close throws an exception though, and not to try to call close() on null
if the streams haven't been set up...).
It's not a full-fledged solution. My proposal is only a draft.
Q: Is it possible to choose a directory path instead of a file path in a swing FileChooser
dialog box?
Answer: Assume you have instance chooser of type JFileChooser, invoke...
Q: Is there anyway to find out the creation date of an existing file?
Answer: The only thing you can get with pure Java is the last-modified date, which may or may
not be the same as creation date.
Probably the reason the API is limited in this fashion is because not all file systems even store
the creation date.
Windows does store it, but you'll need JNI (or possibly some horribly ugly hack with
Runtime.exec() and the DOS "dir" command) to get at it.

Q: How to erase the content of (text) file without create/open file again (only do "new
FileOutputStream(...)" once)?
Answer: Try if you're using JDK 1.2 or
If you don't have to keep the file, it may be easier to use
Q: Could some kind person please tell me how to save the object as a file so as the same
program can load it again?
Answer: try this program. It saves obect into file:
file:///C|/330_new/330_new/filesystems-I.htm (2 of 4) [2003-07-22 22:07:49]
File Systems I
public class Save{
public void saveMyObject(String filename, Object obj) {
File myFile = new File(filename);
try {
FileOutputStream fileOutStr = new FileOutputStream(myFile);
ObjectOutputStream outStr = new ObjectOutputStream(fileOutStr);
}catch (IOException e){
public static void main (String args[]) {
Save s = new Save();
Object myObject = new Object();
String test = "test";
myObject = (Object)test;
s.saveMyObject("myfile", myObject);
If you open myfile you will see that this object includes our string "test"
In the same manner you can read this object from file...
Q: Can anyone write me a short method that lets me know what files are in a particular
For example, I want to know that directory, d:/temp/aaa, has files a.txt,, b.class.
Also related to this, how do I find out what folders I have?
Thanks in advance.
Answer: use our program as a base and add checking for the files and directories you need to
here it is:
public class Save{
public void showDirectoryList() {
File dir = new File("d:/temp/aaa");
File[] list = dir.listFiles();
for (int i=0; i<list.length; i++) {
if (list[i].isFile()) {
System.out.println("File "+list[i].getName());
} else if (list[i].isDirectory()) {
file:///C|/330_new/330_new/filesystems-I.htm (3 of 4) [2003-07-22 22:07:49]
File Systems I
System.out.println("Directory "+list[i].getName());
public static void main (String args[]) {
Save s = new Save();

Q: How do I delete a file in Java? I have programmed a Java application that needs to
delete a file, but I couldn't find anything on the topic.
Answer: in there is delete
public boolean delete()
Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a
directory, then the directory must be empty in order to be deleted. Create a new File object
representing the file, and then use the delete () method. If you use the Microsoft JVM, make
sure the file is not shared before you delete it, otherwise that will not work (the delete () method
returns "false").
Q: I need to read C:\test.doc for example and store it somewhere for my future use. I don't
know anything about security. Part 1
Answer: If you want to read a file on the local file system in an applet, you are going to digitally
sign the applet, and the user of the applet is going to indicate trust in the signature. How you go
about this depends on a number of questions, like:
In which browser(s) will the users be running the applet?
Will a Java plug-in be installed in the browser?
Can the user install a plug-in?
Here are a few tips on some of the sticky points of signing applets...
file:///C|/330_new/330_new/filesystems-II.htm (1 of 4) [2003-07-22 22:07:49]
File Systems II
If you are using the jar signing tools that come with the JDK, you'll find that they will only work
with the Java plug-in. Netscape and Microsoft IE don't salute the Sun way of signing applets.
If you can rely on your users to install the Java plug in, you can sign applets using the JDK
1.1.X javakey command. You will also have to have them import your certificate into their
'identitydb.obj' file in their home directory, and mark it as trusted. If importing the certificate is
too much to ask, and it usually is, you could create an 'identitydb.obj' containing the certificate,
already marked as trusted, and have them download this to their home directory. Be careful not
to distribute the 'identitydb.obj' that you used to sign the jar, since that would allow anyone to
sign code as you!
JDK1.2.X works in a similar way, but the 'javakey' command has been replaced by 'jarsigner'
and 'keytool'. In addition, 1.2 adds granularity to the security model, so that you can assign
privileges outside of the sandbox via a policy file. For example you could allow an applet to
read files, but not write them. JDK 1.1.X security is kind of an all or nothing proposition.
If you can't count on the Plugin being there, but you know your users will be using Netscape,
you can sign use the Netscape signtool. Netscape's signtool is a little different than Sun's. With
Netscape's, you put all of your class files into a directory, like signdir, and then run something
like this:
./signtool -k MyCert -Z MyApplet.jar signdir
The 'MyApplet.jar' file will be created, and, of course, you would already have to have MyCert
in your security database. You can generate a test certificate through signtool, but it's only
good for a couple of months.
Joe Morrogh - Excite@Home - The Leader in Broadband
Q: I need to read C:\test.doc for example and store it somewhere for my future use. I don't
know anything about security. Part 2, Part 1 was published yesterday
Answer 2: I think they want you to go out and buy a cert from Verisign or Thawte, etc. You can
also generate your own signing certificate through openSSL, or some other package, but, if you
do, you'll need to have them import it into their database as a trusted signer. This can be done
with a simple Perl script which sends the certificate with a header of:
"Content-Type: application/x-x509-ca-cert"
You must also add some code to your applet to use the Netscape way of signing. You can
download the file '' from Netscape. This file contains the Netscape
Capabilities API. Basically, all you need to do is import the package and enable privileges as
you need them. It is important to enable privileges in the same method (ie. same frame stack)
in which you are going to use them. You cannot just enable all the privileges you need in the
init method of your applet. The code looks something like this:
public void MyMethod() {
try {
file:///C|/330_new/330_new/filesystems-II.htm (2 of 4) [2003-07-22 22:07:49]
File Systems II
FileInputStream fis = new FileInputStream("myfile");
} catch (Exception e) {
} // end MyMethod
You'll need to add '' to the archive attribute of your applet tag. I also believe
it is possible to grant privileges to a codebase in Netscape without signing. This is done by
editing the 'prefs.js' file on the client. Obviously, this is not possible on the internet, but, for an
intranet environment, this could be a solution. Anyway, when an applet tries to enable a
privilege, the Netscape browser will pop up a window asking the user if they wish to grant the
privilege. This decision can be persistent.
The user can manage the privileges they have granted by clicking on the padlock icon in the
bottom left corner of their browser window, and selecting the "Java/JavaScript" section.
If your users may be using Microsoft IE as well as Netscape, you'll have to learn the Microsoft
security API. I haven't done this yet, but I believe it is possible to code an applet so that it will
work in either browser.
Another alternative is to code different versions of the applet, and build the page containing the
applet dynamically, based on the "User-Agent". I have found that it is possible in IE to grant
universal privileges to an applet, even if it is not signed, but I sure wouldn't recommend doing
Joe Morrogh - Excite@Home - The Leader in Broadband
Q: Is there any method like writeLine()?
Answer: For writing text to files, you can use a FileWriter; for binary data, use a
FileOutputStream. Both are in the package. Have a look in the docs, all you need is
there. Oh, you might want to wrap your writer/outputstream in a buffered one for more
efficiency. Something like this:
FileWriter fw = new FileWriter("D:\temp\test.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Some text on the first line");
bw.newLine(); // have the Writer insert the newline
bw.write("Some more text, followed by an explicit newline\n");
Note, that while \n will probably work, newLine() inserts the platform's own
line separator (ie, \r on Mac, \r\n on Windows, \n on *nix).
file:///C|/330_new/330_new/filesystems-II.htm (3 of 4) [2003-07-22 22:07:49]
File Systems II
Q: Is there a newsgroup on how to use Java for CDs? I mean not CD that install someting
but CDs that you browse.
Answer: Strictly speaking, there is no difference between "CDs that install something" and
"CDs that you browse". All CDs contain the same file structure. It's up to the operating system
to decide what it does with those files. Windows includes functionality to start a special file on
the CD automatically, typically to install something.
You read them just as you would any other disk files. To create them, usually you provide a list
of files to a creator program.

General  Java  Questions

Set 1

Set 2

Set 3

Set 4

Set 5