File f=new File("D:"+File.separator+"音乐"+File.separator+"布列瑟农.mp3");
InputStream is=new FileInputStream(f);
OutputStream os=new FileOutputStream("F:"+File.separator+"狼.mp3");
byte[] b=new byte[(int)f.length()-1];
int len=0;
while((len=is.read(b))!=-1){
os.write(b, 0, len);
}
is.close();
os.close();
一个Mp3文件以字符流把它从一个文件读取输入到另一个文件结果文件变小,在不变上述代码的基础上,有没有一个转换流可以将字符流转换成字节流,正确实现文件的输入,如何转换呢?

解决方案 »

  1.   

    InputStreamReader可以将字节流转换成字符流
    你本身就是字节流啊
      

  2.   

    Java类库里米有字符流转字节流的,原因很简单,字符流已经很方便了,为什么还要转字节呢..
    另外FileInputStream是字节流,byte的大小不能设置为文件大小-1,这样栈区会放不下.
    byte[] b=new byte[(int)f.length()-1];  改成1024试试..
      

  3.   

    看错啦, 是new 出啦的引用,和栈大小无关...
      

  4.   

    InputStreamReader(InputStream in) 
              创建一个使用默认字符集的 InputStreamReader。
      

  5.   

    文件复制错了,是这样的:
                      File f=new File("D:"+File.separator+"69948.jpg");
    Reader r=new FileReader(f);
    int len=0;
    char[] c=new char[(int)f.length()-1];
    len=r.read(c);

    Writer w=new FileWriter("E:"+File.separator+"蒲公英.jpg"); 
    w.write(c, 0, len);

    r.close();
    w.close();
      

  6.   

    这样复制完全没问题的 public static void main(String[] args){
    File f = new File("E:\\music\\爱向着我来的那天.mp3");
    InputStream is = null;
    OutputStream os = null;
    try {
    is = new FileInputStream(f);
    os = new FileOutputStream("E:\\music\\爱向着我来的那天__new.mp3");
    byte[] b = new byte[1024];
    int len = 0;
    while ((len = is.read(b)) != -1) {
    os.write(b, 0, len);
    }
    os.flush();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally{
    try {
    if(is != null)
    is.close();
    if(os != null)
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
      

  7.   

    我知道这样会没问题,而且处理异常的方式也很专业,但用的是字节流FileInputStream和FileOutpurStream,我用的是字符流Reader和Writer,如何将字符流转换成字节流呢?
      

  8.   

    怎么将Writer和Reader字符流转换成字节流呢?
      

  9.   

    你转换成字节流可能读中文的时候会出现乱码,因为一个中文是2个字节,因为字节流是一个字节一个字节读的。
    如果你真想把Writer和Reader字符流转换成字节流的换,那就把字符流转换成byte[]数组,然后
    InputStream input = new ByteArrayInputStream(byte[] buf) 
              创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。
    这样不就转换成字节流了吗?
      

  10.   

    晕。
    MP3文件是二进制数据文件,怎么使用Reader/Writer处理。
      

  11.   

    同意上楼,MP3文件本身是二进制文件,,而不是字符表示的文件,例如txt文件,lrc文件等,你用字符流处理不合适,
    你可以使用字符流来完成txt文件等的传输,何况你的代码本身就是用的字节流;我也完成了类似的功能,代码如下:我也试着用字节流来传输txt文件,结果发现文件内容一模一样,但文件变大了,这个还没有想明白为啥,实现字节流传输txt只要将mp3文件改为txt文件即可。用字符流实现txt在这就不写了,道理差不多。import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class ReadMusic {
    public static void main(String[] args)
    {  File myfile1=null;
       File myfile2=null;
       FileInputStream myfile=null;
       FileOutputStream foutput=null;
       BufferedInputStream s1=null;
       BufferedOutputStream s2=null;
    try{
    myfile1=new File("c:/","飞向别人的床.mp3");
       String filename=myfile1.getName();
       myfile2=new File("d:/",filename);
       myfile2.createNewFile();
       }
     catch(IOException e){
     e.printStackTrace();}
     
    try{
    myfile =new FileInputStream("c:/飞向别人的床.mp3");
        s1=new BufferedInputStream(myfile);
    }
    catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    try
    {
    foutput=new FileOutputStream(myfile2);
    s2=new BufferedOutputStream(foutput);
    byte buffer [] = new byte[4 * 1024];
    while((s1.read(buffer)) != -1){
    s2.write(buffer);
    }
    s2.close();//这条语句应该必须有,否则最后Buffer缓冲区的内容会被丢掉。现实的结果就是MP3文件变小了
    }
    catch(Exception e){
    e.printStackTrace();}
    finally{
    try{
    foutput.close();
    }
    catch(Exception e){
    e.printStackTrace();
    }
    }

    }}