• InputStream读取文件到string后OutputStream到文件,按String和Bytes拷贝


    写了一段代码 大体是 InputStream读取文件到string后OutputStream到文件 
    遇到的问题为TXT文件大小格式等都没有问题,但是PDFRAR等格式的就无法打开了,重新生成的文件大小会比原文件小,代码如下。 
    package com.stream; 

    import java.io.BufferedReader; 
    import java.io.ByteArrayOutputStream; 
    import java.io.DataOutputStream; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import java.io.OutputStream; 
    import java.io.Reader; 
    import java.util.Arrays; 

    public class bytearry { 
    // public static void main(String[] args) throws Exception{ 
    //        String s = "中国"; 
    //        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    //        DataOutputStream dos = new DataOutputStream(baos); 
    //        dos.writeUTF(s);     
    //        byte[] b = baos.toByteArray(); 
    //        for(int i=0;i<b.length;i++){ 
    //            System.out.println(Integer.toHexString(b[i])); 
    //        } 
    //        System.out.println(">>>"+new String(b,"UTF-8")+"<<<");        
    //        System.out.println("--------------------"); 
    //        byte[] b2 = s.getBytes("UTF-8"); 
    //        for(int i=0;i<b2.length;i++){ 
    //            System.out.println(Integer.toHexString(b2[i])); 
    //        } 
    //    } 

    // public static void main(String[] args) throws IOException { 
    //   String str = "Hello world!"; 
    //   // string转byte 
    //   byte[] bs = str.getBytes(); 
    //   System.out.println(Arrays.toString(bs)); 
    //   
    //   // byte转string 
    //   String str2 = new String(bs); 
    //   System.out.println(str2); 
    //   
    //   OutputStream os = new FileOutputStream("C://testCopy11.pdf");  //输出流 
    //   FileInputStream fis = new FileInputStream("d://test.pdf");  //输入流 
    //   //InputStreamReader     
    //   Reader  inputStreamReader = new InputStreamReader(fis); 
    //   BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
    //   
    //   String ss = new String();    
    //   String s;    
    //   while((s = bufferedReader.readLine())!=null){    
    //           ss += s;    
    //   } 
    //   //System.out.println(ss); 
    //   
    //   byte[] buf = new byte[255]; 
    //   
    //   buf = ss.getBytes(); 
    //   
    //   
    //
    // 
    //     int len = 0;  
    //     //while ((len = buf.length) != -1) {  
    //     // os.write(buf, 0, len); 
    //     os.write(buf); 
    //    // }  
    //   
    //     fis.close();  
    //     os.flush();  
    //     os.close(); 
    //     //copy(); 
    // 
    // } 

    public static void main(String[] args) throws IOException { 
      String str = "Hello world!"; 
      // string转byte 
      byte[] bs = str.getBytes(); 
      System.out.println(Arrays.toString(bs)); 
      
      // byte转string 
      String str2 = new String(bs); 
      System.out.println(str2); 
      
      OutputStream os = new FileOutputStream("C://bytearry.java");  //输出流 
      FileInputStream fis = new FileInputStream("d://bytearry.java");  //输入流 
      //InputStreamReader     
      Reader  inputStreamReader = new InputStreamReader(fis); 
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
      
      String ss = new String();    
      String s;    
      while((s = bufferedReader.readLine())!=null){    
              ss += s;    
      } 
      System.out.println(ss.length()); 
      
      byte[] buf = new byte[255]; 
      
      buf = loadAFileToStringDE2(new File("d://bytearry.java")).getBytes(); 
      int len = 0;  
        //while ((len = buf.length) != -1) {  
        // os.write(buf, 0, len); 
        os.write(buf); 
       // }   
        fis.close();  
        os.flush();  
        os.close(); 
        //copy(); 
        System.out.println(loadAFileToStringDE2(new File("d://bytearry.java"))); 

    }
       public static String loadAFileToStringDE2(File f) throws IOException {  
            long beginTime = System.currentTimeMillis(); 
            InputStream is = null; 
            String ret = null; 
            try { 
                is =  new FileInputStream(f) ; 
                long contentLength = f.length(); 
                byte[] ba = new byte[(int)contentLength]; 
                is.read(ba); 
                ret = new String(ba); 
            } finally { 
                if(is!=null) {try{is.close();} catch(Exception e){} } 
            } 
            long endTime = System.currentTimeMillis(); 
            System.out.println("方法2用时"+ (endTime-beginTime) + "ms"); 
            return ret;        
        } 
       
    public static boolean copy() {  
       try {  
        OutputStream os = new FileOutputStream("C://test.pdf");  //输出流 
        InputStream fis = new FileInputStream("d://test.pdf");  //输入流 
      
        byte[] buf = new byte[255];  
        int len = 0;  
        while ((len = fis.read(buf)) != -1) {  
        os.write(buf, 0, len);  
        }  
      
        fis.close();  
        os.flush();  
        os.close();  
      
        return true;  
       } catch (FileNotFoundException e) {  
        // TODO Auto-generated catch block   
        e.printStackTrace();  
        return false;  
       } catch (IOException e) {  
        // TODO Auto-generated catch block   
        e.printStackTrace();  
        return false;  
       }  
    }  



    问题补充:
    chen_yongkai 写道
    好乱啊,是指这段拷贝pdf格式的文档由问题吗? 
    Java代码  收藏代码
    1. public static boolean copy() {  
    2.         try {  
    3.             OutputStream os = new FileOutputStream("C://test.pdf"); // 输出流  
    4.             InputStream fis = new FileInputStream("d://test.pdf"); // 输入流  
    5.   
    6.             byte[] buf = new byte[255];  
    7.             int len = 0;  
    8.             while ((len = fis.read(buf)) != -1) {  
    9.                 os.write(buf, 0, len);  
    10.             }  
    11.   
    12.             fis.close();  
    13.             os.flush();  
    14.             os.close();  
    15.   
    16.             return true;  
    17.         } catch (FileNotFoundException e) {  
    18.             // TODO Auto-generated catch block  
    19.             e.printStackTrace();  
    20.             return false;  
    21.         } catch (IOException e) {  
    22.             // TODO Auto-generated catch block  
    23.             e.printStackTrace();  
    24.             return false;  
    25.         }  
    26.     }  


    这个是按字节拷贝的,貌似没什么问题

    --------------------------------------------------------- 
    这段是没有问题的,但是 InputStream读取文件到string后OutputStream到文件,中间多了一个string过程。

    问题补充:
    chen_yongkai 写道
    有问题的代码呢?重点贴出,不要混在一起



    代码是可以运行的吧,下面的行就是InputStream读取文件到string 到byte[]啊。 
    buf = loadAFileToStringDE2(new File("d://bytearry.java")).getBytes(); 
    2011年9月21日 12:30
     
     
     

    4个答案按时间排序按投票排序

    00

    请参见http://blog.csdn.net/maya2000/article/details/22394933 
    inputstream 转 string 转 outputstream

    2014年3月28日 13:43
     
    00

    找到问题了: 
    loadAFileToStringDE2方法里 
    byte[] ba = new byte[(int)contentLength]; 
                is.read(ba); 
                ret = new String(ba); //这里用的是平台默认编码 

    调用处: 
    buf = loadAFileToStringDE2(new File("d://bytearry.java")).getBytes();//用的也是平台默认编码 


    应该改为: 
    ret = new String(ba,"ISO8859-1"); 

    和 

    buf = loadAFileToStringDE2(new File("d://bytearry.java")).getBytes("ISO8859-1"); 

    平台默认编码有可能不包括某些字符,因而丢失了数据

    2011年9月26日 09:40
     
    00

    有问题的代码呢?重点贴出,不要混在一起

    2011年9月22日 08:21
     
    00

    好乱啊,是指这段拷贝pdf格式的文档由问题吗? 

    Java代码  收藏代码
    1. public static boolean copy() {  
    2.         try {  
    3.             OutputStream os = new FileOutputStream("C://test.pdf"); // 输出流  
    4.             InputStream fis = new FileInputStream("d://test.pdf"); // 输入流  
    5.   
    6.             byte[] buf = new byte[255];  
    7.             int len = 0;  
    8.             while ((len = fis.read(buf)) != -1) {  
    9.                 os.write(buf, 0, len);  
    10.             }  
    11.   
    12.             fis.close();  
    13.             os.flush();  
    14.             os.close();  
    15.   
    16.             return true;  
    17.         } catch (FileNotFoundException e) {  
    18.             // TODO Auto-generated catch block  
    19.             e.printStackTrace();  
    20.             return false;  
    21.         } catch (IOException e) {  
    22.             // TODO Auto-generated catch block  
    23.             e.printStackTrace();  
    24.             return false;  
    25.         }  
    26.     }  


    这个是按字节拷贝的,貌似没什么问题
  • 相关阅读:
    关于MD5的个人笔记
    QueryString 页面传值方法
    酒店管理系统房态图的效果制作
    开始我的学习之路
    SQL取数据库名,取表名,取列名
    鼠标/键盘事件
    C#皮肤使用例子.
    C#调用WIN API
    C#中定时器的使用方法
    C#随机点名程序例子(名字由配置文件提供)
  • 原文地址:https://www.cnblogs.com/timssd/p/5523246.html
Copyright © 2020-2023  润新知