码迷,mamicode.com
首页 > 其他好文 > 详细

I/O流复制文本

时间:2018-10-23 14:55:08      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:path   rate   文本文件   import   sys   let   col   字节流   buffer   

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); 换行 \r\n
     */
    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 \r\nhello".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流复制文本

标签:path   rate   文本文件   import   sys   let   col   字节流   buffer   

原文地址:https://www.cnblogs.com/sixgodbiao/p/9835943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!