Java中IO流的使用例子

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

 

class Person implements Serializable{
 int age;
 String name;
 transient String  password;
 public Person(int age, String name, String password) {
  super();
  this.age = age;
  this.name = name;
  this.password = password;
 }
}

 

public class TestInputStream {


 public static void Input(){
  try {
   FileInputStream fis=new FileInputStream("d://1//1.txt");
   int i=0;
   /*while((i=fis.read())!=-1)
   {
    System.out.print((char)i);
   }*/
   
   byte[] bt=new byte[130];
   fis.read(bt);
   System.out.println(new String(bt,"GBK").trim());
   fis.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void Output(){
  try {
   FileOutputStream fis=new FileOutputStream("d://1//2.txt");
   fis.write("我爱大连理工".getBytes());
   fis.flush();
   fis.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void FileReaderTest(){
  try {
   FileReader fr=new FileReader("d://1//1.txt");
   int c;
   /*while((c=fr.read())!=-1)
   {
    System.out.print((char)c);
   }*/
   char[] ch=new char[100];
   fr.read(ch);
   System.out.println(new String(ch).trim());
   fr.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void FileWriterTest(){
  try {
   FileWriter fw=new FileWriter("d://1//2.txt");
   fw.write("我爱大连理工大uxe");
   fw.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void bufferedReaderTest(){
  try {
   BufferedReader br=new BufferedReader(new FileReader("d://1//1.txt"));
   String str="";
   while((str=br.readLine())!=null)
   {
    System.out.println(str);
   }
   br.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void bufferedWriterTest(){
  try {
   BufferedWriter br=new BufferedWriter(new FileWriter("d://1//2.txt"));
   br.write("我爱大连理工大uxec");
   br.write(123);
   br.write("大量咯规范化".toCharArray());
   br.flush();
   br.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void InputStreamReader(){
  try {
   InputStreamReader isr=new InputStreamReader(new FileInputStream("d://1//1.txt"));
   int i=0;
   /*while((i=isr.read())!=-1)
   {
    System.out.print((char)i);
   }*/
   
   char[] ch=new char[40];
   isr.read(ch);
   System.out.println(ch);
   isr.close();
   
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void OutputStreamWriter(){
  try {
   OutputStreamWriter isw=new OutputStreamWriter(new FileOutputStream("d://1//2.txt"));
   isw.write("使得法国就回家后即可大富豪国家会计科");
   isw.write("safhghgj".toCharArray());
   isw.flush();
   isw.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void DataoutputStreamTest(){
  try {
   DataOutputStream dos=new DataOutputStream(new FileOutputStream("d://1//1.txt"));
   dos.writeUTF("我爱大连理工大uxe");
   dos.writeDouble(12345);
   dos.flush();
   dos.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void DataInputStreamTest(){
  try {
   DataInputStream dis=new DataInputStream(new FileInputStream("d://1//1.txt"));
   String str=dis.readUTF();
   double d=dis.readDouble();
   System.out.println(str);
   System.out.println("double"+d);
   dis.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void ByteArrayStream(){
  byte[] bt="大连理工大学".getBytes();
  ByteArrayOutputStream bos=new ByteArrayOutputStream();
  DataOutputStream dos=new DataOutputStream(bos);
  try {
   dos.writeUTF("大连理工度邪恶");
   dos.writeLong(23145L);
   dos.flush();
   dos.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //如下读取会生成错误
  /*byte[] b=bos.toByteArray();
  try {
   System.out.println(new String(b,"GBK"));
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }*/
  ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
  DataInputStream dis=new DataInputStream(bis);
  try {
   String str=dis.readUTF();
   long l=dis.readLong();
   System.out.println(str);
   System.out.println(l);
   dis.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void ObjectOutputStreamTest(){
  try {
   ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("d://1//2.txt"));
   Person p=new Person(4,"we","ertr");
   oos.writeObject(p);
   Person p1=new Person(34,"dfhghgfhgfhj","fhgkjhlkjp;lk;lk;'");
   oos.writeObject(p1);
   oos.flush();
   oos.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void ObjectInputStreamTest(){
  try {
   ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d://1//2.txt"));
   Person p=(Person)ois.readObject();
   Person p2=(Person)ois.readObject();
   String str1=p.name;
   String str2=p2.name;
   /**
    * 由于password设置为transient,就是不能序列化,不会被存储,也不能被读取
    */
   String pass1=p.password;
   String pass2=p2.password;
   System.out.println("用户1:"+str1+"密码"+pass1);
   System.out.println("用户2:"+str2+"密码"+pass2);
   ois.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public static void systemInputStreamReader(){
  //使用FileInputStream不行
  InputStreamReader fis=new InputStreamReader(System.in);
  int i=0;
  try {
  /* while((i=fis.read())!=-1)
   {
    System.out.print((char)i);
   }*/
   char[] ch=new char[35];
   //显示多余数据
   fis.read(ch);
   System.out.print(ch);
   fis.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void systemInputStream(){
  //使用FileInputStream不行
  InputStream is=System.in;
   byte[] bt=new byte[35];
   try {
    is.read(bt);
    System.out.println(new String(bt).trim());
    is.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 }
 
 public static void systemOutputStream(){
  //使用FileInputStream不行
  OutputStream os=System.out;
   
   try {
    os.write("按时打发过来".getBytes());
    os.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 }
 
 public static void testshurushuchuzhuanghuan(){
  try {
   PrintStream console=System.out;
   FileInputStream fis=new FileInputStream("d://1//1.txt");
   System.setIn(fis);
   FileOutputStream fos=new FileOutputStream("d://1//2.txt");
   PrintStream ps=new PrintStream(fos);
   System.setOut(ps);
   System.setErr(ps);
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   String str="";
   while((str=br.readLine())!=null)
   {
    System.out.println(str);
   }
   System.setOut(console);
   br.close();
   fis.close();
   fos.flush();
   fos.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void RandomAccessFileOutTest(){
  try {
   RandomAccessFile raf=new RandomAccessFile(new File("d://1//1.txt"),"rw");
   try {
    raf.writeBoolean(true);
    raf.writeDouble(234);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   raf.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void RandomAccessFileInTest(){
  try {
   RandomAccessFile raf=new RandomAccessFile(new File("d://1//1.txt"),"rw");
   try {
    /**
     * Boolean占一个字节,向前跳1个字节
     */
    raf.seek(1);
    //boolean t=raf.readBoolean();
    double d=raf.readDouble();
    System.out.println("double"+d);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   raf.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void FileChanelOutTest(){
  try {
   FileChannel fc=new FileOutputStream("d://1//1.txt").getChannel();
   fc.write(ByteBuffer.wrap("大连理工大学李宁".getBytes()));
   fc.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void FileChanelGBKTest(){
  try {
   FileChannel fc=new FileOutputStream("d://1//1.txt").getChannel();
   fc.write(ByteBuffer.wrap("大连理工大学李宁".getBytes("UTF-16BE")));
   fc.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void FileChanelInTest(){
  ByteBuffer bf=ByteBuffer.allocate(40);
  try {
   FileChannel fc=new FileInputStream("d://1//1.txt").getChannel();
   fc.read(bf);
   bf.flip();
   /*while(bf.hasRemaining())
   {
    System.out.println((char)bf.get());
   }*/
   /**
    * 以下仅适用于英文
    */
   System.out.println(bf.asCharBuffer());
   //修改后
   //String encoding=System.getProperty("file.encoding");
   //System.out.println(Charset.forName(encoding).decode(bf));
   /**
    * 或者在输入时使用UTF-16BE
    */
   
   bf.clear();
   fc.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void LineNumberReaderTest(){
  try {
   LineNumberReader lr=new LineNumberReader(new FileReader("d://1//1.txt"));
   String str="";
   while((str=lr.readLine())!=null)
   {
    System.out.println(lr.getLineNumber()+"     "+str);
   }
   lr.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void printStreamTest(){
  try {
   /**
    * 可以带File,也可以不带
    */
   //PrintStream ps=new PrintStream(new File("d://1//1.txt"));
   PrintStream ps=new PrintStream("d://1//1.txt");
   PrintStream console=System.out;
   System.setOut(ps);
   System.out.println("大连理工大些下次的法规和价格和");
   System.setOut(console);
   ps.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void printWriterTest(){
  try {
   /**
    * PrintWriter()中可以是文件路径及文件名,也可以是File,也可以是OutputStream对象
    */
   PrintWriter pw=new PrintWriter("d://1//1.txt");
   pw.println("大利埃法规和加快了立刻离开");
   pw.write("是大方过后就看见"+"/n");
   pw.write(34);
   pw.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void StringReaderTest(){
  /**
   * FileReader不行
   */
  BufferedReader br=new BufferedReader(new StringReader("发的规范及会计幅度高于韩国进口"));
  try {
   System.out.println(br.readLine());
   br.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void StringWriterTest(){
  /**
   * FileReader不行
   */
  BufferedWriter br=new BufferedWriter(new StringWriter(123));
  try {
   br.write("按时大法官采购机构客户及客户即可");
   br.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void main(String[] args) {
  //Input();
  //Output();
  //FileReaderTest();
  //FileWriterTest();
  //bufferedReaderTest();
  //bufferedWriterTest();
  //InputStreamReader();
  //OutputStreamWriter();
  //DataoutputStreamTest();
  //DataInputStreamTest();
  //ByteArrayStream();
  //ObjectOutputStreamTest();
  //ObjectInputStreamTest();
  //systemInputStreamReader();
  //systemInputStream();
  //systemOutputStream();
  //testshurushuchuzhuanghuan();
  //RandomAccessFileOutTest();
  //RandomAccessFileInTest();
    //FileChanelOutTest();
  //FileChanelGBKTest();
  //FileChanelInTest();
  //LineNumberReaderTest();
  //printStreamTest();
  //printWriterTest();
  //StringReaderTest();
  //StringWriterTest();
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值