• java转换流


      转换流是把字节流转换成字符流,比如往一个文件中写内容,原本是一个字节一个字节的写,转换为字符流后,我们可以一个字符串,一个字符串的写,书写中文很方便

      转换流class: OutputStreamWriter,InputStreamReader,需要和OutputStream/inputStream套接,并且在构造是可以指定其编码

     1 import java.io.File;
     2 import java.io.FileOutputStream;
     3 import java.io.IOException;
     4 import java.io.OutputStreamWriter;
     5 
     6 
     7 public class TestTransForm1 {
     8 
     9     /**
    10      * @param args
    11      * @throws IOException 
    12      */
    13     public static void main(String[] args) throws IOException {
    14         
    15         String path="D:"+File.separator+"trans.txt";
    16         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path,true),"ISO8859_1");
    17         osw.write("test");
    18         osw.flush();
    19         System.out.println(osw.getEncoding());
    20         osw.close();
    21         
    22     }
    23 
    24 }

    输出结果:ISO8859_1

    trans.txt中的内容:test

     1 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 
     5 
     6 public class TestTransForm2 {
     7 
     8     /**
     9      * @param args
    10      * @throws IOException 
    11      */
    12     public static void main(String[] args) throws IOException {
    13         InputStreamReader isr=new InputStreamReader(System.in);
    14         BufferedReader br=new BufferedReader(isr);
    15         String s=null;
    16         s=br.readLine();
    17         while(s!=null){
    18             if(s.equalsIgnoreCase("exit")){
    19                 break;
    20             }
    21             
    22             System.out.println(s.toUpperCase());
    23             s=br.readLine();//将s重新指向键盘输入
    24         }
    25         isr.close();
    26         br.close();
    27     }
    28 
    29 }

    输出结果:

  • 相关阅读:
    游戏的真实度
    91)Prometheus简单入门
    90)ubuntu 内核升级/降级系统内核(4.13.0)
    89)yaml文件语法
    使用格式工厂,把视频转为gif,画质受损,出现彩虹条模糊的情况
    fastadmin管理后台--带个人日程管理功能
    http响应--禁用缓存设置
    HTTP响应-302,304码的运用
    http请求详解 防盗链技术
    php支付宝支付接口开发(教程笔记)
  • 原文地址:https://www.cnblogs.com/andong2015/p/4249199.html
Copyright © 2020-2023  润新知