Videos
FileOutputStream has a constructor that takes a File object.
The following should do what you need it to do:
File f = new File("path/to/my/file");
if(f.createNewFile()) { // may not be necessary
FileOutputStream fos = new FileOutputStream(f); // create a file output stream around f
ftpClient.retrieveFile("/" + ftpFile.getName(), fos);
otherMethod(f); // pass the file to your other method
}
Note that in addition to the answer of mcfinnigan, you must know that when you use the code:
FileOutputStream fos = new FileOutputStream(f); // create a file output stream around f
ftpClient.retrieveFile("/" + ftpFile.getName(), fos);
Then an empty file will be created on your filesystem on the first line. Then if the 2nd line throws an exception, because no remote file exist for path "/" + ftpFile.getName(), the empty file will still be on your filesystem.
So I've done a little LazyInitOutputStream with Guava to handle that:
public class LazyInitOutputStream extends OutputStream {
private final Supplier<OutputStream> lazyInitOutputStreamSupplier;
public LazyInitOutputStream(Supplier<OutputStream> outputStreamSupplier) {
this.lazyInitOutputStreamSupplier = Suppliers.memoize(outputStreamSupplier);
}
@Override
public void write(int b) throws IOException {
lazyInitOutputStreamSupplier.get().write(b);
}
@Override
public void write(byte b[]) throws IOException {
lazyInitOutputStreamSupplier.get().write(b);
}
@Override
public void write(byte b[], int off, int len) throws IOException {
lazyInitOutputStreamSupplier.get().write(b,off,len);
}
public static LazyInitOutputStream lazyFileOutputStream(final File file) {
return lazyFileOutputStream(file,false);
}
public static LazyInitOutputStream lazyFileOutputStream(final File file,final boolean append) {
return new LazyInitOutputStream(new Supplier<OutputStream>() {
@Override
public OutputStream get() {
try {
return new FileOutputStream(file,append);
} catch (FileNotFoundException e) {
throw Throwables.propagate(e);
}
}
});
}
I've encoutered this problem while using Spring integration remote.file packages, with the FTP/SFTP file download features. I use it like that to solve this empty file problem:
try ( OutputStream downloadedFileStream = LazyInitOutputStream.lazyFileOutputStream(destinationfilePath.toFile()) ) {
remoteFileSession.read(source, downloadedFileStream);
}
From a first glance: Why are you using a DataOutputStream ? Totally inadequate for your goal. Just use your provided outputstream:
writer = connect.getOutputStream();
BTW, it's dubious practice to call "writer" that variable, Java makes a clear conceptual difference between readers/writers (char oriented) and streams (byte oriented).
UPDATE: Another bad practice, which calls for trouble: you are mixing readers/writers (char oriented) and streams (byte oriented) unnecessarily - and without a specifyng a charset encoding.
BufferedReader br = new BufferedReader(new InputStreamReader(input.getInputStream()));
You must use a Reader when you are dealing with text (in a know encoding), use a InputStream if your are just dealing with bytes.
If you want to open reports after exporting/saving it, then this will definately work
JasperReportBuilder report = DynamicReports.report();// a new report
report.setDataSource(dataSource);
String path = "d:\\PriceReport"+Math.random()+".pdf";
File generatedfile= new File(path);
FileOutputStream fileOutStream = new FileOutputStream(generatedfile);
report.toPdf(fileOutStream);
fileOutStream.close();
fileOutStream.flush();
if(generatedfile.exists()){
if(Desktop.isDesktopSupported()){
Desktop.getDesktop().open(generatedfile);
}else{
System.out.println("Not Supported by your desktop");
}
}else{
System.out.println("File does not Exists");
}
Use the constructor that takes a File and a boolean
FileOutputStream(File file, boolean append)
and set the boolean to true. That way, the data you write will be appended to the end of the file, rather than overwriting what was already there.
Use the constructor for appending material to the file:
FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.
So to append to a file say "abc.txt" use
FileOutputStream fos=new FileOutputStream(new File("abc.txt"),true);