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的功能,下面介绍 ...
随机推荐
- Python之路第八天,进阶-设计模式
设计模式 单例模式 单例,顾名思义单个实例. 学习单例之前,首先来回顾下面向对象的内容: python的面向对象由两个非常重要的两个"东西"组成:类.实例 面向对象场景一: 如:创 ...
- 初学swift笔记 方法(九)
import Foundation /* 方法 */ //实例方法 一定需要依附于对象 class MyPoint { var x: Double = 0.0 var y: Double = 0.0 ...
- Inno Setup:获取isl中的多国语言字串
原文 http://zwkufo.blog.163.com/blog/static/25882512010101041626803/?suggestedreading&wumii 用InnoS ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- CvMat、Mat、IplImage之间的转换详解及实例
见原博客:http://blog.sina.com.cn/s/blog_74a459380101obhm.html OpenCV学习之CvMat的用法详解及实例 CvMat是OpenCV比较基础的函数 ...
- ORACLE 表连接详解
在ORACLE中,表连接方式主要有:内连接,外连接,自连接: 内连接: 这是最常用的连接查询 SELECT * FROM A INNER JOIN B ON A.ID=B.ID SELECT * FR ...
- Flash Recovery Area
1. 设置闪回恢复区 闪回恢复区主要通过3个初始化参数来设置和管理 db_recovery_file_dest:指定闪回恢复区的位置db_recovery_file_dest_size:指定闪回恢复区 ...
- 【floyd求最小环】【Vijos 1046】【观光旅游】
标签:图结构 最短路 题目大意:给你一个无向图,至少经过3个节点的简单回路(不能包括其他环) 一开始的思路:用一个NUM[i][j]表示i到j的最短路经过几个节点,显然解法不太优美,而且还是错的 再想 ...
- C#整理5——break与continue.及数组
一.break与continue.这两个关键字一般放在循环的花括号里面使用.break——结束整个循环.continue——结束本次循环,进入下次循环. break的案例: using System; ...
- 对js中prototype的理解
一直不理解child.prototype = new Parent()和child.prototype =Parent.prototype的区别,到现在为止,我觉得它俩最大的区别就是:前者共享构造器里 ...
添加评论
关注(0)