java--IO流

遍历目录

File dir = new File("e:\\java");

        if(!dir.exists()){
            throw new IllegalArgumentException("目录:" + dir + "不存在!");
        }
        if(!dir.isDirectory()){
            throw new IllegalArgumentException(dir + "不是目录!");
        }
        //String[] filenames = dir.list();//返回结果是字符串数组,直接子目录的名称
        File[] filenames = dir.listFiles();//返回结果是File,直接子目录的抽象
        if(filenames != null && filenames.length > 0){
            for(File f : filenames){
                if(f.isDirectory()){
                    //递归
                    listDirectory(f);
                }
                else
                    System.out.println(f);
            }
        }

访问文件

(读和写)– RandomAccessFile类

File file = new File("e:\\javaio\\a.dat");
        if(!file.exists())
            file.createNewFile();

        RandomAccessFile raf = new RandomAccessFile(file,"rw");
        System.out.println(raf.getFilePointer());
        raf.write('A');
        System.out.println(raf.getFilePointer());
        int i = 0x7fffffff;
        raf.writeInt(i);
        System.out.println(raf.getFilePointer());

运行结果:看到写入后指针位置的变化
这里写图片描述

字节流

public class IOUtil {
    //读取指定文件内容,16进制输出到控制台,每10个byte换行
    //单字节读取(不适合大文件)
    public void printHex(String fileName) throws IOException{
        FileInputStream in = new FileInputStream(fileName);
        int b=0,i=1;
        while((b = in.read()) != -1){
            //单字节前补0(十六进制用两个字节表示)
            if(b <= 0xf)
                System.out.print("0");
            System.out.print(Integer.toHexString(b) + "  ");
            if(i % 10 == 0)
                System.out.println();
            i++;
        }
        in.close();
    }

    //批量读取
    public void printHexByByteArray(String fileName) throws IOException{
        FileInputStream in = new FileInputStream(fileName);
        byte[] bytes = new byte[20 * 1024];//设置缓冲区数组
        int b=0,j=1;
        while((b = in.read(bytes,0,bytes.length)) != -1){
            for(int i = 0 ; i < b ; i++){
                if(bytes[i] <= 0xf){
                    System.out.print("0");
                }
                //byte占8位,int占32位,转换进制时去掉前24位,节省内存
                System.out.print(Integer.toHexString(bytes[i] & 0xff) + " ");
                if(j % 10 == 0)
                    System.out.println();
                j++;
            }
        }

        in.close();
    }

    //写文件
    public void outPut1()throws IOException{
        //文件不存在则直接创建,若存在则删除再创建,若不删除在后面写,则加true
        FileOutputStream out = new FileOutputStream("e:\\javaio\\out.dat");
        out.write('A');
        int a = 10;
        out.write(a);
        byte[] bytes = "中国".getBytes();
        out.write(bytes);
        out.close();
    }

    //复制文件
    public void copyFile(File srcFile,File destFile) throws IOException{
        if(!destFile.exists())
            throw new IllegalArgumentException("文件" + destFile + "不存在!");
        if(!srcFile.isFile())
            throw new IllegalArgumentException(srcFile + "不是文件!");
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        byte[] bytes = new byte[8*1024];
        int b = 0;
        while((b = in.read(bytes, 0, bytes.length)) != -1){
            out.write(bytes,0,b);
            out.flush();
        }
        in.close();
        out.close();
    }

    public void outPut2() throws IOException{
        DataOutputStream out = new DataOutputStream(new FileOutputStream("e:\\javaio\\out.dat"));
        out.writeInt(-10);
    }

    //复制文件(用缓冲区)
    public void copyFileByBuffer(File srcFile,File destFile) throws IOException{
        if(!destFile.exists())
            throw new IllegalArgumentException("文件" + destFile + "不存在!");
        if(!srcFile.isFile())
            throw new IllegalArgumentException(srcFile + "不是文件!");
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
        int c;
        while((c = in.read()) != -1){
            out.write(c);
            out.flush();
        }
        in.close();
        out.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值