InputStream读取文件到string后OutputStream到文件,按String和Bytes拷贝
-
写了一段代码 大体是 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格式的文档由问题吗?- 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;
- }
- }
这个是按字节拷贝的,貌似没什么问题
---------------------------------------------------------
这段是没有问题的,但是 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 转 outputstream2014年3月28日 13:430 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:401 1 23-
00
-
有问题的代码呢?重点贴出,不要混在一起
2011年9月22日 08:211 1 23-
00
-
好乱啊,是指这段拷贝pdf格式的文档由问题吗?
- 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;
- }
- }
这个是按字节拷贝的,貌似没什么问题
-->相关文章
- 10-05 【第201期】面试官:String的长度有限制吗?
- 10-05 【第256期】面试官经常测试的21条Linux命令
- 10-05 【第256期】面试官常测试的21条Linux命令
- 10-05 【第368期】为什么阿里巴巴禁止MyBatis使用
- 10-05 【第208期】我们来敲黑板,说说如何设计秒杀系统(
- 10-05 【第208期】我们来敲黑板,说说如何设计秒杀系统(
- 10-05 【344期】面试官:如何设计群聊消息的已读未读功能
- 10-05 【第328期】Spring高频面试题:如何解决循环
- 10-05 【第305期】面试官:你用过Redis吗?那么能否
- 10-05 为什么 C++ 是最难学的编程语言?看看大神的分析
- 10-05 为什么你的设计一次又一次地改变?这4个方法助你顺利
- 10-05 比看书更高效,这4个提高编程能力的方法你知道吗?
- 10-05 总线 Linux下I2C总线的配置和使用(linu
- 10-05 Linux文件夹链接技术指南(Linux文件夹链接
- 10-05 如何导出Oracle中不涉及的表(oracle导出
- 10-05 Linux下如何查找文件中的字符串(Linux文件
- 10-05 Linux:玩的乐趣(Linux好玩吗?)
- 10-05 改进Linux内核:驱动模块的重要性(linux内
- 10-05 keil5TIM中断_keil5中断编写方法
- 10-05 vs2010中如何防止反编译_vs反编译exe
- 最近发表
-
-
-
-
-
-
-