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文件以字符流把它从一个文件读取输入到另一个文件结果文件变小,在不变上述代码的基础上,有没有一个转换流可以将字符流转换成字节流,正确实现文件的输入,如何转换呢?
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文件以字符流把它从一个文件读取输入到另一个文件结果文件变小,在不变上述代码的基础上,有没有一个转换流可以将字符流转换成字节流,正确实现文件的输入,如何转换呢?
你本身就是字节流啊
另外FileInputStream是字节流,byte的大小不能设置为文件大小-1,这样栈区会放不下.
byte[] b=new byte[(int)f.length()-1]; 改成1024试试..
创建一个使用默认字符集的 InputStreamReader。
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();
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();
}
}
}
如果你真想把Writer和Reader字符流转换成字节流的换,那就把字符流转换成byte[]数组,然后
InputStream input = new ByteArrayInputStream(byte[] buf)
创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。
这样不就转换成字节流了吗?
MP3文件是二进制数据文件,怎么使用Reader/Writer处理。
你可以使用字符流来完成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();
}
}
}}