Java IO流

File类对象用来表示一个文件或者文件目录,可以用来对文件进行创建,删除,命名等操作,

但是不能对文件内容进行修改,只能通过IO流进行文件的读写。

File类的对象常常作为IO流具体类构造器的形参。

public static void main(String[] args) throws IOException{

       File  file = new File("hello.txt"); 
       System.out.println(file.getName());
       System.out.println(file.getParent());
       System.out.println(file.getPath());
       System.out.println(file.getAbsolutePath());
       System.out.println(file.getAbsoluteFile());
       System.out.println(file.exists());
       System.out.println(file.canWrite());
       System.out.println(file.canRead());
       System.out.println(file.isFile());
       System.out.println(new Date(file.lastModified()));
       System.out.println(file.length());
       System.out.println(file.delete());
       System.out.println(file.createNewFile());
   }

}

按照数据流向的不同: 输入流和输出流

按照处理数据的单位的不同:字节流(文本文件和视频文件,音频文件,图片等非文本文件)和字符流(只适合文本文件)

按照角色的不同:节点流(直接作用于文件的)和处理流

IO类体系:

抽象基类:  InputStream        OutputStream    Reader      Writer

节点流(4个):FileInputStream  FileOutputStream  FileReader  FileWriter

处理流(套接在节点流上,即以节点流对象为其构造器形参):

             缓冲流(一种处理流)

              BufferedInputStream  BufferedOutputStream   BufferedReader   BufferedWriter

             转换流(一种处理流)提供字符流与字节流的转换

            InputStreamReader  (字节流--(解码)-->字符流)     OutputStreamWriter(字符流--(编码)-->字节流)

             对象流(引用数据类型(对象)的序列化和反序列化)

             ObjectInputStream  ObjectOutputStream(字节流)

             标准输入输出流

             System.in(从键盘输入)      System.out(在显示屏输出)

             打印流

              PrintStream(字节流) (System.out返回的就是PrintStream)       PrintWriter(字符流)

             数据流(用来处理基本数据类型,String)

             DataInputStream       DataOutputStream

             RandomAeccessFile随机访问文件流

              既可以充当输入流又可以充当输出流,支持随机访问,支持从文件任意位置读取,写入。

例: 使用BufferedReader和 BufferedWriter进行文件复制

       File  file1 = new File("hello.txt"); 
       File file2 = new File("hello1.txt");
        FileReader fr = new FileReader(file1);
        FileWriter fw = new FileWriter(file2);
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(fw);
        char[] re =new char[20];
        try{
        int len;
        while((len =br.read(re))!=-1){
                bw.write(re,0,len);
                bw.flush();
        }
         }catch(IOException e){
             e.printStackTrace();
         }finally{
          if(br != null){
         try{
        br.close();
          }catch(IOException e){
          e.printStackTrace();
          }   
        }
            if(bw != null){
             try{
        bw.close();
          }catch(IOException e){
          e.printStackTrace();
          }
        }
        }

例: 使用转换流InputStreamReader ,Ou tputStreamWriter,BufferedReader和 BufferedWriter进行文件复制       

import java.io.File;
import java.util.Date;
import java.io.IOException;
import java.io.*;
class TestIO1{
        BufferedReader br=null;
        BufferedWriter bw =null;

     try{

     //字节流转成字符流

        FileInputStream fis = new FileInputStream(file1);
        InputStreamReader isr = new InputStreamReader(fis,"GBK");
         br = new BufferedReader(isr);
      //字符流转成字节流
        FileOutputStream fs = new FileOutputStream(file2);
        OutputStreamWriter osw = new OutputStreamWriter(fs,"GBK");
         bw = new BufferedWriter(osw);
        String str;
        while((str=br.readLine())!=null){
        bw.write(str);
        bw.newLine();
        bw.flush();
          }
        }catch(IOException e){
              e.printStackTrace();
        }finally{
        if(bw != null){
                  try{
                  bw.close();
                  }catch(IOException e){
                     e.printStackTrace();
                  }
        }
            if(bw != null){
                  try{
                  br.close();
                  }catch(IOException e){
                     e.printStackTrace();
                  }
        }      
        }        
    }          
}

例: 使用转换流InputStreamReader,缓冲流BufferedReader,对标准输入流进行转换     

import java.io.*;

class TestIO2{

public static void main(String[] args){

   BufferedReader br=null;
   try{
   InputStream ins = System.in;
 //标准输入流返回InputStream
    InputStreamReader isr =new InputStreamReader(ins); // 字节流转成字符流
    br = new BufferedReader(isr); 
    String str;
    while(true){
    System.out.println("enter ");
    str = br.readLine();

    if((str.equals("e")||(str.equals("exit"))){   

              //此处不能使用==,因为str不是字符串常量,并不指向字符串常量池

            break;
    }
    System.out.println(str.toUpperCase());
    }
}catch(IOException e){
   e.printStackTrace();
}finally{
   try{
    br.close();
   }catch(IOException e){
      e.printStackTrace();
   }
}
   
   }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值