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

       }

    }

  • 相关阅读:
    温故知新 将Date和String类型相互转换
    温故知新 线程
    温故知新 数组
    温故知新 集合
    温故知新 流(字节流与字符流)
    温故知新 jdbc 数据库调取封装
    Reds 持久化 高并发 高可用
    批量修改文件后缀名
    scala之旅-核心语言特性【高阶函数】(十)
    scala之旅-核心语言特性【使用混入组合类】(九)
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429544.html
Copyright © 2011-2022 走看看