您的位置:首页 > 编程语言 > PHP开发

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

2016-05-24 14:13 651 查看
http://www.iteye.com/problems/72150

写了一段代码 大体是 InputStream读取文件到string后OutputStream到文件
遇到的问题为TXT文件大小格式等都没有问题,但是PDF\RAR等格式的就无法打开了,重新生成的文件大小会比原文件小,代码如下。
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代码


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;

}

}

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

---------------------------------------------------------
这段是没有问题的,但是 InputStream读取文件到string后OutputStream到文件,中间多了一个string过程。
问题补充:
chen_yongkai 写道
有问题的代码呢?重点贴出,不要混在一起

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

Java综合

2011年9月21日 12:30


dongni110
20
0 0 0


添加评论



关注(0)


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

00

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

2014年3月28日 13:43


aqkf-2001
30
0 0 0


添加评论

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


chen_yongkai
1600
1 1 23


添加评论

00

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

2011年9月22日 08:21


chen_yongkai
1600
1 1 23


添加评论

00

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

Java代码


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;

}

}

这个是按字节拷贝的,貌似没什么问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: