IO流的demo

package com.io.file;

import java.io.File;

/**
 * 1、路径分隔符
 * 2、文件分隔符
 *
 */
public class Demo1 {

   public static void main(String[] args){

       System.out.println(File.pathSeparator);
       System.out.println(File.separator);
       //路径的表示形式
        String path="D:\\softApp\\PDF";
        path="D:"+File.separator+"softApp"+File.separator+"PDF";
        //推荐方式
        path="D:/softApp/PDF";
   }
}
package com.io.file;

import java.io.File;

/**
 * 相对路劲与绝对路劲构造File对象
 * 1、相对路径
 * 2、绝对路劲
 *
 */
public class Demo2 {

    public static void main(String[] args){

        String parentPath="D:/softApp/PDF";
        String name="5.jpg";
        //1、相对路劲
        File src= new File(parentPath,name);
        src= new File(new File(parentPath),name);
        //输出
        System.out.println(src.getName());
        System.out.println(src.getPath());
        //2、绝对路径
        src=new File("D:/softApp/PDF/5.jpg");
        //输出
        System.out.println(src.getName());
        System.out.println(src.getPath());
        //3、没有盘符
        src=new File("test.txt");
        System.out.println(src.getName());
        System.out.println(src.getPath());
        System.out.println(src.getAbsolutePath());
    }
}
package com.io.file;

import java.io.File;
import java.io.IOException;

/**
 * 常用方法
 */
public class Demo3 {
    public static void main(String[] args){
        test();
    }
    //1、名称
    private static void test() {
        //建立联系
        File src = new File("D:/softApp/PDF/5.jpg");
        //File src = new File("5.jpg");
        System.out.println(src.getName());//返回名称
        System.out.println(src.getPath());//如果是绝对路径,返回完整路劲,否则相对路劲
        System.out.println(src.getAbsolutePath());//返回绝对路径
        System.out.println(src.getParent());//返回上一级目录,如果是相对,返回null
    }
    //2、判断信息
    private static void test1() {
        String path="5.jpg";
        File src = new File(path);
        System.out.println("文件是否存在"+src.exists());
        //是否可读写
        System.out.println("文件是否可写"+src.canWrite());
        //是文件
       if(src.isFile()){
           System.out.println("文件");
       }else if(src.isDirectory()){
           //没有存在默认文件夹
           System.out.println("文件夹");
       }else{
           System.out.println("文件不存在");
       }
        System.out.println("是否为绝对路径"+src.isAbsolute());

        System.out.println("长度"+src.length());//不能读取文件夹的长度
    }
    //创建文件
    private static void test2() throws IOException, InterruptedException {
        String path="D:/softApp/PDF/5.jpg";
        File src=new File(path);
        if(!src.exists()){
            boolean flag=src.createNewFile();
            System.out.println(flag?"成功":"失败");
            //删除文件
            boolean flag1=src.delete();
            System.out.println(flag1?"成功":"失败");

            File temp=File.createTempFile("test",".temp",new File("D:/softApp/PDF"));
            Thread.sleep(1000);
            temp.deleteOnExit();//退出即删除
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Richard_666/article/details/88701532
今日推荐