黑马程序员技术交流社区

标题: 自写了一个写入的缓冲区来拷贝avi视频,为什么拷贝不全,求解 [打印本页]

作者: 郑传庆    时间: 2012-6-8 23:16
标题: 自写了一个写入的缓冲区来拷贝avi视频,为什么拷贝不全,求解
为什么我运行时,拷贝出来的视频文件怎么不全呢?是不是在读取的时候某个字节的时候,连续读取了8个1,在转换的时候返回的是-1啊?

public class MyBuffereReader {

        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                FileReader filereader = new FileReader("D:\\04.avi");
                FileWriter write = new FileWriter("D:\\14.avi");
                MyBufferedDemo my = new MyBufferedDemo(filereader);
                String l =null;
                /*把读取的文件写到另一个文件中*/
                while((l=my.myNewLine())!=null){
                        write.write(l, 0, l.length());
                }
                my.readerclose();
                write.close();
        }

        public static class MyBufferedDemo {
                FileReader reader=null;
                FileWriter write=null;
                public MyBufferedDemo(FileReader reader) {
                        this.reader = reader;
                }
                       
                /**
                 * 自定义一个写一行的方法
                 * @throws IOException
                 */
                public String myNewLine() throws IOException{
                        StringBuilder builer = new StringBuilder();
                        int len = 0;
                        while((len = reader.read())!=-1){
                                if(len=='\r')
                                        continue;
                                if(len=='\n')
                                        return builer.toString();
                                else
                                        builer.append((char)len);
                        }
                        if(builer.length()!=0){
                                return builer.toString();
                        }
                        return null;
                }
                /**
                 * 关闭读取流
                 * @throws IOException
                 */
                public void readerclose() throws IOException {
                        reader.close();
                }
        }
}
作者: 伊文龙    时间: 2012-6-8 23:21
{:soso_e126:}你用的字符流~~~
换字节流吧
作者: 邓杰    时间: 2012-6-8 23:27
哥。你这不是没有COPY完的问题,这个代码根本就不能COPY视频;这个叫字符读取流,只能读写文本、要COPY视频需要字节读取流:FileInputStream,FileOutputStream;
刚好我也刚刚写了一个一样的代码。分享下;
import java.io.*;
class  CopyMedia
{
        public static void main(String[] args)
        {
                FileOutputStream fos= null;
                FileInputStream fis = null;
                try
                {
                         fos=new FileOutputStream("f:\\陈奕迅 - K歌之王.mp3");
                         fis=new FileInputStream("d:\\陈奕迅 - K歌之王.mp3");

                         byte[] buf=new byte[1024*10];
                         int len=0;

                         while ((len=fis.read(buf))!=-1)
                         {
                                 fos.write(buf,0,len);
                         }
                       
                }
                catch (IOException e)
                {
                        throw new RuntimeException("读写失败");
                }
                finally
                {
                        try
                        {
                                if (fos!=null)
                                {
                                        fos.close();
                                }
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("读取关闭失败");
                        }
                        try
                        {
                                if (fis!=null)
                                {
                                        fis.close();
                                }
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("写入关闭失败");
                        }
                       
                }
               
        }
}

作者: 陈嘉宾    时间: 2012-6-8 23:27
。。。。恩看出来了呵呵,改成FileOutputStream,FileInputStream
作者: 陈嘉宾    时间: 2012-6-8 23:28
方法也不对啊重新写一下吧
作者: 郑传庆    时间: 2012-6-9 14:51
{:soso_e130:}正在学流的运用呢,所以不懂不奇怪。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2