Java文件操作编程

package com.joshua317;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static void main(String[] args) {
        renameFile();
    }

    /**
     * 当前目录下,创建文件
     */
    public static void createFile()
    {
        String dir = System.getProperty("user.dir");
        String filePath = dir + "/info.log";
        try {
            File file = new File(filePath);
            if (file.createNewFile()) {
                System.out.println("创建文件成功!");
            } else {
                System.out.println("创建失败,该文件已经存在!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 当前目录下,写入文件
     */
    public static void writeFile()
    {
        String dir = System.getProperty("user.dir");
        String filePath = dir + "/info1.log";
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
            bufferedWriter.write("joshua317\n");
            bufferedWriter.write("joshua318\n");
            bufferedWriter.close();
            System.out.println("写入文件成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 当前目录下,追加文件内容
     */
    public static void writeAppendFile()
    {
        String dir = System.getProperty("user.dir");
        String filePath = dir + "/info2.log";
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath,true));
            bufferedWriter.write("joshua317\n");
            bufferedWriter.close();
            System.out.println("追加文件文件成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 将文件内容复制到另一个文件
     */
    public static void copyFile()
    {
        String dir = System.getProperty("user.dir");
        String filePath1 = dir + "/info2.log";
        String filePath2 = dir + "/info3.log";

        try {
            InputStream in = new FileInputStream(new File(filePath1));
            OutputStream out = new FileOutputStream(new File(filePath2));
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();

            BufferedReader fileReader = new BufferedReader(new FileReader(filePath2));
            String str;
            while ((str = fileReader.readLine()) != null) {
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 当前目录下,读取文件内容
     */
    public static void readFile()
    {
        String dir = System.getProperty("user.dir");
        String filePath = dir + "/info2.log";
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                System.out.println(str);
            }
            bufferedReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 当前目录下,获取文件大小,1KB=1024字节
     */
    public static void getFileSize() {
        String dir = System.getProperty("user.dir");
        String filePath = dir + "/info2.log";
        File file = new File(filePath);
        if (!file.isFile() || !file.exists()) {
            System.out.println("文件不存在");
        } else {
            System.out.println(file.length());
        }
    }

    /**
     * 当前目录下,获取文件最后的修改时间
     */
    public static void getLastModified() {
        String dir = System.getProperty("user.dir");
        String filePath = dir + "/info2.log";
        File file = new File(filePath);
        //文件最后的修改时间
        long lastModified = file.lastModified();
        //格式化打印日期
        Date date = new Date(lastModified);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        System.out.println(format);
    }

    /**
     * 当前目录下,获取文件最后的修改时间
     */
    public static void fileExists() {
        String dir = System.getProperty("user.dir");
        String filePath = dir + "/info4.log";
        File file = new File(filePath);
        System.out.println(file.exists());
    }

    /**
     * 当前目录下,重命名文件
     */
    public static void renameFile() {
        String dir = System.getProperty("user.dir");
        String filePathOld = dir + "/info2.log";
        String filePathNew = dir + "/info5.log";
        File fileOld = new File(filePathOld);
        File fileNew = new File(filePathNew);
        //  确保新的文件名不存在
        try {
            if (fileNew.exists()) {
                throw new IOException("file exists");
            }
            if(fileOld.renameTo(fileNew)) {
                System.out.println("已重命名");
            } else {
                System.out.println("重命名失败");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2021-12-23 18:36  joshua317  阅读(33)  评论(0编辑  收藏  举报