Move file without using renameTo() - java

In my Java program, I would like to display the progress of moving a file. I use the following snippet of code to copy files, which allows me to track the bytes copied and shows it in a progress bar. I was wondering if the code code be adapted to move files rather than just copy them?
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
int theByte;
while((theByte = bis.read()) != -1)
{
bos.write(theByte);
}
bis.close();
bos.close();

Okay, so a "move" operation is a copy with a "delete" at the end, for example...
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(sourceFile));
bos = new BufferedOutputStream(new FileOutputStream(targetFile));
int theByte;
while((theByte = bis.read()) != -1)
{
bos.write(theByte);
}
bos.close();
bis.close();
// You may want to verify that the file's are the same (ie the file size for example)
if (!sourceFile.delete()) {
throw new IOException("Failed to remove source file " + sourceFile);
}
} catch (IOException exp) {
exp.printStackTrace();
} finally {
try {
bis.close();
} catch (Exception exp) {
}
try {
bos.close();
} catch (Exception exp) {
}
}

Related

Create zip archive from a list of ByteArrayInputStream and return byte[] array of the archive - JAVA

I want to archive some files that I am receiving from an external source. I receive each of this files as a ByteArrayInputStream. In the end I want my method to create the zip archive and return a byte[] array so that I can download it later.
I have successfully managed to do this, but the end result is an ZipOutputStream and not the byte[] array that I need.
Any ideeas are appreciated. Thanks!
FileOutputStream fos = null;
ZipOutputStream zipOut = null;
try {
fos = new FileOutputStream(archiveFileName.concat(ReportFormat.ZIP.getFileExtension()));
zipOut = new ZipOutputStream(new BufferedOutputStream(fos));
for (Map.Entry<?, ByteArrayInputStream> entry : mapInputStream.entrySet()) {
String fileFromInsiteArchiveName = (String) entry.getKey();
ByteArrayInputStream inputStream = entry.getValue();
ZipEntry zipEntry = new ZipEntry(fileFromInsiteArchiveName);
zipOut.putNextEntry(zipEntry);
byte[] tmp = new byte[4 * 1024];
int size = 0;
while ((size = inputStream.read(tmp)) != -1) {
zipOut.write(tmp, 0, size);
}
zipOut.flush();
inputStream.close();
}
zipOut.close();
return zipOut;
} catch (FileNotFoundException e) {
//handle this
} catch (IOException e) {
//handle this
} finally {
try {
if (fos != null) fos.close();
} catch (Exception ex) {
//handlethis
}
}
return zipOut;
}
Copy to ByteArrayOutputStream and then do baos.toByteArray() to get your byte[]

Create text file, tar the file and put it into an inputstream?

I want to put a textfile containing just one string into a tar archive and put this tar file into an inputstream... I want to do this "on the fly", without saving any temporary files to the disk...
I tried out this snippet, where I end up with a file:
FileOutputStream fos = null;
try {
fos = new FileOutputStream ("helloworld.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String config = "HelloWorld!";
byte[] b = config.getBytes();
try {
baos.write(b);
baos.writeTo(fos);
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And I had a look at the jtar library:
https://code.google.com/p/jtar/
So I would like to put that textfile directly into that tar, but I also don't need that new FileOutputStream( "c:/test/test.tar" ), instead I want to put it into an inputstream directly.
I'm a bit confused with all those inputstreams/outputstreams and I'm not sure if it's even possible to move around a bunch of those streams instead of creating temporary files to do this.
THX & BR
Just get rid of the FileOutputStream and the bos.writeTo(), and do:
ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray());
instead.
I'm not sure if this is the best way to do it, but this seems to work, but not exactly as I imagined:
File tempFile = File.createTempFile("config", ".properties");
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
bw.write("Helloworld!!!!");
bw.close();
File tempTarFile = File.createTempFile("config", ".tar");
FileOutputStream dest = new FileOutputStream( tempTarFile );
TarOutputStream out = new TarOutputStream( new BufferedOutputStream( dest ) );
out.putNextEntry(new TarEntry(tempFile, "config.properties"));
BufferedInputStream origin = new BufferedInputStream(new FileInputStream( tempFile ));
int count;
byte data[] = new byte[2048];
while((count = origin.read(data)) != -1) {
out.write(data, 0, count);
}
out.flush();
origin.close();
out.close();
tempFile.delete();
tempTarFile.delete();
InputStream inputStream = new FileInputStream(tempTarFile);
//to check if it worked:
// OutputStream outputStream = null;
//
// try {
//
// // write the inputStream to a FileOutputStream
// outputStream =
// new FileOutputStream(new File("config.tar"));
//
// int read = 0;
// byte[] bytes = new byte[1024];
//
// while ((read = inputStream.read(bytes)) != -1) {
// outputStream.write(bytes, 0, read);
// }
//
// System.out.println("Done!");
//
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// if (inputStream != null) {
// try {
// inputStream.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// if (outputStream != null) {
// try {
// // outputStream.flush();
// outputStream.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// }
// }

Efficient way to write InputStream to a File in Java 6

I will get input stream from third party library to my application.
I have to write this input stream to a file.
Following is the code snippet I tried:
private void writeDataToFile(Stub stub) {
OutputStream os = null;
InputStream inputStream = null;
try {
inputStream = stub.getStream();
os = new FileOutputStream("test.txt");
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
} catch (Exception e) {
log("Error while fetching data", e);
} finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log("Error while closing input stream", e);
}
}
if(os != null) {
try {
os.close();
} catch (IOException e) {
log("Error while closing output stream", e);
}
}
}
}
Is there any better approach to do this ?
Since you are stuck with Java 6, do yourself a favour and use Guava and its Closer:
final Closer closer = Closer.create();
final InputStream in;
final OutputStream out;
final byte[] buf = new byte[32768]; // 32k
int bytesRead;
try {
in = closer.register(createInputStreamHere());
out = closer.register(new FileOutputStream(...));
while ((bytesRead = in.read(buf)) != -1)
out.write(buf, 0, bytesRead);
out.flush();
} finally {
closer.close();
}
Had you used Java 7, the solution would have been as simple as:
final Path destination = Paths.get("pathToYourFile");
try (
final InputStream in = createInputStreamHere();
) {
Files.copy(in, destination);
}
And yourInputStream would have been automatically closed for you as a "bonus"; Files would have handled destination all by itself.
If you're not on Java 7 and can't use fge's solution, you may want to wrap your OutputStream in a BufferedOutputStream
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream("xx.txt"));
Such buffered output stream will write bytes in blocks to the file, which is more efficient than writing byte per byte.
It can get cleaner with an OutputStreamWriter:
OutputStream outputStream = new FileOutputStream("output.txt");
Writer writer = new OutputStreamWriter(outputStream);
writer.write("data");
writer.close();
Instead of writing a string, you can use a Scanner on your inputStream
Scanner sc = new Scanner(inputStream);
while (sc.HasNext())
//read using scanner methods

Problems reading a huge file of 12 MB (java.lang.OutOfMemoryError)

i need to open a file of 12 Megabytes, but actually i'm doing it creating a buffer of 12834566-byte, because the size of the file is 12MB and i am developing this app for Android mobile systems.
Then, i supose i have to read with blocks of 1024 Kbytes instead of one block of 12 Mbytes, with a for, but i don't know how to do it, i need a little help with it.
This is my actual code:
File f = new File(getCacheDir()+"/berlin.mp3");
if (!f.exists()) try {
InputStream is = getAssets().open("berlin.mp3");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) { throw new RuntimeException(e); }
Please, can someone tell me what i have to changue in this code to read blocks of 1024 Kbytes instead of one block of 12 Mbytes?
THanks!
Try copying 1 KB at a time.
File f = new File(getCacheDir()+"/berlin.mp3");
if (!f.exists()) try {
byte[] buffer = new byte[1024];
InputStream is = getAssets().open("berlin.mp3");
FileOutputStream fos = new FileOutputStream(f);
int len;
while((len = is.read(buffer)) > 0)
fos.write(buffer, 0, len);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
IOUtils.close(is); // utility to close the stream properly.
IOUtils.close(fos);
}
Does Android support symbolic or hand links like UNIX? If it does, this would be faster/more efficient.
File f = new File(getCacheDir()+"/berlin.mp3");
InputStream is = null;
FileOutputStream fos = null;
if (!f.exists()) try {
is = getAssets().open("berlin.mp3");
fos = new FileOutputStream(f);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
fos.write(buffer);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// proper stream closing
if (is != null) {
try { is.close(); } catch (Exception ignored) {} finally {
if (fos != null) {
try { fos.close(); } catch (Exception ignored2) {}
}
}
}
}
import org.apache.commons.fileupload.util.Streams;
InputStream in = getAssets().open("berlin.mp3");
OutputStream out = new FileOutputStream(f);
Streams.copy(in, out, true);

Android - file download problem - incomplete file

I have checked many code snippets, tried with and without buffer and I can't get to download whole file to SD card. The code I use currently is:
try {
url = new URL("http://mywebsite.com/directory/");
} catch (MalformedURLException e1) { }
String filename = "someKindOfFile.jpg"; // this won't be .jpg in future
File folder = new File(PATH); // TODO: add checking if folder exist
if (folder.mkdir()) Log.i("MKDIR", "Folder created");
else Log.i("MKDIR", "Folder not created");
File file = new File(folder, filename);
try {
conn = url.openConnection();
is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
is.close();
} catch (IOException e) { }
This code creates directory on SD card but downloads only 77 bytes of files. What might be the problem?
The error here is that he was writing the count variable converted to byte datatype instead of the bytes read from the input stream (those should be stored in a temporary byte[] buffer via bis.read(buffer))
The proper code block should be:
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream(file);
int current = 0;
byte[] buffer = new byte[1024];
while ((current = bis.read(buffer)) != -1) {
fos.write(buffer, 0, current);
}
fos.close();
is.close();

Categories