Solved

PrintWriter out - Changing location of output files

Posted on 2004-10-07
15
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

627 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