欢迎来到Introzo百科
Introzo百科
当前位置:网站首页 > 技术 > InputStream读取文件到string后OutputStream到文件,按String和Bytes拷贝

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

日期:2023-09-28 16:38

-->
http://www.introzo.com/problems/72150
写了一段代码 大体是 InputStream读取文件到string后OutputStream到文件 
遇到的问题为TXT文件大小格式等都没有问题,但是PDF\RAR等格式的就无法打开了,重新生成的文件大小会比原文件小,代码如下。 
package www.introzo.com;

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//            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//            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://www.introzo.com");  //输出流 
  FileInputStream fis = new FileInputStream("d://www.introzo.com");  //输入流 
  //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://www.introzo.com")).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://www.introzo.com")));

}
   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]; 
            www.introzo.com(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 = www.introzo.com(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格式的文档由问题吗?

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

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

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


问题补充:

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

代码是可以运行的吧,下面的行就是InputStream读取文件到string 到byte[]啊。 
buf = loadAFileToStringDE2(new File("d://www.introzo.com")).getBytes(); 

Java综合 

2011年9月21日 12:30

0 0 0
 
 
 

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

00

请参见http://www.introzo.com/maya2000/article/details/22394933 
inputstream 转 string 转 outputstream

2014年3月28日 13:43
0 0 0
 
00

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

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

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

buf = loadAFileToStringDE2(new File("d://www.introzo.com")).getBytes("ISO8859-1");

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

2011年9月26日 09:40
1 1 23
 
00

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

2011年9月22日 08:21
1 1 23
 
00

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

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

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

-->