Last active
May 24, 2017 14:25
-
-
Save thetekst/79bb30fb76f0d185cd49ac54f3d669bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final String FILE_SRC_PATH = "src/main/resources/logo.jpg"; | |
private static final String FILE_DEST_PATH = "src/main/resources/logo-copy.jpg"; | |
// копируем jpg-изображение | |
// не работает | |
// исходное изображение 13.1 kB | |
// новое изображение размером: | |
// 1. при условии (len = fin.read()) != -1 равняется 1.6MB | |
// 2. при условии (len = fin.read()) > 0 равняется 950 byte | |
try(FileInputStream fin = new FileInputStream(FILE_SRC_PATH); | |
FileOutputStream fout = new FileOutputStream(FILE_DEST_PATH)) { | |
byte[] buff = new byte[1024]; | |
int len; | |
while ((len = fin.read(buff)) != -1) { | |
fout.write(buff, 0, len); | |
} | |
} catch (IOException ioex) { | |
System.out.println("Failed to copy files : " + ioex.getMessage()); | |
ioex.printStackTrace(); | |
} | |
// рабочий вариант | |
// копируем jpg-изображение | |
InputStream input = null; | |
OutputStream output = null; | |
try { | |
input = new FileInputStream(FILE_SRC_PATH); | |
output = new FileOutputStream(FILE_DEST_PATH); | |
byte[] buf = new byte[1024]; | |
int bytesRead; | |
while ((bytesRead = input.read(buf)) != -1) { | |
output.write(buf, 0, bytesRead); | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
input.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
try { | |
output.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment