0

I want to create an ouputStreamWriter object say "out", for a file named say "f". I want this this file f to be created in a directory or folder named "output". If the directory "output" does not exist then it should create it and if that already exists then it should create file "f" in that directory.

In short I want to have directory named "output" and then I want to put different files in it during execution of my program.

Could anyone please tell me how to do this in java ? Right now my following code is creating different files in current directoy. I want to put all files in a folder for my convinience.

public Dump(String outputFile) throws IOException {
        final FileOutputStream fos = new FileOutputStream(outputFile + "gz.xml");
        final GZIPOutputStream gzfos = new GZIPOutputStream(fos);
        out = new OutputStreamWriter(new BufferedOutputStream(gzfos), "UTF-8");

    }
Ragini
  • 1,509
  • 7
  • 28
  • 42

3 Answers3

2

It is supported out-of-the-box:

final File f = new File("dir/file.txt");
f.getParentFile().mkdirs();
f.createNewFile();
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Your best answer is to have a look at the commons IO library, especially the FileUtils. It should cover most things you need:

Commons IO Javadoc

Woody
  • 5,052
  • 2
  • 22
  • 28
0

Hi There you can use the exists method on file to check if your directory/file exists all ready,

File myOutputDir = new File("path");

if(!myOutputDir.exists())
     myOutputDir.mkdir(); //Returns a boolean if you want to check it was successful.
//Continue with your code the directory should now exist if it did not before.

Cheers,

E

Aswan
  • 66
  • 10