I/O流复制文本

Posted sixgodbiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了I/O流复制文本相关的知识,希望对你有一定的参考价值。

package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class IO流 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        bufferedWR();

    }

    private static void show(File file) {
        File[] fileList = file.listFiles();
        for (File f : fileList) {
            if (f.isFile()) {
                if (f.getName().endsWith(".txt"))
                    System.out.println(f.getName());
            } else {
                show(f);
            }
        }
    }

    private static void delete(File file) {
        File[] fileList = file.listFiles();
        for (File f : fileList) {
            if (f.isFile()) {
                f.delete();
            } else {
                delete(f);
            }
        }
        file.delete();
    }

    /**
     * 抽象类 字节流 读取 InputStream 字节流 输出 OutputStream
     * 
     * 字符流 读取 Reader 字符流 输出 Writer
     * 
     * 实现类 BateArrayOutputStream FileOutputStream OutputStream FileOutputStream
     * 构造方法 FileOutputStream(File file) FileOutputStream(String path)
     * 
     * @throws
     */

    // 字符串写入文件
    /**
     * 追加写入 FileOutputStream fos = new FileOutputStream("a.txt",true); 换行 

     */
    public static void strToFile() {
        // 创建字节输出流对象
        String str = "hello";
        try {
            // File file = new File("D:\a.txt");
            // FileOutputStream fs = new FileOutputStream(file);
            FileOutputStream fos = new FileOutputStream("a.txt", true);
            fos.write("hello 
hello".getBytes());

            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取文件内容,输出到控制台
     */
    public static void readFromFile() {
        File file = new File("a.txt");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] bys = new byte[1024];
            int len = 0;
            while ((len = fis.read()) != -1) {
                System.out.print((char) len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 复制文本文件 从a.txt 到 b.txt
     * 
     */
    public static void copyByte() {
        try {
            FileInputStream is = new FileInputStream("a.txt");
            FileOutputStream os = new FileOutputStream("b.txt");
            int len = 0;
            byte[] bys = new byte[1024];
            while ((len = is.read(bys)) != -1) {
                os.write(bys, 0, len);
            }
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    /**
     * BufferedOutputStream 缓冲输出
     */
    public static void bufferedOutputDemo() {
        try {
            FileOutputStream fos = new FileOutputStream("a.txt");
            FileInputStream fis = new FileInputStream("b.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            BufferedInputStream bis = new BufferedInputStream(fis);
            int len = 0;
            byte[] bys = new byte[1024];
            while ((len = bis.read(bys)) != -1) {
                bos.write(bys, 0, len);
            }
            bos.close();
            fos.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    /**
     * OutputStreamWriter(file,character) 是转换流 字节+编码方式
     * 
     * InputStreamWriter(file,character) 是转换流 字节+编码方式
     * 
     * flush()方法 刷新缓冲
     */

    /**
     * 字符流复制文件
     */
    public static void copyZiFULiu() {
        try {
            OutputStreamWriter osw = new OutputStreamWriter(
                    new FileOutputStream("a.txt"));
            InputStreamReader oiw = new InputStreamReader(new FileInputStream(
                    "c.txt"));
            int len = 0;
            char[] b = new char[1024];
            while ((len = oiw.read(b)) != -1) {
                osw.write(b, 0, len);
                osw.flush();
            }
            osw.close();
            oiw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * FileWriter = FileOutputStream + 编码 FileReader = FileInputStream + 编码
     */
    public static void copyRW() {
        try {
            FileReader reader = new FileReader("a.txt");
            FileWriter writer = new FileWriter("d.txt");
            int len = 0;
            char[] c = new char[1024];
            while ((len = reader.read(c)) != -1) {
                writer.write(c, 0, len);
                writer.flush();

            }
            writer.close();
            reader.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * BufferedWriter(Writer out)
     * BufferedReader(Reader read)
     * 字符缓冲流的特殊方法:
     *                 newLine():根据系统来决定换行符
     *                 readLine(): 一次读取一行数据
     */
    public static void bufferedWR(){
        try {
//            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e.txt")));
            BufferedWriter bw = new BufferedWriter(new FileWriter("e.txt"));
//            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
            BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//            int len = 0;
            String s = null;
            char[] c = new char[1024];
            while((s= (br.readLine()))!= null){
//                bw.write(c,0,len);
//                bw.flush();
                bw.write(s);
                bw.newLine();
                bw.flush();
            }
            bw.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}

 

以上是关于I/O流复制文本的主要内容,如果未能解决你的问题,请参考以下文章

第十八章 Java I/O流

Java I/O系列

使用I/O流复制指定文件

使用I/O流复制指定文件

JAVA基础复习-FileRead与FileWriter结合使用示例:字符输入输出流整合(实现文本类文件的复制)

文件I/O操作