Hello, super java programmers!
I've been a fan of java programming for some years but still around
with some very basic input-output stuff.
I am currently having a problem with file creation, that I don't know
how to generate many files at the same time. I add a button on my frame
already, so please tell me how I can create a shortcut of an already
existing drive and then add some files in it when I click that button ?
Thanks a lot
Michael Rauscher - 29 Jul 2006 23:23 GMT
oqestra@yahoo.com schrieb:
> Hello, super java programmers!
>
[quoted text clipped - 4 lines]
> already, so please tell me how I can create a shortcut of an already
> existing drive and then add some files in it when I click that button ?
I don't know exactly if I understand you correctly, but perhaps this one
helps:
public void createSomeFiles( String path ) throws IOException {
File directory = new File(path);
if ( !directory.isDirectory() )
throw new IllegalArgumentException( path +
" denotes no directory." );
for ( int i = 0; i < 20; i++ ) {
StringBuilder fileNameBuilder =
new StringBuilder( directory.getAbsolutePath() );
fileNameBuilder.append( File.separator );
fileNameBuilder.append( "File" );
fileNameBuilder.append( i );
File f = new File(fileNameBuilder.toString());
f.createNewFile();
}
}
Bye
Michael
Mark Space - 30 Jul 2006 03:05 GMT
> Hello, super java programmers!
>
[quoted text clipped - 4 lines]
> already, so please tell me how I can create a shortcut of an already
> existing drive and then add some files in it when I click that button ?
For the existing drive, first you need to know what the existing drives
are. Use the listRoots() method.
http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#listRoots()
You'll have to list all the roots (drive letters), throw away the ones
you don't want, if any (maybe you don't want A: and B:, for example, or
you don't want the CDROM). Then you'll need to make a new button for
each drive left.
Then when the said button is clicked, you have to switch the window,
frame or whatever, and present the user with a new file listing. Any
new files go in the current directory, so you'll have to store that for
use as well.
Disclaimer: I haven't tried this yet, or used listRoots(). Read the
docs first, make sure it does what you want.
Mark Space - 30 Jul 2006 03:10 GMT
Oh yes, one more thing: Why go to all this trouble?
Why not just use the FileChooser? All the hard work is done for you...
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html