Solved

PrintWriter out - Changing location of output files

Posted on 2004-10-07
15
231 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
Comment Utility
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
Comment Utility
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
Comment Utility
"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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Try

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

Author Comment

by:dkim18
Comment Utility
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
Comment Utility
Sound like you be better off changing the path return by getOutputPath().
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Please post the output of

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

using the code i posted
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:dkim18
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
it didn't even create output files nor dir.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
superb!
thanks!!
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
no worries :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 51
JDeveloper 12c for 32 bit 4 34
unix in java example 9 37
bitbucket vs gitbucket 3 29
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now