InputStream读取文件到string后OutputStream到文件,按String和Bytes拷贝
-
写了一段代码 大体是 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格式的文档由问题吗?- 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();2011年9月21日 12:30
4个答案按时间排序按投票排序
-
请参见http://blog.csdn.net/maya2000/article/details/22394933
inputstream 转 string 转 outputstream2014年3月28日 13:43
-
找到问题了:
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
-
好乱啊,是指这段拷贝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 = 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和Bytes拷贝的更多相关文章
- python 压缩文件为zip后删除原文件
压缩.log 文件为zip后删除原文件 需要注意:本人作为小白,该脚本需要和.log在一起,后面有时间需要改正. #!/usr/local/python/bin/python #-*-coding=u ...
- IDEA中部署tomcat,运行JSP文件,编译后的JSP文件存放地点总结
首先保证你正常部署了Tomcat,并且正常在浏览器中运行了JSP文件. 那么Tomcat编译后的JSP文件(_jsp.class 和 _jsp.java)的存放地点: (一)一般存放在你安装的Tomc ...
- Itext读取PDF模板文件渲染数据后创建新文件
Maven导入依赖 <properties> <itextpdf.version>5.5.0</itextpdf.version> <itext-asian. ...
- Linux文件被删除后恢复
当ext4中的文件被删除后,进行文件恢复:http://www.360doc.com/content/18/0320/08/51898798_738625260.shtml上面的博客是恢复删除的文件, ...
- Android下使用InputStream读取文件
在Android下使用InputStream读取文件. 如果不是从头开始读取文件,使用skip 后 在读取文件 使用read读取的长度为-1会获取不到数据. 换成RandomAccessFile 使用 ...
- 关于AysncController的一次测试(url重写后静态页文件内容的读取是否需要使用异步?)
因为做网站的静态页缓存,所以做了这个测试 MVC项目 准备了4个Action,分两组,一组是读取本地磁盘的一个html页面文件,一组是延时2秒 public class TestController ...
- C# 中使用Image.FromFile(string path)后,提示该文件正在被另一进程使用XXX的问题
C# 中使用Image.FromFile(string path)后,提示该文件正在被另一进程使用XXX的问题 C# 中使用Image.FromFile(string path)后,提示该文件正在被另 ...
- [Head First Python]4.读取文件datafile.txt, 去除两边空格, 存储到列表,从列表格式化(nester.py)后输出到文件man.out,other.out
datafile.txt #文件 Man: this is the right room for an argument. Other Man: I've told you once. Man: N ...
- weblogic对JSP预编译、weblogic读取JSP编译后的class文件、ant中weblogic.jspc预编译JSP
我们都知道在weblogic中JSP是每次第一次访问的时候才会编译,这就造成第一次访问某个JSP的时候性能下降,有时候我们也希望JSP被编译成class然后打包在jar中实现隐藏JSP的功能,下面介绍 ...
随机推荐
- 在右键添加Cmder here选项,添加启动Cmder的快捷键
右键菜单添加“Cmder here” 打开cmder,在其中输入: cmder /register user 或 cmder /register all 即可 设置启动cmder的快捷键 右键 C ...
- python核心编程-第三章-习题
1.这是python的语言特性,python先创建对象,在给变量赋值时,不需要定义变量的名称和类型,它实际是用变量引用对象.变量类型在给变量赋值时自动声明 2.原因类似变量无须声明类型 3.pytho ...
- MFC 遍历FTP服务器目录中文乱码问题
在编写FTP客户端的时候我用的是server u来做我的测试服务器,而server u 默认使用utf-8作为默认字符集,vs则使用unicode作为默认字符集,所以会产生乱码,将server u的默 ...
- android的listview组件
http://www.cnblogs.com/menlsh/archive/2013/03/15/2962350.html http://www.tuicool.com/articles/IveeI3 ...
- js的框架
Ember.js的一些学习总结 1.1.1 摘要 现在,我们经常都可以看到复杂的JavaScript应用程序,由于这些应用程序变得越来越复杂,一长串的jQuery回调语句或者通过应用程序在各个状态 ...
- WMI问题终极解决
1. 如果用JInterOp,先用这篇文章叙述的: http://m.oschina.net/blog/73163 2.另外开启这个: https://dev.c-ware.de/confluence ...
- Mongo客户端
http://www.linuxidc.com/Linux/2012-07/64233.htm http://www.oschina.net/p/rockmongo http://www.cnblog ...
- #pragma pack(n) 的作用
在C语言中,结构是一种复合数据类型,其构成元素既可以是基本数据类型(如int.long.float等)的变量,也可以是一些复合数据类型(如数组.结构.联合等)的数据单元.在结构中,编译器为结构的每个成 ...
- centos curl web站点监控实践
1,监控给定web站点的状态--站点请求返回代码,下载整个web站点页面文本到-o 指定的文本 curl -o /dev/null -s-silent -w--wirte-out "%{ht ...
- 使用xmanager 远程redhat6.3
之前装过一次,特别麻烦,装上只有远程还卡卡的,这次按照教程居然装的灰常顺利,不符合我bug体质的特性,一定要记下来啊~~~ 1.先关闭防火墙 # service iptables stop #chkc ...
添加评论
关注(0)