create files and a drive for them

O

oqestra

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
 
M

Michael Rauscher

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 ?

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
 
M

Mark Space

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 ?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top