java的转换流,打印流,数据流

InputStreamReader(字符输入转换流)

解决不同编码,字符流读取文本内容乱码的问题

public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException

Creates an InputStreamReader that uses the named charset.

把原始的字节输入流(UTF-8),按照指定字符集编码转成字符输入流。

public class test1 {
    public static void main(String[] args) {
        try(
                //1:得到(GBK)文件的原始字节流
                InputStream f1=new FileInputStream("day17-file-app\\src\\hhh4.txt");
                //2:把原始的字节流按照指定的字符集编码转换成字符输入流,默认是UTF-8,要变成GBK
                Reader reader=new InputStreamReader(f1,"GBK");
                //3:把字符输入流包装成缓冲字符输入流
                BufferedReader bufferedInputStream=new BufferedReader(reader);
                )
        {
            String line;
            while((line=bufferedInputStream.readLine())!=null)
            {
                System.out.println(line);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

需要控制写出去的字符使用特定的字符集编码:


1:调用String提供的getBytes方法解决

String data="你好呀";

byte[]bytes=data.getBytes("GBK"); 

 2:使用“字符输出转换流”

OutputStreamWriter字符输出转换流

作用:可以控制写出去的字符使用说明字符集编码

public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException

Creates an OutputStreamWriter that uses the named charset.

可以把原始的字节输出流,按照指定的编码转换成字符输出流

public class test {
    public static void main(String[] args) {
        try(
                //创建一个默认的字节输出流
        OutputStream out=new FileOutputStream("day17-file-app\\src\\hhh5.txt");
        //把原始的字节输出流,转换成指定字符编码集的字符输出流
        Writer writer=new OutputStreamWriter(out,"GBK");
        //包装
        BufferedWriter Bwriter=new BufferedWriter(writer);
        )
        {
            Bwriter.write("你好呀");
            Bwriter.newLine();
            Bwriter.write("aabbcc");

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

PrintStream/PrintWriter(打印流)

作用:打印流可以实现更方便,更高效的打印数据出去 

public PrintStream(File/OutputStream/String) throws FileNotFoundException 

打印流直接通向字节输出流/文件/文件路径 

public PrintStream(String fileName, Charset charset) throws IOException

可以指定写出去的字符集编码 

public PrintStream(OutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException

可以指定实现自动刷新,并可以指定字符的编码 

 public void println()

打印任何类型的数据

public void write(byte[] buf) throws IOException

可以指定写字节数据出去 

public class test1 {
    public static void main(String[] args) {
        try(
                //创建一个打印流管道
                PrintStream printStream=new PrintStream("day17-file-app\\src\\hhh6.txt", Charset.forName("GBK"));
                )
        {
            printStream.println(1);
            printStream.println("你好");
            printStream.println(true);
            printStream.println('a');

            printStream.write(97);//a
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

 PrintWriter和PrintStream差不多

如何追加数据

PrintStream printStream=new PrintStream(new FileOutputStream("\"day17-file-app\\\\src\\\\hhh6.txt",true));

先包装成普通的字节输出流

PrintStream继承字节输出流OutputStream,因此支持写字节数据出去

PrintWriter继承字符输出流Writer,因此支持写字符数据出去 

打印流的应用:输出语句的重定向

public class test2 {
    public static void main(String[] args) {
        try(
                PrintStream printStream=new PrintStream("day17-file-app\\src\\hhh7.txt");
                )
        {
            System.setOut(printStream);//把系统的默认打印流对象改成自己设置的打印流
            System.out.println("hello");
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

 数据输出流(DataOutputStream)

允许把数据和其数据类型一并写出去

public DataOutputStream(OutputStream out)

Creates a new data output stream to write data to the specified underlying output stream. The counter written is set to zero.

创建新数据输出流包装基础的字节输出流

public final void writeByte(int v) throws IOException

将byte类型的数据写入基础的字节输出流 

public final void writeInt(int v) throws IOException

 将int类型的数据写入基础的字节输出流 

public final void writeDouble(double v) throws IOException

  将double类型的数据写入基础的字节输出流 

public final void writeUTF(String str) throws IOException

将字符串数据以UTF-8编码成字节写入基础的字节输出流 

public void write(byte[] b, int off, int len) throws IOException

支持写字节数据出去 

public class test1 {
    public static void main(String[] args) {
        try(
                DataOutputStream dataOutputStream=new DataOutputStream(new FileOutputStream("day17-file-app\\src\\hhh8.txt"));
                )
        {
            dataOutputStream.writeChar('c');
            dataOutputStream.writeDouble(3.2);
            dataOutputStream.writeBoolean(true);
            dataOutputStream.writeUTF("hello");
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
结果:

数据输入流(DataInputStream)

用于读取数据输出流写出去的数据

public DataInputStream(InputStream in)

创建新数据输入流包装的基础的字节输入流 

public final byte readByte() throws IOException

读取字节数据并返回 

public final int readUnsignedShort() throws IOException

读取int类型的数据返回 

public final String readUTF() throws IOException

读取字符串数据(UTF-8)返回 

 

public class test2 {
    public static void main(String[] args) {
        try(
                DataInputStream dataInputStream=new DataInputStream(new FileInputStream("day17-file-app\\src\\hhh8.txt"));
                )
        {
//按照顺序读
            System.out.println(dataInputStream.readChar());//c
            System.out.println(dataInputStream.readDouble());//3.2
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

  • 33
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落落落sss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值