初识java的几种io流

最近刚刚学习了java的io流,今天抽空把io流里面的BufferedReader,BufferedWriter,FileInputStream,FileOutputStream,PrintWriter的基本读写方法总结了一趟

package com.lh.iostream;

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.PrintWriter;

public class iostream_text {
    /*
     * 用打印流PrintWriter來写入数据
     * 
     * */
    public  void printstream(StringBuffer str) throws IOException
    {
        String content = String.valueOf(str);
        File file = new File("src/lab02.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        PrintWriter prw = new PrintWriter(fw);
        long start = System.currentTimeMillis();
        prw.write(content); 
        prw.flush(); // 因为数据读取的时候,一般都是先将数据读取到内存里面,然后再是写入到文本里面的,
        // 当数据读取完毕的时候并不意味着内容就完全写入到了文本里面了
        // 此时文本内容还有可能会留在了缓存区里面,因此我们需要对缓存里面的数据进行flush();
        System.out.println("---------printwriter is ok!---------");
        long end = System.currentTimeMillis();
        long total = end - start;
        System.out.println("the total time is:" + total + "ms");
        prw.close();
        fw.close();
    }

    /*
     * 用BufferedReader来写入数据
     * 
     * */
    public void bufferedwriter(StringBuffer str) throws IOException
    {
        String content = String.valueOf(str);
        File file = new File("src/lab03.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bfw = new BufferedWriter(fw);
        long start = System.currentTimeMillis();
        bfw.write(content); 
        bfw.flush(); // 因为数据读取的时候,一般都是先将数据读取到内存里面,然后再是写入到文本里面的,
        // 当数据读取完毕的时候并不意味着内容就完全写入到了文本里面了
        // 此时文本内容还有可能会留在了缓存区里面,因此我们需要对缓存里面的数据进行flush();
        System.out.println("---------BufferedReader is ok!---------");
        long end = System.currentTimeMillis();
        long total = end - start;
        System.out.println("the total time is:" + total + "ms");
        bfw.close();
        fw.close();
    }

    /*
     * 用打印流fileoutputwriter來写入数据
     * 
     * */
    public  void fileoutputwriter(StringBuffer str) throws IOException
    {
        String content = String.valueOf(str);
        File file = new File("src/lab04.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        byte Contentbyte[]=content.getBytes();
        FileOutputStream out=new FileOutputStream(file);
        long start = System.currentTimeMillis();
        out.write(Contentbyte,0,Contentbyte.length);
        System.out.println("----------FileOutputStream is ok!---------");
        long end = System.currentTimeMillis();
        long total = end - start;
        System.out.println("the total time is:" + total + "ms");
        out.close();
    }


    public void bufferedreader(File file) throws IOException
    {

        BufferedReader bfr=new BufferedReader(new FileReader(file));
        String content=null;
        long start = System.currentTimeMillis();
        while(bfr.readLine() != null)
        {
            content=content+(bfr.readLine()+'\n');
        }
        System.out.println("----------BufferedReader is ok!---------");
        long end = System.currentTimeMillis();
        long total = end - start;
        System.out.println("the total time is:" + total + "ms");
        bfr.close();

    }

    public void fileinputstream(File file) throws IOException
    {

        FileInputStream in=new FileInputStream(file);
        long start = System.currentTimeMillis();
        byte Content_byte[]=new byte[(int) file.length()];
        in.read(Content_byte);
        System.out.println("----------fileinputstream is ok!---------");
        long end = System.currentTimeMillis();
        long total = end - start;
        System.out.println("the total time is:" + total + "ms");
        in.close();

    }



    public static void main(String[] sda) throws IOException {
        iostream_text io=new iostream_text();
        StringBuffer str_text=new StringBuffer();
        for (int i = 0; i < 4000; i++) {  
            str_text = str_text.append("s");  //str_text所能容纳的字符串长度和jvm自身的内存有关
        }
        File file =new File("src/lab01.txt");
        io.printstream(str_text);
        io.bufferedwriter(str_text);
        io.bufferedreader(file);
        io.fileoutputwriter(str_text);
        io.fileinputstream(file);

    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值