Solved

PrintWriter out - Changing location of output files

Posted on 2004-10-07
15
238 Views
Last Modified: 2010-03-31
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) {}

      }
0
Comment
Question by:dkim18
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 3

Expert Comment

by:nesnemis
ID: 12253990
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


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12254205
Change

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

to

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

Author Comment

by:dkim18
ID: 12254371
"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?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12254486
Try

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

Author Comment

by:dkim18
ID: 12254605
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...
0
 
LVL 92

Expert Comment

by:objects
ID: 12254730
Sound like you be better off changing the path return by getOutputPath().
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12254827
Please post the output of

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

using the code i posted
0
 

Author Comment

by:dkim18
ID: 12255300
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";
      }
0
 
LVL 92

Expert Comment

by:objects
ID: 12255311
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?
0
 

Author Comment

by:dkim18
ID: 12255609
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

0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 12255734
try:

private static String getOutputPath(File file) throws Exception {
          String fileName = file.getParentFile().getAbsolutePath();
          return fileName + "_output/" + file.getName() + ".hdr";
}      
0
 

Author Comment

by:dkim18
ID: 12255798
it didn't even create output files nor dir.
0
 
LVL 92

Expert Comment

by:objects
ID: 12255811
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();
 }
0
 

Author Comment

by:dkim18
ID: 12255855
superb!
thanks!!
0
 
LVL 92

Expert Comment

by:objects
ID: 12255859
no worries :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
jboss wildfly 10.1 10 241
Why is enum singleton a better approach than static factory 3 24
Crystal Reports Licensing Questions 4 37
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question