java基础—IO流——字节流的操作演示

时间:2022-09-15 19:44:49



字节流的操作演示

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()
{
// 将Copy Mp3 文件的代码简写
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() {// 加入字节的缓冲区//向指定目录下Copy一个mp3文件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();}}}}



运行程序:



java基础—IO流——字节流的操作演示