• io(字符流)


    关于字节流乱码问题:OutputStreamWriter

    package ioChar;

    import org.junit.Test;

    import javax.annotation.processing.Filer;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;

    public class CharStream {
    //字符流
    @Test
    public void charS() throws Exception {
    //建立输出流
    FileWriter fileWriter = new FileWriter("h:\java.txt");
    //写入数据
    fileWriter.write("this is ");
    //关闭连接
    fileWriter.close();

    //BufferedWriter
    //建立输出流
    FileWriter fileWriter1 = new FileWriter("h:\java.txt");
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter1);
    //写数据
    bufferedWriter.write("thiidllda daadas kk");
    //关闭连接
    bufferedWriter.close();
    fileWriter.close();


    //FileReader
    //建立输入流
    FileReader fileReader = new FileReader("h:\java.txt");
    //read data
    // while ( (fileReader.read())!=-1){
    // System.out.println((char)fileReader.read());
    // }

    //BufferedReader
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String line = null;
    while ((line = (bufferedReader.readLine())) != null) {
    System.out.println(line);//整行读取
    }
    bufferedReader.close();
    fileReader.close();
    }
    }

    package com.zrm.io;

    import org.junit.Test;

    import java.io.*;

    public class ByteLuan {
    //处理字节流乱码问题
    @Test
    public void doLuanMa() throws IOException {
    //创建流
    FileOutputStream fio = new FileOutputStream("h:\java.txt", true);
    OutputStreamWriter osw = new OutputStreamWriter(fio, "utf-8");
    //写数据
    osw.write("java is the best ");
    osw.write("中国是最强大的");
    //关闭流
    osw.close();
    fio.close();

    //读取数据
    //创建流
    FileInputStream fis = new FileInputStream("h:\java.txt");
    InputStreamReader isr = new InputStreamReader(fis, "utf-8");
    //read
    char[] car = new char[1024];
    int len = 0;
    while ((len = isr.read(car)) != -1) {
    System.out.println(len);
    }
    //关闭流
    isr.close();
    fis.close();
    }
    }
  • 相关阅读:
    文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"
    web.xml配置参数context-param和init-param的区别
    web.xml中通过contextConfigLocation的读取spring的配置文件
    Web.xml配置详解之context-param
    xml 文件 和xsd 文件的关系
    事务并发控制
    Java泛型详解
    MongoDB集群
    MongoDB集群——分片
    MongoDB集群——副本集
  • 原文地址:https://www.cnblogs.com/zrmj/p/11427428.html
Copyright © 2020-2023  润新知