zoukankan      html  css  js  c++  java
  • 文件操作三

    一、内存操作流:

    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();

       }

    }

  • 相关阅读:
    [LeetCode] 56. Merge Intervals 解题思路
    [LeetCode] 128. Longest Consecutive Sequence 解题思路
    [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法
    linux安装PHP7以及扩展
    php安装composer
    细说PHP中strlen和mb_strlen的区别
    mysql一些简单操作
    mysql数据库使用Navicat时向Navicat导入sql文件时某字段过大时的处理
    JS中||的某些用法
    PHP验证身份信息
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429544.html
Copyright © 2011-2022 走看看