美文网首页
文件与流-1

文件与流-1

作者: 李洋codingfarmer | 来源:发表于2018-12-03 19:06 被阅读0次

文件与流

持久化操作:
(文件里、数据库里)Java.io

文件分隔符

   //以下两种路径书写方式windows操作下都可以
    File path1=new File("d:/io");
    File path2=new File("d:\\io");
    //linux下路径是
    File linux_path=new File("/root/home");
    //如果我想两种系统都通用
    //第一种方式用/方式
    //第二种方式  用File.separator 
    File path_ty=new File("d:"+File.separator+"io");
    System.out.println(path_ty);

目录操作

   public void test2() {
    File path1 = new File("d:/io");
    // 判断是否存在 exists()
    // 测试此抽象路径名表示的文件或目录是否存在
    // exist存在
    // Directory 目录
    // file  文件
    if (path1.exists()) {
        // 判断他是不是目录isDirectory()
        if (path1.isDirectory()) {
            //path1.delete();删除目录
            System.out.println("是目录");
        } else {
            System.out.println("不是");
        }
        
    } else {
        System.out.println("目录不存在");
        System.out.println("开始创建");
        //如果不存在,创建目录
        boolean b=path1.mkdir();
        if(b){
            System.out.println("目录创建成功");
        }
        }
}

文件操作

@Test
public void test() {
    //在d:/io/test.txt
    File path1 = new File("d:/io");
   //新建一个文件位置  分成两部分,第一部分path1指定路径 第二部分指定文件名
    File file1=new File(path1,"test.txt");
    if(!path1.exists()){
        path1.mkdir();
    }
    if(!file1.exists()){//如果文件不存在
        //创建一个文件
        try {
            file1.createNewFile();
        } catch (IOException e) {
            System.out.println("创建文件异常");
            e.printStackTrace();
        }
    }
    //读取文件属性
    System.out.println("文件的名称是------>"+file1.getName());
    System.out.println("文件的路径是------>"+file1.getPath());
    System.out.println("文件的绝对路径是------>"+file1.getAbsolutePath());
    System.out.println("文件是否可读"+file1.canRead());
    System.out.println("文件是否可写"+file1.canWrite());
}

@Test
public void test4() {
    
    //新建一个文件位置  分成两部分,第一部分path1指定路径 第二部分指定文件名
    File file1=new File("d:/io/test.txt");
    if(!file1.exists()){//如果文件不存在
        //创建一个文件
        try {
            file1.createNewFile();
        } catch (IOException e) {
            System.out.println("创建文件异常");
            e.printStackTrace();
            
        }
    }
    //读取文件属性
    System.out.println("文件的名称是------>"+file1.getName());
    System.out.println("文件的路径是------>"+file1.getPath());
    System.out.println("文件的绝对路径是------>"+file1.getAbsolutePath());
    System.out.println("文件是否可读"+file1.canRead());
    System.out.println("文件是否可写"+file1.canWrite());
}


@Test
public void test5() {
    //相对于当前路径
    File file1=new File("test.txt");
    if(!file1.exists()){//如果文件不存在
        //创建一个文件
        try {
            file1.createNewFile();
        } catch (IOException e) {
            System.out.println("创建文件异常");
            e.printStackTrace();
        }
    }
    //读取文件属性
    System.out.println("文件的名称是------>"+file1.getName());
    System.out.println("文件的相对路径是------>"+file1.getPath());
    System.out.println("文件的绝对路径是------>"+file1.getAbsolutePath());
    System.out.println("文件是否可读"+file1.canRead());
    System.out.println("文件是否可写"+file1.canWrite());
}

相关文章

  • 文件与流-1

    文件与流 持久化操作:(文件里、数据库里)Java.io 文件分隔符 目录操作 文件操作

  • 第12章 文件与流

    目标 文件与目录管理 流 文件读写(字节流 字符流) 对象序列化 文件与目录管理 前置任务 前置需要了解的知识1、...

  • 流与文件-流

    写在书上 保存下来防止丢失

  • 流与文件

    可以从其中读入一个字节序列的对象称为输入流,而可以向其中写入一个字节序列的对象叫做输出流。抽象类InputStre...

  • 文件与流

    JAVA中的文件及目录处理类 在Java中提供了操作文件及目录(即我们所说的文件夹)类File。有以下几点注意事项...

  • 文件与流

    文件 作用:对文件的管理,在程序里操作硬盘的文件和文件夹概述:通过IO包中的File类实现对文件的管理功能:文件的...

  • Bufffered I/O Others

    获取相关联的文件描述符fd 成功后,fileno()返回与流关联的文件描述符。如果失败,则返回−1。这只能在给定流...

  • 16 文件与流

    什么是文件? 文件可认为是相关记录或放在一起的数据的集合 JAVA程序如何访问文件属性? JAVA中的文件及目录处...

  • IO与流 -- 文件

    文件对象 可以将存在或不存在的文件或目录封装为对象 当创建一个文件对象后,就可以利用它来对文件或目录的属性进行操作...

  • Python与文件流

    Python读写文件非常简单,本文除了介绍简单的读写字符文件和字节文件以外,还会介绍文件对象的属性方法和文件流的一...

网友评论

      本文标题:文件与流-1

      本文链接:https://www.haomeiwen.com/subject/bsducqtx.html