Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

PrintWriter out - Changing location of output files

Hi!

This func writes output files in the where input files are, but I am trying to create output folder for output files upper dir and place all output files there. Any ideas?
BTW, Does ‘out’ write output files current dir?

Thanks in advance,

+++++++++++++++++
private void saveHeader(File file, String header, int length) {
            if (debug)
                  System.out.println(header);
            PrintWriter out;
            try {
                  File outf = new File(getOutputPath(file));//with filename.hdr
                  System.out.println("outf: " + outf);
                  out = new PrintWriter(new FileWriter(outf));
            } catch (NullPointerException ex) {
                  System.out.println("File Not Found ...");
                  ex.printStackTrace();
                  return;
            } catch (Exception ex) {
                  System.err.println("Error in File Name ...");
                  ex.printStackTrace();
                  return;
            }

            out.print(header);
            out.close();

            
            try {
                  System.out.println("***\nSuccessfully Wrote:\n" +
                  getOutputPath(file) + "\nwith " + length + " slices in directory.\n");
            } catch (Exception ex) {}

      }
Avatar of nesnemis
nesnemis

output to parent directory
    use File outf = new File("./"+getOutputPath(file));//with filename.hdr
output to parent directory
    use File outf = new File("./"+getOutputPath(file));//with filename.hdr
output to sub-directory
    use File outf = new File("subdir/"+getOutputPath(file));//with filename.hdr
output to subdir on parent directory
    use File outf = new File("./subdir/"+getOutputPath(file));//with filename.hdr


Avatar of CEHJ
Change

>>File outf = new File(getOutputPath(file));//with filename.hdr

to

File outf = new File(".." + File.separatorChar + getOutputPath(file));//with filename.hdr
Avatar of dkim18

ASKER

"outf" is hdr file and I cannot change that.
I created temp dir in upper dir. Can I put output file(outf) in that dir if i pass in absolute pass as parameter?
Try

File outf = new File(".." + File.separatorChar + temp + File.separatorChar + getOutputPath(file));//with filename.hdr
Avatar of dkim18

ASKER

hm...it doesn't work. it doesn't even create output files...
I even tried

File outf = new File("C:\\" + getOutputPath(file));

but didn't work...
Sound like you be better off changing the path return by getOutputPath().
Please post the output of

System.out.println(outf.getAbsolutePath());

using the code i posted
Avatar of dkim18

ASKER

cehj,

i used the following code...

++++++++++
File outf = new File(getOutputPath(file));//with filename.hdr
System.out.println("outf.getAbsolutePath() : " + outf.getAbsolutePath());

+++++++++++=
and i got the following result...
outf.getAbsolutePath() : C:\scans_test\Scans_for_Header_ext\cd_133\barrett040429\2043056021\005\002.hdr

btw, this is not my code and getOutputPath(file) is not really get path. it just add .hdr...

+++++++++++
private String getOutputPath(File file) throws Exception {
            String fileName = file.getAbsolutePath();
            //String fileName = outPath.getAbsolutePath();

            if ((fileName.substring(fileName.length()- 1)).equals(System.getProperty("file.separator")))
                  fileName = fileName.substring(0,fileName.length()-1);

            if (debug)
                  System.out.println("absolutePath: " + fileName);
            
            //fileName = fileName.substring(0, fileName.lastIndexOf("\\")+1);

            //fileName = fileName + file;
            return fileName + ".hdr";
      }
looks like you're making thing far more complicated for yourself than they need to be.
What exactly is iot you need to achieve? ie. can you show me what a certain input should produce?
Avatar of dkim18

ASKER

to make it simple, let say there are two img files under:
C:\scans_test\Scans_for_Header_ext\cd_133\barrett040429\2043056021\005\001
C:\scans_test\Scans_for_Header_ext\cd_133\barrett040429\2043056021\005\002

and I need to create corresponding output file(.hdr) and put them in new dir.

C:\scans_test\Scans_for_Header_ext\cd_133\barrett040429\2043056021\005_output\001.hdr
C:\scans_test\Scans_for_Header_ext\cd_133\barrett040429\2043056021\005_output\002.hdr

Now, this program does create 001.hdr and 002.hdr under this dir
C:\scans_test\Scans_for_Header_ext\cd_133\barrett040429\2043056021\005

I need to create them under this dir
C:\scans_test\Scans_for_Header_ext\cd_133\barrett040429\2043056021\005_output

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkim18

ASKER

it didn't even create output files nor dir.
sorry forgout to mention you'll also need to create the directory if it doesn't already exist.

 File outf = new File(getOutputPath(file));//with filename.hdr
 File outdir = outf.getParentFile();
 if (!outdir.exists())
 {
     outdir.mkdirs();
 }
Avatar of dkim18

ASKER

superb!
thanks!!
no worries :)