1. 程式人生 > 其它 >Java檔案操作程式設計

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();
        }
    }
}