IO流(File类,File基本操作,遍历目录,RandomAccessFile)

IO流就是JAVA程序进行数据传输的管道

File类

基本API:

  • exists() 判断文件/文件夹是否存在
  • mkdir()创建文件夹 mkdirs()创建多级目录(即文件夹)
  • delete()删除文件夹、文件
  • isDirectory() 判断是不是一个目录(即文件夹)
  • isFile()) 判断是不是一个文件
  • createNewFile() 创建文件
  • file
    toString()
    getAbsoluteFile() 三个都是获取路径
  • getName() 获取文件名
  • getParent() 访问父目录
    file.getParentFile().getAbsoluteFile()); 先访问父目录,再访问当前目录
public class FileDemo01 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        File file=new File("D:\\code\\test");
        System.out.println(file.exists()); //exists()判断文件/文件夹是否存在
        if(!file.exists()) {
    
    
            file.mkdir();//mkdir()创建文件夹  mkdirs()创建多级目录
        }
        //else file.delete(); //delete()删除文件夹
        System.out.println(file.isDirectory());//判断是不是一个目录
        System.out.println(file.isFile()); //判断是不是一个文件

        File file1=new File("D:\\code\\test\\IO数据流.txt");
        if(!file1.exists()){
    
    
            file1.createNewFile(); //createNewFile()创建文件
        }
        //else file1.delete();
        System.out.println("========================");
        System.out.println(file1.isDirectory());//判断是不是一个目录
        System.out.println(file1.isFile()); //判断是不是一个文件

        System.out.println("========================");
        //常用File类的API
        System.out.println(file);
        System.out.println(file.toString());
        System.out.println(file.getAbsoluteFile());//三个都是获取路径

        System.out.println(file.getName());
        System.out.println(file1.getName());   //获取文件名

        System.out.println(file.getParent());
        System.out.println(file1.getParent());  //访问父目录
        System.out.println(file.getParentFile().getAbsoluteFile()); //先访问父目录,再访问当前目录
    }
}
/*输出:

true
true
false
========================
false
true
========================
D:\code\test
D:\code\test
D:\code\test
test
IO数据流.txt
D:\code
D:\code\test
D:\code

Process finished with exit code 0
*/
File基本操作,遍历目录
//测试类
public class FileUtiTest1 {
    
    
    public static void main(String[] args) throws IOException {
    
    
       FileUtils.listDirectory(new File("D:\\code\\test")); //调用FileUtils类中的listDirectory方法
    }
}


//列出一些File类常用的操作
public class FileUtils {
    
    
   //列出指定目录下所有的文件,包括其子目录下的所有文件           抛出IO的异常,避免出现异常
    public static void listDirectory(File dir) throws IOException{
    
    
         if(!dir.exists()){
    
    
            throw new IllegalArgumentException("目录"+dir+"不存在"); //如果dir不存在,抛出异常
         }
         if(!dir.isDirectory()){
    
    
             throw new IllegalArgumentException(dir+"不是目录");
         }
         /* //返回的是字符串数组,不包含子目录下的内容
         String[] filenames= dir.list();
         for (String string:filenames) {
            System.out.println(dir+"\\"+string);
         }
         */

         /*
         如果要遍历子目录下的内容,就需要构造成File对象做递归操作
         File提供了直接返回File对象的API
         返回的是直接子目录(文件)的抽象
         */
        File[] files=dir.listFiles();
        if (files!=null&&files.length>0){
    
    
             for(File file:files){
    
    
                 if (file.isDirectory()){
    
    
                     //递归
                     listDirectory(file);
                 }else {
    
    
                     System.out.println(file);
                 }
             }
         }
    }
}
/*输出:
D:\code\test\IO数据流.txt
D:\code\test\www\日记.txt
D:\code\test\www\笔记.txt
D:\code\test\测试\新建文本文档.txt

Process finished with exit code 0
*/
RandomAccessFile

RandomAccessFile是JAVA提供的对文件进行访问的类,既可读文件,也可写文件

  • 打开文件有两种模式,“rw”(读写)、“r”(只读)
    写方法:raf.write(int ) 只写一个字节,同时指针指向下一个位置,准备再次写入
    读方法:int b=raf.read()读一个字节
  • 读写完成后一定要关闭,否则会产生意想不到的问题
public class RafDemo {
    
    
    public static void main(String[] args) throws IOException {
    
    
        File demo=new File("demo");
        if(!demo.exists()){
    
    
            demo.mkdir();   //创建demo文件夹
        }
        File file=new File(demo,"raf.fat");
        if(!file.exists()){
    
    
            file.createNewFile();   //创建raf.fat文件
        }
        RandomAccessFile raf=new RandomAccessFile(file,"rw");
        //指针的位置
        System.out.println(raf.getFilePointer());
        raf.write('A'); //只写了一个字节,不是整个A被写进去
        System.out.println(raf.getFilePointer());
        raf.write('B');

        int i=0x7fffffff;
        //write方法每次只写一个字节,要把i写进去就得写4次
        raf.write(i>>>24);
        raf.write(i>>>16);
        raf.write(i>>>8);
        raf.write(i);

        //可以直接写一个int
        raf.writeInt(i);

        String s="中";
        byte[] gbk=s.getBytes("gbk");
        raf.write(gbk);
        System.out.println(raf.length());

        //读文件,必须把指针移到头部
        raf.seek(0);
        //一次性读取,把文件中的内容都读到字节数组中
        byte[] buf=new byte[(int)raf.length()];
        raf.read(buf);

        System.out.println(Arrays.toString(buf));
        String s1=new String(buf);
        System.out.println(s1);

        //16进制输出
        for (byte b:buf){
    
    
            System.out.println(Integer.toHexString(b&0xff)+"");
        }
        raf.close();
    }
}
/*输出:
0
1
12
[65, 66, 127, -1, -1, -1, 127, -1, -1, -1, -42, -48]
AB��������
41
42
7f
ff
ff
ff
7f
ff
ff
ff
d6
d0

Process finished with exit code 0
*/

猜你喜欢

转载自blog.csdn.net/z3447643805/article/details/113641827