文件操作三

一、内存操作流:

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

   }

}

原文地址:https://www.cnblogs.com/itfenqing/p/4429544.html