java基础-IO流(转换流)

1、指定字符编码读取

复制代码
package Day10_IO;

import java.io.*;
import java.nio.charset.Charset;

public class IOTest04 {
    public static void main(String[] args) throws IOException {
        //利用转换流按照指定字符编码读取
        //1、创建对象并指定字符编码
//        InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\java\\javase\\basic\\javadown\\test.txt"),"GBK");
//        int len;
//        while((len=isr.read())!=-1){
//            System.out.print((char)len);
//        }
//        isr.close();
        //JDK11之后的替代方案
        FileReader fr = new FileReader("E:\\java\\javase\\basic\\javadown\\test.txt", Charset.forName("GBK"));
        int len;
        while((len=fr.read())!=-1){
            System.out.print((char)len);
        }
        fr.close();
    }
}

2、指定字符编码写出

复制代码
package Day10_IO;

import java.io.*;
import java.nio.charset.Charset;

public class IOTest05 {
    public static void main(String[] args) throws IOException {
        //利用转换流按照指定字符编码写出

//        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\java\\javase\\basic\\javadown\\test1.txt"),"GBK");
//        //写出数据
//        osw.write("你好");
//        osw.close();
        //同样的在JDK11之后,字符输出流也可以指定字符编码了
        FileWriter fw = new FileWriter("E:\\java\\javase\\basic\\javadown\\test2.txt", Charset.forName("GBK"));
        fw.write("字符输出");
        fw.close();
    }
}

3、将本地GBK文件转化为UTF-8,两种方式,JDK11之后会不需要单独new一个字符基本流,也不用在做字符转换了

复制代码
package Day10_IO;

import java.io.*;
import java.nio.charset.Charset;

public class IOTest06 {
    public static void main(String[] args) throws IOException {
        //将本地GBK转化为UTF-8字符编码
        //方式一:
        InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\java\\javase\\basic\\javadown\\test.txt"),"GBK");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\java\\javase\\basic\\javadown\\test3.txt"),"UTF-8");

        int ch;
        while((ch=isr.read())!=-1){
            osw.write((char)ch);
        }
        isr.close();
        osw.close();
        //方式二:JDK11之后的
        FileReader fr = new FileReader("E:\\java\\javase\\basic\\javadown\\test.txt", Charset.forName("GBK"));
        FileWriter fw = new FileWriter("E:\\java\\javase\\basic\\javadown\\test4.txt", Charset.forName("UTF-8"));
        int len;
        while((len=fr.read())!=-1){
            fw.write(len);
        }
        fr.close();
        fw.close();
    }
}
复制代码
4、利用字节流读取文件,每次读取一整行,并且不能乱码
复制代码
package Day10_IO;

import java.io.*;

public class IOTest07 {
    public static void main(String[] args) throws IOException {
        //利用字节流读取文件,每次读取一整行,并且不能乱码
        InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\java\\javase\\basic\\javadown\\test5.txt"));
        BufferedReader br = new BufferedReader(isr);
        String len;
        while((len=br.readLine())!=null){
            System.out.println(len);
        }
        isr.close();
        br.close();
    }
}
相关推荐
2601_962293242 小时前
Python自动化统计团队工作量并生成可视化仪表盘的脚本方案【指导】
python·数据分析·自动化·可视化·仪表盘
点心的游戏开发世界2 小时前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
phltxy2 小时前
C语言操作符详解
java·c语言·算法
2601_962293793 小时前
OCRmyPDF批处理脚本:使用Python自动化复杂OCR任务
python·自动化·批处理脚本·ocrmypdf·ocr任务
步行cgn3 小时前
@Configuration 详解:Spring 配置类的核心注解
java·后端·spring
sunshine22 girl3 小时前
Java学习一 环境配置2 安装和基本使用Idea
java·学习·intellij-idea
事圆则缓4 小时前
Kotlin 协程完全指南
开发语言·kotlin
我爱写代码i4 小时前
BeLink - 支持生成多种URL 缩短网址PHP源码
开发语言·php·短网址php源码
Java后端的Ai之路4 小时前
20、Python - 备忘录模式
开发语言·人工智能·python·外观模式·备忘录模式
嘿嘿-665 小时前
Windows 一键使用 GPT-6 Astra:Codex CLI 配置教程
java·人工智能·windows·gpt·chatgpt·web