Creating directories with File

H

Hendrik Maryns

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.
--
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


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFHTaRte+7xMGD3itQRAuzpAJ4qjkpQisaYGpNSj2G4b3YVX6cpWACfeF9F
P9D9ECxL1u2pcOL7kgQPPYY=
=eTzh
-----END PGP SIGNATURE-----
 
H

Hendrik Maryns

Ravi schreef:

(fixed top-posting)
Hendrik said:
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.

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.
--
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


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFHTqCee+7xMGD3itQRArEwAJ4lULfIzb5XBu0pslu8FFBD8fNm7QCeIFhC
BhN8mtvD+KSK8/hBdh4mfW0=
=y46Y
-----END PGP SIGNATURE-----
 
L

Lew

E

Esmond Pitt

Hendrik said:
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.
 

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

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top