javaIO实验

前言:这个多看看老师给的ppt,找到接口就行了,注意自己的文件路径; 

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class IOTEST{
    public static void main(String[] args) {
        String fileName = "C:Users/yu/Desktop/from.txt";
        System.out.println("----- 创建文件 ------");
        createFile(fileName);
        System.out.println("-----  将字符串写入文件 -------");
        // \r\n在txt文本中换行
        String str = "白日依山尽"+'\n'+ "黄河入海流"+'\n'+"欲穷千里目"+'\n'+ "更上一层楼"+'\n';
        writeToFile(fileName, str);
        System.out.println("--------- 基于基本IO流实现文件的复制 ----------");
        String toFile = "C:/Users/yu/Desktop/to.txt";
        try {
            copyByIO(fileName, toFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("--------- 基于NIO实现文件的复制 ----------");
        String toFile2 = "C:/Users/yu/Desktop/nio.txt";
        try {
            copyByIO(fileName, toFile2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("---------- 删除指定文件 -------------");
        try {
            deleteFile(toFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("---------- 遍历指定目录文件 -------------");
        String dir = "C:/Users/yu/Desktop";
        walkDirectories(dir);
    }
    /**
     * 基于指定文件名称创建目录及文件
     * 如果文件已经存在,则忽略
     *
     * @param fileName
     */
    private static void createFile(String fileName) {
        Path p=Paths.get(fileName);
        if(!Files.exists(p)){
            try {
                Files.createDirectories(p.getParent());
                Files.createFile(p);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 提示:文件以字节操作,因此可以
     * 字符串,转字节数组,直接基于Files写入文件
     *
     * @param fileName
     * @param content
     */
    private static void writeToFile(String fileName, String content) {
        Writer writer=null;
        try {
            writer=new FileWriter(fileName);
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 基于基本IO,以及字节数组缓冲区,复制文件
     * 打印显示循环读写循环次数
     *
     * @param sourceFile
     * @param targetFile
     */
    private static void copyByIO(String sourceFile, String targetFile) throws IOException {
        Writer writer=null;
        Reader  reader=null;
        reader=new FileReader(sourceFile);
        writer =new FileWriter(targetFile);
        char []str=new char[1024];
        reader.read(str);
        writer.write(str);
        if(writer!=null){
            writer.close();
        }
        if(reader!=null){
            reader.close();
        }
    }

    /**
     * 基于NIO,实现文件的复制
     *
     * @param sourceFile
     * @param targetFile
     */
    private static void copyByNIO(String sourceFile, String targetFile) throws IOException {
        createFile(targetFile);
        Path sourcepath=Paths.get(sourceFile);
        Path targetpath=Paths.get(targetFile);
        Files.copy(sourcepath,targetpath);
    }

    /**
     * 删除文件
     *
     * @param fileName
     */
    private static void deleteFile(String fileName) throws IOException {
        Path path=Paths.get(fileName);
        Files.delete(path);
    }

    /**
     * 遍历打印指定目录下全部目录/文件名称
     *
     * @param dir
     */
    private static void walkDirectories(String dir) {
        Path path=Paths.get(dir);
        try {
            Files.walk(path).forEach(
                    s->System.out.println(s)
            );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_56350439/article/details/124595621