java中的IO流(课程作业)

static void testStream(){
  FileInputStream in = null;
  try {
   in = new FileInputStream(new File("asd"));
   byte[] buff = new byte[1024];
   while(in.read(buff)!= -1){
    System.out.println(buff);
   }
    
       int b=in.read();
       for(int i=0;b!=-1;i++){          
            System.out.println((char)b);    //输出的是字节流,需要转换
            b=in.read();
       }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   if(in != null){
    try {
     in.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

 

***************************************************************************************************


 static void byteArrayStream(){
  String str = "asdsdfsdf1";
  byte[] bytes = str.getBytes();
  ByteArrayInputStream in = new ByteArrayInputStream(bytes);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  int temp = 0;                                  //此方法将字符写入内存数组保存
  while((temp = in.read())!=-1){
   char ch = (char) temp;
   temp = Character.toUpperCase(ch); 
   out.write(temp);
  }
  byte[] bts = out.toByteArray();
  System.out.println(Arrays.toString(bts));
 }


***************************************************************************************************


static void testCopy(){
  File f1 = new File("asd");
  System.out.println(f1.length());
  File f2 = new File("def");
  FileInputStream in = null;
  FileOutputStream out = null;
  try {
   in = new FileInputStream(f1);
   out = new FileOutputStream(f2);
   byte[] buff = new byte[512];       //复制文件的方式  将asd复制一份def
   while(in.read(buff)!= -1){
    out.write(buff);
    Arrays.fill(buff, (byte)0);    
   }
  }


***************************************************************************************************

static void testReaderCopy(){
  File f1 = new File("asd");
  File f2 = new File("deff");
  FileReader reader = null;
  FileWriter writer = null;
  try {
   reader = new FileReader(f1);
   writer = new FileWriter(f2);
   char chs[] = new char[256];
   while(reader.read(chs)!= -1){
    writer.write(chs);
    Arrays.fill(chs, '\0');    //字符复制文件方法,每一次都要将字符数组清空
   }                                   //否则会发生多写情况
  }

***************************************************************************************************


 static void dataStream(){       //数据流,读取各种数据
 
  try {
 
   FileOutputStream fout = new FileOutputStream("number");
   DataOutputStream dfout = new DataOutputStream(fout);
   int i;
   for(i=0; i<4; i++){
    dfout.writeInt(i);    
   }
   dfout.writeUTF("你好");
   dfout.close();
   
   FileInputStream fin = new FileInputStream("number");
   DataInputStream dfin = new DataInputStream(fin);
   for(i=0; i<4; i++){
    System.out.println(dfin.readInt() + ",");    
   }
   String str = dfin.readUTF();
   System.out.println(str);
            dfin.close();
  
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }


***************************************************************************************************


 /**
  * 实现 Serializable接口
  * 序列化ID 一致性
  * 静态 瞬态 成员变量 不能被序列化
  */  

static void testObjectStream(){
  Student stu = new Student("张伟",3);
  ObjectOutputStream out = null;                  //读取自定义的对象
  ObjectInputStream in = null;
  try {
   FileOutputStream fout = new FileOutputStream("asd");
   out = new ObjectOutputStream(fout);
   out.writeObject(stu);
   in = new ObjectInputStream(new FileInputStream("asd"));
   Student ss = (Student) in.readObject();
   System.out.println(ss);
   System.out.println(ss.temp);
  }


*****************************

public class Student implements Serializable{
 private static final long serialVersionUID = -2L;
 private String name;
 private int age;
 //瞬态
 public transient int temp = 10;
 Student(String name,int age){
  this.name = name;
  this.age = age;
 }
 @Override
 public String toString() {
  return name+age;
 }
 

}

***************************************************************************************************


                                  //读取大文件一般用这个类

static void randomRead(){
  File f = new File("C:/Users/Administrator/Desktop/1.avi");
  RandomAccessFile raf = null;
  long startTime = System.currentTimeMillis();   //随机读写文件流
  FileOutputStream out = null;
  try {
    raf = new RandomAccessFile(f, "rw");
    out = new FileOutputStream(new File("C:/Users/Administrator/Desktop/2.avi"));
    byte[] bys = new byte[1024];          //这里可以设置每次读取的量,它的值影响到读取的速度
    while(raf.read(bys)!= -1){
     raf.write(bys);
     Arrays.fill(bys, (byte)0);
    }
    startTime = System.currentTimeMillis()-startTime;
    System.out.println(startTime);
  }

 

***************************************************************************************************

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值