一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

一、File类

public class A01file {
    public static void main(String[] args) throws IOException {
//        路径分隔符(windows:分号、linux:冒号)
//        String pathSeparator = File.pathSeparator;
//        文件名称分隔符(windows:反斜杠、linux:正斜杠)
//        String separator = File.separator;

//        File file = new File("E:\\java\\resource\\aaa.txt");
//        System.out.println(file.getAbsolutePath());//获取绝对路径
//        System.out.println(file.getPath());//获取路径
//        System.out.println(file.getName());//获取文件名
//        System.out.println(file.length());//获取文件大小(字节)

//        System.out.println(file.exists());//判断文件是否存在
//        System.out.println(file.isDirectory());//判断文件是否为目录
//        System.out.println(file.isFile());//判断文件是否为文件

//        File file = new File("E:\\java\\resource\\bbb.txt");
//        System.out.println(file.createNewFile());//创建文件,不会创建路径上不存在的目录,文件已存在也不会创建文件

//        File file = new File("E:\\java\\resource\\test");
//        System.out.println(file.mkdir());//创建目录,不会创建路径上不存在的目录,目录已存在也不会创建目录

//        File file = new File("E:\\java\\resource\\lin\\long\\ding");
//        System.out.println(file.mkdirs());//创建目录,会创建路径上不存在的目录,目录已存在不会创建目录

//        File file = new File("E:\\java\\resource\\lin\\long");
//        System.out.println(file.delete());//删除文件或目录,如果目录下存在目录或文件则无法删除

//        遍历目录下的所有目录和文件
//        File file = new File("E:\\java\\resource");
//        String[] listr = file.list();
//        for (String str : listr) {
//            System.out.println(str);
//        }
//        File file = new File("E:\\java\\resource");
//        File[] lifi = file.listFiles();
//        for (File fi : lifi) {
//            System.out.println(fi);
//        }
    }
}

 

二、文件过滤器

public class A02filefilter {
    public static void main(String[] args) {
        File file = new File("E:\\java\\resource");
        digui(file);
    }

    public static void digui(File dir) {
        if (dir.isDirectory()) {
//            System.out.println(dir);
//            for (File file : dir.listFiles(li -> li.isDirectory() || li.toString().toLowerCase().endsWith(".png"))) {
//                digui(file);
//            }
            for (File file : dir.listFiles((fi, name) -> new File(fi, name).isDirectory() || name.toLowerCase().endsWith(".png"))) {
                digui(file);
            }
        } else {
            System.out.println(dir);
        }
    }
}

 

三、IO流

public class A03outputstream {
    public static void main(String[] args) throws Exception {
//        字节输出流(byte)
//        FileOutputStream fileOutputStream = new FileOutputStream("E:\\java\\resource\\testfileoutputstream.txt", true);
//        fileOutputStream.write("ABCDE".getBytes());
//        fileOutputStream.write("\r\n".getBytes());
//        fileOutputStream.close();

//        字节输入流(byte)
//        FileInputStream fileInputStream = new FileInputStream("E:\\java\\resource\\testfileoutputstream.txt");
//        byte[] bytes = new byte[1024];
//        int len = 0;
//        while ((len = fileInputStream.read(bytes)) != -1) {
//            System.out.println(new String(bytes, 0, len));
//        }
//        fileInputStream.close();

//        文件复制(byte)
//        FileInputStream fileInputStream = new FileInputStream("E:\\java\\resource\\sk2.jpg");
//        FileOutputStream fileOutputStream = new FileOutputStream("E:\\java\\resource\\lin\\sk2.jpg");
//        byte[] bytes = new byte[1024];
//        int len = 0;
//        while ((len = fileInputStream.read(bytes)) != -1) {
//            fileOutputStream.write(bytes, 0, len);
//        }
//        先关闭写,后关闭读
//        fileOutputStream.close();
//        fileInputStream.close();

//        字符输入流(char)
//        FileReader fileReader = new FileReader("E:\\java\\resource\\testfileoutputstream.txt");
//        char[] chars = new char[1024];
//        int len = 0;
//        while ((len = fileReader.read(chars)) != -1) {
//            System.out.println(new String(chars, 0, len));
//        }
//        fileReader.close();

//        字符输出流(char)
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter("E:\\java\\resource\\testfileoutputstream.txt", true);
            fileWriter.write("这是一段中文\r\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

四、properties

public class practice {
    public static void main(String[] args) throws IOException {

        Properties properties = new Properties();
//        properties.setProperty("林志玲", "美丽");
//        properties.setProperty("佟丽娅", "端庄");
//        properties.setProperty("林允儿", "大方");
        
//        遍历properties
//        Set<String> set = properties.stringPropertyNames();
//        for (String s : set) {
//            System.out.println(s + "=" + properties.getProperty(s));
//        }

//        将properties写入硬盘
//        FileWriter fileWriter = new FileWriter("F:\\java\\resource\\data.txt");
//        properties.store(fileWriter, "save data");
//        fileWriter.close();

//        将properties从硬盘读取
        properties.load(new FileReader("F:\\java\\resource\\data.txt"));
        Set<String> set = properties.stringPropertyNames();
        for (String str : set) {
            System.out.println(str + "=" + properties.getProperty(str));
        }
    }
}

 

五、缓冲流

public class practice {
    public static void main(String[] args) throws IOException {
//        字节缓冲输出流的使用
//        FileOutputStream fileOutputStream = new FileOutputStream("F:\\java\\resource\\data.txt", true);
//        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//        bufferedOutputStream.write("这是一段中文".getBytes());
//        bufferedOutputStream.close();

//        字节缓冲输入流的使用
//        FileInputStream fileInputStream = new FileInputStream("F:\\java\\resource\\data.txt");
//        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
//        byte[] bytes = new byte[1024];
//        int len = 0;
//        while ((len = bufferedInputStream.read(bytes)) != -1) {
//            System.out.println(new String(bytes, 0, len));
//        }
//        bufferedInputStream.close();

//        字符缓冲输出流
//        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("F:\\java\\resource\\data.txt"));
//        bufferedWriter.write("这是一段中文");
//        bufferedWriter.newLine();
//        bufferedWriter.write("这是一段中文");
//        bufferedWriter.close();

//        字符缓冲输入流
        BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\java\\resource\\data.txt"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        bufferedReader.close();
    }
}

 

六、转换流

public class practice {
    public static void main(String[] args) throws IOException {
//        转换输出流,默认编码为utf-8
//        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("data.txt"), "gbk");
//        outputStreamWriter.write("你好");
//        outputStreamWriter.close();

//        转换输入流,默认编码为utf-8
//        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("data.txt"), "gbk");
//        int len = 0;
//        while ((len = inputStreamReader.read()) != -1) {
//            System.out.print((char) len);
//        }
//        inputStreamReader.close();

//        将gbk编码文件转换成utf-8文件
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("datagbk.txt"), "gbk");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("datautf8.txt"), "utf-8");
        int len = 0;
        while ((len = inputStreamReader.read()) != -1) {
            outputStreamWriter.write(len);
        }
        outputStreamWriter.close();
        inputStreamReader.close();
    }
}

 

七、序列化流

public class A04objectIO implements Serializable {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        静态成员变量不能被序列化
//        被transient修饰的成员变量不能被序列化
//        类必须实现Serializable接口
//        加serialVersionUID字段防止报InvalidClassException异常

//        对象序列化输出流
        ObjectOutputStream ops = new ObjectOutputStream(new FileOutputStream("data.txt"));
        ops.writeObject(new A04objectIO("林志玲", 18));
        ops.close();

//        对象反序列化输入流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.txt"));
        A04objectIO obj = (A04objectIO) ois.readObject();
        ois.close();
        System.out.println(obj);
    }

    public static final long serialVersionUID = 42L;

    private String name;
    private Integer age;

    public A04objectIO() {
    }

    public A04objectIO(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "A04objectIO{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

八、打印流

public class A05printIO {
    public static void main(String[] args) throws FileNotFoundException {
        PrintStream ps = new PrintStream("data.txt");
        System.setOut(ps);
        System.out.println("打印流打印至文件");
        ps.close();
    }
}

 

posted on 2020-08-18 13:42  一路繁花似锦绣前程  阅读(89)  评论(0编辑  收藏  举报