/*一,操作文件
* 1.操作字符,也可以操作其他媒体文件
* 字节流可以复制图片等文件,但是字符流不可
* 2.InputStream
* OutputStream
* 3.直接将数据写入到目的地中,有flush函数只是单单继承了父类函数的原因
* 4.代码
public class 字节流 {
public static void main(String[]args) throws IOException {
read_();
write_();
}
public static void read_() throws IOException {
FileInputStream readStream=new FileInputStream("in.txt");
// byte[]arr=new byte[readStream.available()];//此方法在文件较大时不要使用
// readStream.read(arr);
// System.out.println(new String(arr));
// byte[]arr=new byte[3];
// int len=0;
// while((len=readStream.read(arr))!=-1) {
// System.out.println(new String(arr,0,len));
// }
int ch=0;
while((ch=readStream.read())!=-1)
System.out.println((char)ch);
}
public static void write_() throws IOException {
FileOutputStream writerStream=new FileOutputStream("out.txt");
writerStream.write("abcdefg".getBytes());
}
}
*二,mp3复制
*代码
public class 字节流
{
public static void main(String []args) throws IOException {
Copy2();
}
//复制方式一,自己定义字节数组做缓冲区
public static void Copy1() throws IOException {
FileInputStream fielStream=new FileInputStream("D:\\CloudMusic\\butterfly.mp3");
FileOutputStream fileOutputStream=new FileOutputStream("D:\\CloudMusic\\butterfly1.mp3");
int len=0;
byte[]arr=new byte[1024];
while((len=fielStream.read(arr))!=-1) {
fileOutputStream.write(arr,0,len);
}
fileOutputStream.close();
fielStream.close();
}
//复制方式二,使用缓冲区
public static void Copy2() throws IOException {
FileInputStream fielStream=new FileInputStream("D:\\CloudMusic\\butterfly.mp3");
BufferedInputStream bufferedInputStream=new BufferedInputStream(fielStream);
FileOutputStream fileOutputStream=new FileOutputStream("D:\\CloudMusic\\butterfly1.mp3");
BufferedOutputStream fileBufferedOutputStream=new BufferedOutputStream(fileOutputStream);
int ch=0;
while((ch=bufferedInputStream.read())!=-1) {
fileBufferedOutputStream.write(ch);
fileBufferedOutputStream.flush();
}
fileOutputStream.close();
fielStream.close();
}
}