黑马程序员_IO输入输出补充

---------------------- android培训、java培训、期待与您交流! ----------------------

new RandomAccessFile(f,"rw");(读写);new RandomAccessFile(f,"r");(只读)

 

randomaccessfile类提供了类似随机访问的方式,但该类只能操作文件,不能访问其他的IO设备,如网络,内存映像等。

 

实例:

import java.io.*;
class Employee
{
 int age;
 String name;
 
 public Employee(String name, int age)
 {
 final int LEN=8; 
  if (name.length()>LEN)
   name=name.substring(0,LEN);
  else
  {
   while(name.length()<LEN)
    name+='\u0000';
  }
  this.name=name;
  this.age=age;
 }
}
class testio
{
 public static void main(String [] args) throws Exception
 {
  Employee e1=new Employee("e1",1);
  Employee e2=new Employee("e2",2);
  Employee e3=new Employee("e3",3);
  RandomAccessFile ra=new RandomAccessFile("employee.txt","rw");
  ra.write(e1.name.getBytes());
  ra.writeInt(e1.age);
  ra.write(e2.name.getBytes());
  ra.writeInt(e2.age);
  ra.write(e3.name.getBytes());
  ra.writeInt(e3.age);
  ra.close();
  ra=new RandomAccessFile("employee.txt","r");
  int len=8;
  ra.skipBytes(12);
  System.out.println("the second employee:");
  String str="";
  for(int i=0;i<len;i++)
   str=str+(char)ra.readByte();
  System.out.println(str);
  System.out.println(ra.readInt());
  System.out.println("the fist employee:");
  ra.seek(0);
  str="";
  for(int i=0;i<len;i++)
   str=str+(char)ra.readByte();
  System.out.println(str);
  System.out.println(ra.readInt());
  ra.seek(24);
  str="";
  for(int i=0;i<len;i++)
   str=str+(char)ra.readByte();
  System.out.println(str);
  System.out.println(ra.readInt());
  ra.close();
 }
}

 

流是字节序列的抽象概念

 

文件是数据的具体静态形式,而流是数据传输时的状态

 

InputStream描述了所有输入流的抽象状态

 

inputstream的方法:

int read();int read(byte[]);int read(byte[],int offset,int len);long skip(long n)(用于包装类);int available()(检查输入流有多少可读的数据);void mark(int readlimit)参数的意思是从标记起最多还能读多少字节(包装类);void reset()(配合mark方法,比如我从A开始标记,读了几个字节,然后reset一下,又可以从A读起);boolean markSupported()返回当前的流是否支持mark和reset操作;void close();

 

OutputStream描述了所有输入流的抽象状态

 

void write(int b);void write(byte []);void write(byte[],int offset,int len);void flush();void close();

 

 FileInputStream和FileOutputStream用来创建磁盘文件输入流和输出流的对象,通过它们的构造函数来指定文件路径和文件名,前者操作的对象必须是存在且可读的,没有被其他程序打开,后者操作的对象如果已有,内容会被覆盖清除

 

后者在创建输入流之前先容许file类对文件做一系列的检验,会更安全

 

实例:

import java.io.*;
class testio
{
 public static void main(String [] args) throws Exception
 {
  File f =new File("hello world!.txtj");
  FileOutputStream out=new FileOutputStream(f);
  out.write("weijingning".getBytes());
  out.close();
  FileInputStream in=new FileInputStream(f);
  byte[] buf=new byte[1024] ;
  int len=in.read(buf);
  System.out.println(new String(buf,0,len));
 } 
}

Reader和Writer是所有字符流类的抽象基类,用于简化字符串的相关编程,即用于读写文本文件

实例:(将一个文件copy到别处)

import java.io.*;
class testio
{
  public static void main(String [] args)
  {
    try
    {
    File f1=new File("in.txt");//将与源文件同目录的文件copy另一份
    File f2=new File("out.txt");
    int length=2097152;//用2m的byte数组作为缓存
    FileInputStream in=new FileInputStream(f1);//创建输入流,输出流
    FileOutputStream out=new FileOutputStream(f2);
    byte[] buffer=new byte[length];
    int ins=0;
    while((ins=in.read(buffer))!=-1)//若未到文件结尾,则将缓存的内容存入到输出流中
    {
         out.write(buffer,0,ins);
   }
    in.close();//文件copy完成,关闭流
     out.close();
    }
    catch (Exception e)
    {
     e.printStackTrace();
    }
  }
}

 实例:(管道通信)

import java.io.*;
class pipestreamtest
{
 public static void main(String [] args)
 {
  try
  {
  Thread t1=new Sender();
  Thread t2=new Receiver();
  PipedOutputStream out=((Sender)t1).getOutputStream();
  PipedInputStream in=((Receiver)t2).getInputStream();
  out.connect(in);
  t1.start();
  t2.start();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  
 }
}
class Sender extends Thread
{
 private PipedOutputStream out=new PipedOutputStream();
 public PipedOutputStream getOutputStream()
 {
  return out;
 }
 public void run()
 {
  String s= new String("hello,I'm sender!");
  try
  {
   out.write(s.getBytes());
   out.close();
  }
  catch(IOException e)
  {
   e.printStackTrace();
  }
 }
}
class Receiver extends Thread
{
 private PipedInputStream in=new PipedInputStream();
 public PipedInputStream getInputStream()
 {
  return in;
 }
 public void run()
 {
  String s=null;
  byte[] buf= new byte[1024];
  try
  {
  int len=in.read(buf);
  s=new String(buf,0,len);
  System.out.println("from sender:"+s);
  in.close();
  }
  catch(Exception e)
  {e.printStackTrace();}
 }
}


---------------------- android培训、java培训、期待与您交流! ----------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值