Writing inputstream to file - no file found - java

I'm trying to write inputstream to a file, but it never gets written on disk, I just get the error file doesnt exist. The file I open is a drawable icluded in the project, I would like to save it to sd card. This is what I have so far:
File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/tester");
InputStream inputStream = getResources().openRawResource(R.drawable.test);
OutputStream out = new FileOutputStream(new File(storagePath, "test.png"));
byte buffer[] = new byte[900];
int len;
while ((len = inputStream.read(buf)) > 0)
out.write(buffer, 0, len);
out.close();
inputStream.close();

Your tester directory doesn't exist. Check for it and create it if necessary before opening your FileOutputStream.

Related

How to compress and download a xlsx file using zip in java

I'm creating a xlsx using poi and saving it on fileSystem. I need to download the file on a servlet call and due to memory constraints I did not create a xssf workbook object and used the following code instead :
byte[] buf = new byte[1024];
ServletOutputStream sOut = response.getOutputStream();
FileInputStream input = null;
try {
long length = fileToRead.length();
input = new FileInputStream(fileToRead);
while ((input != null) && ((length = input.read(buf)) != -1)) {
sOut.write(buf, 0, (int) length);
}
Where fileToRead is the file present at the file system.
How can I integrate this with How to create a zip file in Java
You could use
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
....
ze = new ZipEntry("xlsData");
zos.putEntry (ze);
// loop
zos.write(buf, 0, (int) length);
// finally
zos.close();

Creating default file for my application with images and text

I have the following problem:
I need to save information that is currently on my application, and this information consists on images (.gif and .jpg) and text. I want to save everything in one single file, because saving only the image path for these images may cause an error if the user deletes any of the images. How can I save image and text into one single file using Java?
You can zip the files under one file
Code Example:
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream("C:\\MyFile.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze= new ZipEntry("spy.log");
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream("C:\\spy.log");
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
//remember close it
zos.close();
System.out.println("Done");
}catch(IOException ex){
ex.printStackTrace();
}

Unable to unzip zip file created with java

I have a list of files from different locations. I create a zip file using the following the code which works without error. But when I try to unzip the file in Windows using Extract All it fails seeing unable to find any bytes, yet if I double click into the zip file itself with Windows Explorer I can see the files and individual ones can be opened and contains the correct data
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (File next : files)
{
ZipEntry zipEntry = new ZipEntry(next.getName());
zos.putNextEntry(zipEntry);
FileInputStream in = new FileInputStream(next);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
zos.close();
This may or may not be related but I've found using fixed byte length can lead to a loss of new line characters.
This may help:
final byte[] newLine = System.getProperty(
"line.separator").getBytes("UTF-8");
while ((line = in.readLine()) != null)
final byte[] buffer = line.getBytes("UTF-8");
out.write(buffer, 0, buffer.length);
out.write(newLine, 0, newLine.length);
}

Saving BufferedImage using zip compression

How would I go about saving a BufferedImage straight to a zip file.
Here is my current code for saving my BufferedImage to a zip file but I do not know how to convert the BufferedImage to a InputStream so that it can be saved to the zip file.
If possible I need to save the BufferedImage straight from RAM without saving it to HDD first
byte[] buffer = new byte[1024];
try
{
FileOutputStream outputStream = new FileOutputStream(PathName + imageData.getFileNumber() + ".zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
ZipEntry imageZipOutput = new ZipEntry(imageData.getFileNumber() + ".png");
zipOutputStream.putNextEntry(imageZipOutput);
//the BufferedImage is stored in imageData.getImage();
//how would I parse the BufferedImage to the InputStream below without saving the png first but straight from RAM
InputStream in = new InputStream();
int len;
while ((len = in.read(buffer)) > 0)
{
zipOutputStream.write(buffer, 0, len);
}
in.close();
zipOutputStream.closeEntry();
zipOutputStream.close();
}
Use ImageIO.write(RenderedImage im,String formatName, OutputStream output) to write the output. Pass a ZipOutputStream for the OutputStream parameter.
Check this page for more info.

I have one java problem for moving files one by one by name

I would like to copy my image file by name From E:\Tejas\FM_Operations\source to E:\Tejas\FM_Operations\destination in a such way that if I call the MovePhoto(source,destination,filename) method, then my image will copied to the destination folder in Java.
You might want to consider using FileUtils.copyFile() from Apache Commons IO. Otherwise you'll have to copy the bytes manually, like this:
InputStream in = new FileInputStream(new File("/path/to/src"));
OutputStream out = new FileOutputStream(new File("/path/to/dest"));
byte[] buffer = new byte[8192];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
out.close();

Categories

Resources