A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 成果 黑马帝   /  2012-2-4 12:40  /  2241 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

FileInputStream fin = new FileInputStream("D:/cosmo.jpg");//改为 txt格式能正常复制
                FileOutputStream fout = new FileOutputStream("F:/cosmo.jpg");//改为 txt格式能正常复制

                int len = 0;
                while ((len = fin.read()) != -1) {
                        fout.write(len);
                }
                fin.close();
                fout.close();
为什么我这代码能复制txt文件,但是非曲直doc,图片之类的就会出现乱码。

该贴已经同步到 寻找幸福的13狼的微博

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

9 个回复

倒序浏览
应该行啊,不都是操作字节流么。
我刚才试了试,行啊。
回复 使用道具 举报
我试了很多次复制doc.和图片。显示出来的为乱码。是不是哪个地方丢失了数据导致的?
回复 使用道具 举报
楼主图片,doc等都是二进制存储的,运用字节流是可以复制成功的
相同代码
import java.io.*;
public class Pic
{
        public static void main(String args[]) throws Exception
        {
                int temp = 0;
                        FileInputStream fin = new FileInputStream("d:/file/1.doc");
                        FileOutputStream fot = new FileOutputStream("d:/2222.doc");
                        while((temp = fin.read())!=-1)
                        {
                                fot.write(temp);
                        }
                        fin.close();
                        fot.close();
        }       
}
我试了图片,和doc都能正常显示的,再试试

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
import java.io.*;
class Test
{
        public static void main(String[] args)throws IOException
        {
                FileInputStream fin = new FileInputStream("E:/000.jpg");
         FileOutputStream fout = new FileOutputStream("00.jpg");
                int len = 0;
                while ((len = fin.read()) != -1)
                                {
                       fout.write(len);
                }
                fin.close();
                fout.close();
        }
}

同楼上,你的代码能可以复制jpg的


你是不是编译后运行的是上个class是文件

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
楼主图片,doc等都是二进制存储的,运用字节流是可以复制成功的
相同代码
import java.io.*;
public class Pic
{
        public static void main(String args[]) throws Exception
        {
                int temp = 0;
                        FileInputStream fin = new FileInputStream("d:/file/1.doc");
                        FileOutputStream fot = new FileOutputStream("d:/2222.doc");
                        while((temp = fin.read())!=-1)
                        {
                                fot.write(temp);
                        }
                        fin.close();
                        fot.close();
        }       
}
我试了图片,和doc都能正常显示的,再试试

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
张伟~ 黑马帝 2012-2-4 13:45:22
7#
试了下,用你这代码确实可以复制图片的,不过你这没加缓冲速度也太慢了点。

评分

参与人数 1技术分 +1 收起 理由
admin + 1 恭喜,25分了

查看全部评分

回复 使用道具 举报
成果 黑马帝 2012-2-4 14:14:29
8#
我试了一个简单的doc是行。但我换成一个我下载的基础试题这个doc就不行。
回复 使用道具 举报
成果 黑马帝 2012-2-4 14:28:21
9#
谢谢各位了。。已解决。原来是自己的问题。我用另一个byte[]读取时把值更改错误了。
回复 使用道具 举报
那就把问题改为已解决
同学
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马