文件常用的操作方法

代码示例

public class FileClient1 {

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

//               File file = new File("D:\\test\\a.txt");

//               long ft = file.lastModified();

//               Date d = new Date(ft);

//               System.out.println(d);

//               createNewFile("ab.txt");

//               deleteFile("d:\\test\\ab");

//               existsFile("d:\\test\\ab");

//               getAbsolutePath("d:\\test\\ab.txt");

//               getFileName("d:\\test\\ab.txt");

//               getParent("d:\\test\\ab.txt");

//               isDirectory("d:\\test");

//               isFile("d:\\test\\ab.txt");

//               list("d:\\test");

//               listFiles("d:\\test");

//               mkdir("d:\\test\\dd");

//               mkdirs("d:\\tests\\dd");

                   renameTo("d:\\test\\a2.txt", "d:\\test\\a2.bat");

//               setReadonly("d:\\test\\a2.txt");

         }

        

         /**

          * 创建一个新文件

          * @param path

          * @throws IOException

          */

         public static void createNewFile(String path) throws IOException {

                   File file = new File(path);

                   boolean createFlag = file.createNewFile();

                   System.out.println(createFlag);

         }

        

         /**

          * 删除一个文件

          * @param path

          * @throws IOException

          */

         public static void deleteFile(String path) throws IOException {

                   File file = new File(path);

                   boolean delteFlag = file.delete();

                   // 返回true表示删除成功 false表示删除失败,失败的原因大部分是文件不存在

                   System.out.println(delteFlag);

         }

        

         /**

          * 判断一个文件后者路径是否存在

          * @param path

          */

         public static void existsFile(String path) {

                   File file = new File(path);

                   System.out.println(file.exists());

         }

        

         /**

          * 获取文件的绝对路径

          * @param path

          */

         public static void getAbsolutePath(String path) {

                   File file = new File(path);

                   System.out.println(file.getAbsolutePath());

         }

        

         /**

          * 获取文件名

          * @param path

          */

         public static void getFileName(String path) {

                   File file = new File(path);

                   System.out.println(file.getName());

         }

        

         /**

          * 获取父路径

          */

         public static void getParent(String path) {

                   File file = new File(path);

                   System.out.println(file.getParent());

         }

        

         /**

          * 判断一个文件对象是否是目录

          */

         public static void isDirectory(String path) {

                   File file = new File(path);

                   System.out.println(file.isDirectory());

         }

        

         /**

          * 判断一个文件对象是否是文件

          * @param path

          */

         public static void isFile(String path) {

                   File file = new File(path);

                   System.out.println(file.isFile());

         }

        

         /**

          * 获取文件目录下的所有文件和目录清单

          * @param path

          */

         public static void list(String path) {

                   File file = new File(path);

                   String[] files = file.list();

                   for (String fileName : files) {

                            System.out.println(fileName);

                   }

         }

        

         /**

          * 获取文件目录下的所有文件和目录清单(文件名)

          * @param path

          */

         public static void listFiles(String path) {

                   File file = new File(path);

                   File[] files = file.listFiles();

                   for (File fileName : files) {

                            System.out.println(fileName.getAbsolutePath());

                   }

         }

        

         /**

          * 创建目录

          * @param path

          */

         public static void mkdir(String path) {

                   File file = new File(path);

                   System.out.println(file.mkdir());

         }

        

         /**

          * 创建目录(如果父目录不存在,则创建父目录)

          * @param path

          */

         public static void mkdirs(String path) {

                   File file = new File(path);

                   System.out.println(file.mkdirs());

         }

        

         /**

          * 文件重命名

          * @param oldFileName

          * @param newFileName

          */

         public static void renameTo(String oldFileName, String newFileName) {

                   File oldFile = new File(oldFileName);

                   File newFile = new File(newFileName);

                   System.out.println(oldFile.renameTo(newFile));

         }

        

         /**

          * 设置文件只读

          * @param path

          */

         public static void setReadonly(String path) {

                   File file = new File(path);

                   System.out.println(file.setReadOnly());

         }

        

        

        

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值