读取图片演示字节流 使用字节流读取文本可能出现乱码问题(不在内存读,直接复制到另一个文件,不存在问题)

一、字节流进行读入读出复制

ByteStreamCopyUtils.java

  1. public class ByteStreamCopyUtils {
  2. public static void main(String[] args) {
  3. copy("","");
  4. }
  5. public static void copy(String scrPath,String descPath){
  6. FileInputStream fileInputStream = null;
  7. FileOutputStream fileOutputStream = null;
  8. try {
  9. // 1、文件
  10. File scrFile = new File(scrPath);
  11. File descFile = new File(descPath);
  12. // 2、流
  13. fileInputStream = new FileInputStream(scrFile);
  14. fileOutputStream = new FileOutputStream(descFile);
  15. // 3、复制
  16. byte[] data = new byte[1024];
  17. int len;
  18. while ( ( len = fileInputStream.read(data) ) != -1 ){
  19. fileOutputStream.write(data,0,len);
  20. }
  21. }catch (Exception e){
  22. e.printStackTrace();
  23. }finally {
  24. if(null != fileInputStream){
  25. try {
  26. fileInputStream.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. if(null != fileOutputStream){
  32. try {
  33. fileOutputStream.close();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. }
  40. }