java字符流

字符流

缓冲字符输出流

package FB;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class ZFOut {
    public static void main(String[] args) {
        try {
            Writer w = new FileWriter("E:");

            BufferedWriter bw = new BufferedWriter(w);

            bw.write("asjkfkahs");

            bw.close();

            w.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

缓冲字符输入流

public class BufferedReaderPractice {
    public static void main(String[] args) {
        try {
            // 文件输入流
            Reader reader = new FileReader("");

            // 不希望直接操作文件输入流,为了提升效率,使用缓冲输入流
            BufferedReader br = new BufferedReader(reader);

            // 缓冲输入流,比字符输入流,多出一个readLine方法,能够一次读一行
            // 并且读到的是String类型,而不是char[]类型
            String line = br.readLine();
            System.out.println(line);

            // 关闭资源
            br.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

打印流

package FB;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class PrintIO {
    public static void main(String[] args) {
        try {
            File f = new File("E:");
            PrintWriter pw = new PrintWriter(f);

            pw.print("alkhsfliAH");
            pw.println("5614646");
            pw.println("hakHFfwn");

            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

输出转换流

能够将字节流,转换成字符流

package FB;

import java.io.*;

public class Inputst {
    public static void main(String[] args) {
        try {
            OutputStream os = new FileOutputStream("E:);
            OutputStreamWriter osw = new OutputStreamWriter(os);

            osw.write("张三");


            osw.close();
            os.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输入转换流

能够将字节流,转换成字符流

package FB;

import java.io.*;

public class OutputSt {
    public static void main(String[] args) {
        try {
            InputStream is = new FileInputStream("E:");
            InputStreamReader isr = new InputStreamReader(is);

            StringBuilder sb = new StringBuilder();
            char[] c = new char[512];

            while (true){
                int lin = isr.read(c);
                if(lin==-1){
                    break;
                }
                sb.append(c,0,lin);
            }
            System.out.println(sb);
            isr.close();
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Properties对象

  • load(输入流)

    加载输入流里面的配置,一般为properties文件
    这种文件的格式为: 键=值[换行]

本质上,与Map没有区别
使用场景上,在读配置文件时使用

package FB;

import java.io.*;
import java.util.Properties;

public class Peres {
    public static void main(String[] args) {
        try{
            InputStream is = new FileInputStream("E:");

            Properties p = new Properties();

            p.load(is);

            String username = p.getProperty("username");
            String password = p.getProperty("password");
            String monld = p.getProperty("monld","未知");
            System.out.println(username);
            System.out.println(password);
            System.out.println(monld);

            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

总结

类型

  • InputStream

  • OutputStream

  • FileInputStream

  • FileOutputStream

  • BufferedInputStream

  • BufferedOutputStream

  • ObjectInputStream

  • ObjectOutputStream

  • Serializeble

  • Reader

  • Writer

  • FileReader

  • FileWriter

  • BufferedReader

  • BufferedWriter

  • PrintWriter

  • InputStreamReader

  • OutputStreamWriter

  • File

  • FileFilter

  • Properties

单词

Input
Output
Stream
Read
Write
Buffer
File
Serializable
Filter
Properties

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值