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

       }

    }

  • 相关阅读:
    垃圾回收app冲击(一)
    本篇看似是一篇拍马屁的博客!但是真情实感!
    搜索建议
    冲击团队项目需求分析
    疫情分析冲击(五)
    疫情分析冲击(四)
    疫情分析冲击(三)
    疫情分析冲击(二)
    垃圾分类网站
    Android学习——singleInstance
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429544.html
Copyright © 2011-2022 走看看