Solved

PrintWriter out - Changing location of output files

Posted on 2004-10-07
15
235 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 43
how do i compare an object based on two fields 6 53
Java Loop 6 59
varialbe initialization 11 33
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

776 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