文件输入输出流(复制文本,视频,音频,图片的多种方式)

来源:互联网 发布:access 找不到数据库 编辑:程序博客网 时间:2024/06/11 01:07

FileInputStream文件输入输出流
public FileInputStream(String name)
读数据的方式:
1) 一次读取一个字节:public int read();
2)一次读取一个字节数组:public int read(byte[] byt)

具体代码如下:

//一次读取一个字节public static void main(String[] args){    FileInputStream fis=new FileInputStream("F:\\A.txt");    int by=fis.read();    System.out.println((char)by);   }
//一次读取一个字节数组package _13.homework;import java.io.FileInputStream;import java.io.IOException;public class Demo1 {    public static void main(String[] args) throws IOException {        FileInputStream fis = new FileInputStream("F:\\a.txt");        byte[] by = new byte[1024];        int len = fis.read(by);        System.out.println(len);        System.out.println(new String(by));    }}

复制文本的多种方法:
1)

package _13.homework;//用字节数组复制import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Demo1 {    public static void main(String[] args) throws IOException {        FileInputStream fis = new FileInputStream("F:\\a.txt");        FileOutputStream fos = new FileOutputStream("F:\\b.txt");        byte[] by = new byte[1024];        int len = 0;        while ((len = fis.read(by)) != -1) {            fos.write(by, 0, len);        }        fis.close();        fos.close();    }}

2)

package _13.homework;//一次复制一个字节import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Demo1 {    public static void main(String[] args) throws IOException {        FileInputStream fis = new FileInputStream("F:\\a.txt");        FileOutputStream fos = new FileOutputStream("F:\\b.txt");        int by = 0;        while ((by = fis.read()) != -1) {            fos.write(by);        }        fis.close();        fos.close();    }}

BufferedInputStream文件输入输出流
3)

package _13.homework;//一次复制一个字节import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Demo1 {    public static void main(String[] args) throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(                "F:\\a.txt"));        BufferedOutputStream bos = new BufferedOutputStream(                new FileOutputStream("F:\\b.txt"));        int by = 0;        while ((by = bis.read()) != -1) {            bos.write(by);            bos.flush();        }        bis.close();        bos.close();    }}

4)

package _13.homework;//一次复制一个字节数组import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Demo1 {    public static void main(String[] args) throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(                "F:\\a.txt"));        BufferedOutputStream bos = new BufferedOutputStream(                new FileOutputStream("F:\\b.txt"));        byte[] bys=new byte[1024];        int len=0;        while ((len = bis.read(bys)) != -1) {            bos.write(bys,0,len);            bos.flush();        }        bis.close();        bos.close();    }}

5)

public class CopyFileDemo10 {    public static void main(String[] args) throws IOException{        BufferedReader br=new BufferedReader(new FileReader("F:\\a.txt"));        BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\k.txt"));        char[] chs=new char[1024];        int len=0;        while((len=br.read(chs))!=-1){            bw.write(chs,0,len);            bw.flush();        }        br.close();        bw.close();    }}

6)

public class CopyFileDemo9 {    public static void main(String[] args) throws IOException{        BufferedReader br=new BufferedReader(new FileReader("F:\\a.txt"));        BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\j.txt"));        int by=0;        while((by=br.read())!=-1){            bw.write(by);            bw.flush();        }        br.close();        bw.close();    }}

7)

public static void main(String[] args) throws IOException{        FileReader fr=new FileReader("F:\\a.txt");        FileWriter fw=new FileWriter("D:\\f.txt");        char[] chs=new char[1024];        int len=0;        while((len=fr.read(chs))!=-1){            fw.write(chs,0,len);            fw.flush();        }        fr.close();        fw.close();    }}

8)

public class CopyFileDemo4 {    public static void main(String[] args) throws IOException{        FileReader fr=new FileReader("F:\\a.txt");        FileWriter fw=new FileWriter("D:\\e.txt");        int by=0;        while((by=fr.read())!=-1){            fw.write(by);            fw.flush();        }        fr.close();        fw.close();    }}

9)

public class CopyFileDemo7 {    public static void main(String[] args) throws IOException{        InputStreamReader isr=new InputStreamReader(new FileInputStream("F:\\a.txt"));        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("D:\\h.txt"));        int by=0;        while((by=isr.read())!=-1){            osw.write(by);            osw.flush();        }        isr.close();        osw.close();    }}

10)

public class CopyFileDemo8 {    public static void main(String[] args) throws IOException{        InputStreamReader isr=new InputStreamReader(new FileInputStream("F:\\a.txt"));        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("D:\\i.txt"));        char[] chs=new char[1024];        int len=0;        while((len=isr.read(chs))!=-1){            osw.write(chs,0,len);            osw.flush();        }        isr.close();        osw.close();    }}

11)

public class CopyFileDemo11 {    public static void main(String[] args) throws IOException{        BufferedReader br=new BufferedReader(new FileReader("F:\\a.txt"));        BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\l.txt"));         String line=null;         while((line=br.readLine())!=null){             int len=Integer.parseInt(line);              bw.write(line,0,len);             bw.flush();         }        br.close();        bw.close();    }}

复制图片的多种方法:
1)

public class CopyImageDmeo1 {    public static void  main(String[] args) throws IOException{        FileInputStream fis = new FileInputStream("F:\\jietu.JPG") ;        FileOutputStream fos = new FileOutputStream("D:\\study1.JPG") ;        byte[] bys = new byte[1024] ;        int len = 0 ;        while((len=fis.read(bys))!=-1){            fos.write(bys, 0, len) ;        }        fis.close() ;        fos.close() ;    }}

2)

public class CopyImageDmeo2 {    public static void main(String[] args) throws IOException{        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\jietu.JPG"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\study2.JPG"));        byte[] bys=new byte[1024];        int len=0;        while((len=bis.read(bys))!=-1){            bos.write(bys,0,len);            bos.flush();        }        bis.close();        bos.close();    }}

3)

public class CopyImageDmeo3 {    public static void main(String[] args) throws IOException{        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\jietu.JPG"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\study3.JPG"));        int by=0;        while((by=bis.read())!=-1){            bos.write(by);            bos.flush();        }        bis.close();        bos.close();    }}

4)

public class CopyImageDmeo4 {    public static void  main(String[] args) throws IOException{        FileInputStream fis = new FileInputStream("F:\\jietu.JPG") ;        FileOutputStream fos = new FileOutputStream("D:\\study4.JPG") ;        int by=0;        while((by=fis.read())!=-1){            fos.write(by);            fos.flush();        }        fis.close();        fos.close();    }}

复制视频的多种方法:
1)

public class CopyVideoDemo4 {    public static void  main(String[] args) throws IOException{        FileInputStream fis = new FileInputStream("D:\\2017-9-24 11-39-19.ASF") ;        FileOutputStream fos = new FileOutputStream("F:\\2017-9-24 11-39-19.ASF") ;        int by=0;        while((by=fis.read())!=-1){            fos.write(by);            fos.flush();        }        fis.close();        fos.close();    }}

2)

public class CopyVideoDmeo3 {    public static void main(String[] args) throws IOException{        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\2017-9-24 11-39-19.ASF"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\2017-9-24 11-39-19.ASF"));        int by=0;        while((by=bis.read())!=-1){            bos.write(by);            bos.flush();        }        bis.close();        bos.close();    }}

3)

public class CopyVideoDmeo2 {    public static void main(String[] args) throws IOException{        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\2017-9-24 11-39-19.ASF"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\2017-9-24 11-39-19.ASF"));        byte[] bys=new byte[1024];        int len=0;        while((len=bis.read(bys))!=-1){            bos.write(bys,0,len);            bos.flush();        }        bis.close();        bos.close();    }}

4)

public class CopyVideoDmeo1 {    public static void  main(String[] args) throws IOException{        FileInputStream fis = new FileInputStream("D:\\2017-9-24 11-39-19.ASF") ;        FileOutputStream fos = new FileOutputStream("F:\\2017-9-24 11-39-19.ASF") ;        byte[] bys = new byte[1024] ;        int len = 0 ;        while((len=fis.read(bys))!=-1){            fos.write(bys, 0, len) ;        }        fis.close() ;        fos.close() ;    }}

复制音频的多种方法:
1)

public class CopyVoiceDmeo1 {    public static void  main(String[] args) throws IOException{        FileInputStream fis = new FileInputStream("F:\\English.mp3") ;        FileOutputStream fos = new FileOutputStream("D:\\English.mp3") ;        byte[] bys = new byte[1024] ;        int len = 0 ;        while((len=fis.read(bys))!=-1){            fos.write(bys, 0, len) ;        }        fis.close() ;        fos.close() ;    }}

2)

public class CopyVoiceDmeo2 {    public static void main(String[] args) throws IOException{        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\English.mp3"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\English.mp3"));        byte[] bys=new byte[1024];        int len=0;        while((len=bis.read(bys))!=-1){            bos.write(bys,0,len);            bos.flush();        }        bis.close();        bos.close();    }}

3)

public class CopyVoiceDmeo3 {    public static void main(String[] args) throws IOException{        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\English.mp3"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\English.mp3"));        int by=0;        while((by=bis.read())!=-1){            bos.write(by);            bos.flush();        }        bis.close();        bos.close();    }}

4)

public class CopyVoiceDemo4 {    public static void  main(String[] args) throws IOException{        FileInputStream fis = new FileInputStream("F:\\English.mp3") ;        FileOutputStream fos = new FileOutputStream("D:\\English.mp3") ;        int by=0;        while((by=fis.read())!=-1){            fos.write(by);            fos.flush();        }        fis.close();        fos.close();    }}