Java IO流相关知识

一、IO流的分类
  • 字符流
                Reader
                        InputStreamReader(节点流)
                        BufferedReader(处理流)
                Writer
                        OutputStreamWriter(节点流)
                        BufferedWriter(处理流)
                        PrintWriter
  • 字节流
                InputStream
                        FileInputStream(节点流)
                        BufferedInputStream(处理流)
                        ObjectInputStream(处理流)
                        PrintStream
                OutputStream
                        FileOutputStream(节点流)
                        BufferedOutputStream(处理流)
                        ObjectOutputStream(处理流)
  •  断点处理的流
                RandomAccessfile
                
二、IO流的用法
1、转换流的用法
          FileInputStream in = new FileInputStream(newFile(""));         
           Readerreader = new InputStreamReader(in);//字节转字符
           FileOutputStreamout = new FileOutputStream(newFile(""));
           Writer writer = new OutputStreamWriter(out);//字符转字节
           2、对象序列化,对象需要实现Serializable接口
            FileOutputStreamfileOutputStream = new FileOutputStream("C:\\Users\\lx\\Desktop\\Record.txt");
           ObjectOutputStreamobjectOutputStream = new ObjectOutputStream(fileOutputStream);
           objectOutputStream.writeObject(object);//向指定文件写入对象object
           objectOutputStream.close();

            FileInputStreamfileInputStream = new FileInputStream("C:\\Users\\lx\\Desktop\\Record.txt");
           ObjectInputStreamobjectInputStream = new ObjectInputStream(fileInputStream);
           object =  objectInputStream.readObject();//读取得到对象object
            fileInputStream . lose();
        3、断点的运用
public class Copy extends Thread{ //可以利用多线程实现拷贝    
     longstart;
     longend;
     Filesorce;
     Filetargetdir;
     
     publicCopy() {                }
     publicCopy(longstart,long end, File sorce, File targetdir) {//利用构造方法传递需要拷贝的长度,拷贝开始位置,以及目标文件和源文件
           super();
           this.start= start;
           this.end= end;
           this.sorce= sorce;
           this.targetdir= targetdir;
     }
     @Override
     publicvoid run(){         
           try{
                RandomAccessFilesouceRaf = new RandomAccessFile(sorce,"r");
                RandomAccessFiletargetRaf = new RandomAccessFile(newFile(targetdir,sorce.getName()),"rw");
                souceRaf.seek(start);
                targetRaf.seek(start);
                
                intlen= 0;
                byte[]bs = new byte[1024];
                longseek;
                
                System.out.println(start+"---->"+end+this.getName());
                while((len= souceRaf.read(bs))!=-1){
                     targetRaf.write(bs, 0, len);
           
                     seek= souceRaf.getFilePointer();//获取断点位置
                     if(seek== end){
                           break;
                     }
                }    
                targetRaf.close();
                souceRaf.close();
                
           }catch (IOException e) {
                e.printStackTrace();
           } 
     }
}
4、字节流的用法
public class Test_InputStream {//利用字节流获取文本文件内容,但是容易出现问题
/*    
    //可能出现int长度越界
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream(new File("C:\\Users\\lx\\Desktop\\test\\33.txt"));
        byte[] b = new byte[inputStream.available()];    
        inputStream.read(b);
        
        String str = new String(b);
        System.out.println(str);
    
    }
*/
    //可能出现乱码        
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\lx\\Desktop\\test\\33.txt");
        InputStream inputStream = new FileInputStream(file);
        //统计每次读取的实际长度
        int len = 0;
        //声明每次读取1024个字节
        byte[] b = new byte[2];
        StringBuffer sBuffer = new StringBuffer();        
        while((len=inputStream.read(b))!=-1){
            sBuffer.append(new String(b,0,len));
        }
        System.out.println(sBuffer.toString());
    }
}
//利用字节流拷贝文件
public void copy(File sourceFile, File targetDir) {//
           FileInputStreamfileInputStream = null;
           FileOutputStreamfileOutputStream = null;
           fileInputStream= new FileInputStream(sourceFile);
           FiletargetFile = new File(targetDir,sourceFile.getName());
           fileOutputStream= new FileOutputStream(targetFile);
           byte[]b = new byte[1024];
            intlen = 0;
            while((len= fileInputStream.read(b)) != -1) {
                  fileOutputStream.write(b, 0, len);
             }

}
5、缓存字符流的用法
     publicstatic void main(String[] args) throws IOException {//缓存字符流实现写入文件
           InputStreamin = System.in;
           Readerreader = new InputStreamReader(in);
           BufferedReaderbr = new BufferedReader(reader);
           BufferedWriterbw = new BufferedWriter(new FileWriter(new File("src/1.txt")));
           Strings="";
           while((s=br.readLine())!=null) {
                bw.write(s);
                bw.newLine();
                bw.flush();          //字符流千万不要忘了flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
           }
     }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值