• 文件操作三


    一、内存操作流:

    ByteArrayOutputStream和ByteArrayInputStream内存操作流,前者是内存向程序输出,后者是程序向内存写入。

    通过内存操作流转换大小写:

    import java.io.ByteArrayInputStream;

    import java.io.ByteArrayOutputStream;

    public class ByteArrayDemo {

       /**

        * @param args

        * @throws Exception 

        */

       public static void main(String[] args) throws Exception {

         // TODO Auto-generated method stub

         String hello="hello world";

         ByteArrayInputStream bis=null;

         ByteArrayOutputStream bos=null;

         bis=new ByteArrayInputStream(hello.getBytes());

         bos=new ByteArrayOutputStream();

         int tmp=0;

         while((tmp=bis.read())!=-1){

            charc=(char)tmp;

            bos.write(Character.toUpperCase(c));

         }

         String s=bos.toString();

         System.out.println(s);

         bis.close();

         bos.close();

       }

    }

    程序通过ByteArrayInputStream来保存字符串,然后再从ByteArrayInputStream读取数据存入ByteArrayOutputStream。

    二、Scanner类:

    方便的完成输入操作,尤其适用于System.in。示例代码如下:

    import java.util.Scanner;

    public class ScannerDemo {

       public static void main(String[] args) {

         // TODO Auto-generated method stub

         Scanner s=new Scanner(System.in);

         int i=0;

         //可以方便的取得各种类型的数据,同时还支持正则表达式,也支持自定义分隔符

         if(s.hasNextInt()){

            i=s.nextInt();

         }

         System.out.println(i);

         s.close();

       }

    }

  • 相关阅读:
    vue中的$event
    vue实现div拖拽互换位置
    关于marquee首次加载出现闪跳问题
    微信小程序之深色模式下样式的写法
    html动态添加公共页头和页脚
    微信小程序改变上一页面的数据,页面中的通信
    mui中的a标签无效和click无法点击的问题
    mui.DtPicker is not a constructor解决方案
    mui-slider选项卡设置默认index
    flutter环境配置遇到的一些坑
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429544.html
Copyright © 2020-2023  润新知