Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Creating directories with File

Thread view: 
Hendrik Maryns - 28 Nov 2007 17:24 GMT
Hi group,

I am playing around a bit making a program of a colleague a bit more
robust, and have a question about input/output files.

I managed to make sure the input file can be given as a relative or
absolute path now, and even without the required extension with the
following snippet:

String inFileName = args[0];
if (!inFileName.endsWith(".export")) {
   inFileName += ".export";
}
File inFile = null;
try {
   inFile = new File(inFileName);
   inFile = inFile.getCanonicalFile();
} catch (IOException e) {
   System.out.format("Error getting canonical file name:");
   e.printStackTrace();
   System.exit(1);
}

try {
   BufferedReader corpus = new BufferedReader(new FileReader(inFile));

Then, later on, I can make similarly named files as follows:

String fileDir = inFile.getParent();
String fileName = inFile.getName();
fileName = fileName.replace(".export", "");
String sigFileName = fileDir + File.separator + "td"
 + File.separator + fileName + ".sig";
final ObjectOutputStream sigfile = new ObjectOutputStream(
new FileOutputStream(sigFileName));

although I get a warning in the first line that inFile might be null.

My question is about the creation of the directory: initially, the td/
directory might not be there.  It is created automatically, and used if
it is already there.

Is this a good way to do this, or should I use File.mkdir()?

Any other remarks on how to do this more nicely?  For example, a way to
recover instead of doing System.exit.

Cheers, H.
Signature

Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Ravi - 29 Nov 2007 10:18 GMT
Do you mean this?
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#mkdir()

Regards,
Ravi.

> Hi group,
>
[quoted text clipped - 51 lines]
> Ask smart questions, get good answers:
> http://www.catb.org/~esr/faqs/smart-questions.html
Hendrik Maryns - 29 Nov 2007 11:20 GMT
Ravi schreef:

(fixed top-posting)

>> Hi group,
>>
[quoted text clipped - 42 lines]
>> Any other remarks on how to do this more nicely?  For example, a way to
>> recover instead of doing System.exit.

> Do you mean this?
> http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#mkdir()

Yes.  Although I use Java 6, so it’d rather be

http://java.sun.com/javase/6/docs/api/java/io/File.html#mkdir()

My question is, though, whether my idiom is good, or whether better ways
exist to do this.

H.
Signature

Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Lew - 29 Nov 2007 14:46 GMT
Ravi schreef:
>> Do you mean this?
>> http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#mkdir()

> Yes.  Although I use Java 6, so it’d rather be
>
> http://java.sun.com/javase/6/docs/api/java/io/File.html#mkdir()

And you're hardly an "early adopter" to do so - Java 6 is coming up on its
first anniversary, while Java 1.4 is less than a year from retirement.

> My question is, though, whether my idiom is good, or whether better ways
> exist to do this.

What would "better" mean for you in this context?

Signature

Lew

Esmond Pitt - 29 Nov 2007 23:46 GMT
> My question is about the creation of the directory: initially, the td/
> directory might not be there.  It is created automatically, and used if
> it is already there.

Created automatically by who? Not by Java, and I can't see it in your code.

> Is this a good way to do this, or should I use File.mkdir()?

You must.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.