【关于Java的IO的学习】

可以从其中读入一个字节序列的对象称为:输入流

  可以从其中写入一个字节序列的对象称为:输出流

  字节的来源以及目的地可以是文件也可以是网络连接或者内存块

  抽象类inputstream和outputstream是面向字节的输入\输出类

  而从reader和writer抽象类继承出来的是面向双字节的unicode字符;

  Inputstream基本方法:

  Abstract void Read()读取一个字节并返回,遇到结尾是输出-1;

  Int avialable()查看流中还可用的字节数

  Void close()关闭当前流

  Outputstream基本方法:

  Abstract void write()写一个字节到流对象中

  Void close()关闭流,关闭的同时会将流中的内容送出;

  Void flush()输出并清空流

  Read和write在执行的时候是阻塞的;

  PushBackStream的用法:

  package org.xzh.in;

  import java.io.FileInputStream;

  import java.io.IOException;

  import java.io.PushbackInputStream;

  publicclass in {

  publicstaticvoid main(String[] args){

  String fileName="aa.txt";

  int c=0;

  int cc=0;

  try{

  PushbackInputStream s=new PushbackInputStream(new FileInputStream(fileName));

  System.out.println(s);

  while((c=s.read())!=-1){

  if(c=='a'){

  if((cc=s.read())== 's'){

  System.out.println("daiti");

  }else{

  s.unread(cc);

  System.out.println('a');

  }

  }else{

  System.out.println((char)c);

  }

  s.close();

  }catch(IOException e){

  e.printStackTrace();

  }

  }

  }

  这个类用于多字符匹配用;

  如果要以文本格式输入数据,使用的是SCANNER,下面是实例程序:

  Scanner s = new Scanner(System.in);

  //receive string

  String str = s.next();

  //receive integer

  Integer i = s.nextInt();

  //receive double

  Double d = s.nextDouble();

  System.out.println(str+i+d);更多精彩教程请关注:win7旗舰版系统下载

posted on 2013-08-02 09:32  挖掘者者者  阅读(207)  评论(0编辑  收藏  举报