zoukankan      html  css  js  c++  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();
            }
        }
    }
    

     

  • 相关阅读:
    构造代码块重要理解
    Java中静态代码块、构造代码块、构造函数、普通代码块
    MySQL-分组查询(GROUP BY)及二次筛选(HAVING)
    mysql select将多个字段横向合拼到一个字段
    java语言支持的变量类型
    static修饰属性,方法,类
    恶意代码分析----网络环境配置
    Windows反调试技术(下)
    Windows反调试技术(上)
    脱壳入门----常见的寻找OEP的方法
  • 原文地址:https://www.cnblogs.com/joshua317/p/15724758.html
Copyright © 2011-2022 走看看