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("***\nS uccessfull y Wrote:\n" +
getOutputPath(file) + "\nwith " + length + " slices in directory.\n");
} catch (Exception ex) {}
}
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));
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("***\nS
getOutputPath(file) + "\nwith " + length + " slices in directory.\n");
} catch (Exception ex) {}
}
Change
>>File outf = new File(getOutputPath(file)); //with filename.hdr
to
File outf = new File(".." + File.separatorChar + getOutputPath(file));//wit h filename.hdr
>>File outf = new File(getOutputPath(file));
to
File outf = new File(".." + File.separatorChar + getOutputPath(file));//wit
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?
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));//wit h filename.hdr
File outf = new File(".." + File.separatorChar + temp + File.separatorChar + getOutputPath(file));//wit
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...
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.ge tAbsoluteP ath());
using the code i posted
System.out.println(outf.ge
using the code i posted
ASKER
cehj,
i used the following code...
++++++++++
File outf = new File(getOutputPath(file)); //with filename.hdr
System.out.println("outf.g etAbsolute Path() : " + outf.getAbsolutePath());
+++++++++++=
and i got the following result...
outf.getAbsolutePath() : C:\scans_test\Scans_for_He ader_ext\c d_133\barr ett040429\ 2043056021 \005\002.h dr
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(fileN ame.length ()- 1)).equals(System.getPrope rty("file. separator" )))
fileName = fileName.substring(0,fileN ame.length ()-1);
if (debug)
System.out.println("absolu tePath: " + fileName);
//fileName = fileName.substring(0, fileName.lastIndexOf("\\") +1);
//fileName = fileName + file;
return fileName + ".hdr";
}
i used the following code...
++++++++++
File outf = new File(getOutputPath(file));
System.out.println("outf.g
+++++++++++=
and i got the following result...
outf.getAbsolutePath() : C:\scans_test\Scans_for_He
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(fileN
fileName = fileName.substring(0,fileN
if (debug)
System.out.println("absolu
//fileName = fileName.substring(0, fileName.lastIndexOf("\\")
//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?
What exactly is iot you need to achieve? ie. can you show me what a certain input should produce?
ASKER
to make it simple, let say there are two img files under:
C:\scans_test\Scans_for_He ader_ext\c d_133\barr ett040429\ 2043056021 \005\001
C:\scans_test\Scans_for_He ader_ext\c d_133\barr ett040429\ 2043056021 \005\002
and I need to create corresponding output file(.hdr) and put them in new dir.
C:\scans_test\Scans_for_He ader_ext\c d_133\barr ett040429\ 2043056021 \005_outpu t\001.hdr
C:\scans_test\Scans_for_He ader_ext\c d_133\barr ett040429\ 2043056021 \005_outpu t\002.hdr
Now, this program does create 001.hdr and 002.hdr under this dir
C:\scans_test\Scans_for_He ader_ext\c d_133\barr ett040429\ 2043056021 \005
I need to create them under this dir
C:\scans_test\Scans_for_He ader_ext\c d_133\barr ett040429\ 2043056021 \005_outpu t
C:\scans_test\Scans_for_He
C:\scans_test\Scans_for_He
and I need to create corresponding output file(.hdr) and put them in new dir.
C:\scans_test\Scans_for_He
C:\scans_test\Scans_for_He
Now, this program does create 001.hdr and 002.hdr under this dir
C:\scans_test\Scans_for_He
I need to create them under this dir
C:\scans_test\Scans_for_He
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
}
File outf = new File(getOutputPath(file));
File outdir = outf.getParentFile();
if (!outdir.exists())
{
outdir.mkdirs();
}
ASKER
superb!
thanks!!
thanks!!
no worries :)
use File outf = new File("./"+getOutputPath(fi
output to parent directory
use File outf = new File("./"+getOutputPath(fi
output to sub-directory
use File outf = new File("subdir/"+getOutputPa
output to subdir on parent directory
use File outf = new File("./subdir/"+getOutput