java基础—IO流——字节流的操作演示
原创
©著作权归作者所有:来自51CTO博客作者早起的年轻人的原创作品,请联系作者获取转载授权,否则将追究法律责任
字节流的操作演示
1.将一个文本通过字符流操作复制到指定目录下
2.将一个文本通过字节流操作复制到指定目录下
3.将一个Mp3文件通过字节流操作方式复制到指定目录下,并加入缓冲技术
4.简写复制Mp3文件的代码
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileByteStreamDemo
{
public static void main(String[] args)
{
TextCopy();
TextCopy2();
Mp3Copy3();
Mp3Copy4();
}
private static void Mp3Copy4()
{
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis=new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\测试文件夹\\陈楚生-黄金时代.mp3"));
bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\测试文件夹\\2ByteStreamCopyMp3.mp3"));
int len = 0;
byte[] b = new byte[1024*1024];
while((len=bis.read(b))!=-1)
{
bos.write(b);
bos.flush();
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
try
{
bos.close();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
bis.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
<h2><span style="font-family:FangSong_GB2312;">3.将一个Mp3文件通过字节流操作方式复制到指定目录下,并加入缓冲技术</span></h2>
private static void Mp3Copy3() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis =null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\测试文件夹\\陈楚生-黄金时代.mp3");
bis = new BufferedInputStream(fis);
fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\测试文件夹\\ByteStreamCopyMp3.mp3");
bos = new BufferedOutputStream(fos);
int len = 0;
byte[] b = new byte[1024*1024];
while((len=bis.read(b))!=-1)
{
bos.write(b);
bos.flush();
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
try
{
bis.close();
} catch (IOException e)
{
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
<h2><span style="font-family:FangSong_GB2312;">2.将一个文本通过字节流操作复制到指定目录下</span></h2>
private static void TextCopy2() {
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\测试文件夹\\初三化学复习资料.doc");
fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\测试文件夹\\ByteStreamCopyText.doc");
int len = 0;
byte[] b = new byte[1024];
while((len=fis.read(b))!=-1)
{
fos.write(b);
fos.flush();
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
try
{
fos.close();
} catch (IOException e1)
{
e1.printStackTrace();
}
try
{
fis.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
<h2><span style="font-family:FangSong_GB2312;">
</span></h2><h2>
</h2>
<h2 style="font-family: monospace; white-space: pre;"><span style="font-family: FangSong_GB2312;">1.将一个文本通过字符流操作复制到指定目录下</span></h2>
private static void TextCopy() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br= new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\测试文件夹\\初三化学复习资料.doc"));
bw = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\测试文件夹\\Copy复习资料.doc"));
String len = null;
while((len=br.readLine())!=null)
{
System.out.println(len);
bw.write(len);
bw.newLine();
bw.flush();
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
try
{
bw.close();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
br.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
运行程序: