IO流之文件的操作

IO流之文件的操作

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

public class FileOperation {

	public static void main(String[] args) {
		/**
		 * 文件路径:(表示文件夹: / 或者 \\ 表示转义字符)
		 *  1.项目下的文件路径:word.txt
		 *  2.包中的文件路径:src/JavaTutorial.P2.p83/word.txt
		 *                  src\\JavaTutorial.P2.p83\\word.txt
		 *  3.文件的绝对路径:D:/word.txt 
		 *                  D:\\word.txt
		 */
		File file1 = new File("D:/word.txt"); // 第一种构造方法

		System.out.println("判断文件是否存在:" + file1.exists());

		File file2 = new File("D:/", "word.txt"); // 第二种构造方法,文件夹和文件名分开的

		File dir = new File("D:/"); // 文件夹
		File file3 = new File(dir, "word.txt"); // 第三种构造方法

		System.out.println(file1.getAbsolutePath()); // 输出文件的绝对路径
		System.out.println(file2.getAbsolutePath()); // 输出文件的绝对路径
		System.out.println(file3.getAbsolutePath()); // 输出文件的绝对路径

		System.out.println("文件名:" + file1.getName()); // 输出文件名
		System.out.println("绝对路径:" + file1.getAbsolutePath()); // 输出文件的绝对路径
		System.out.println("是否是隐藏文件:" + file1.isHidden()); // 是否是隐藏文件
		System.out.println("文件的字节数:" + file1.length()); // 文件的字节数

		Date date = new Date(file1.lastModified()); // 通过毫秒值创建日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 格式化日期
		System.out.println("文件的最后修改时间:" + sdf.format(date)); // 文件的最后修改时间

		System.out.println("删除文件是否成功:" + file1.delete());

		try {
			// createNewFile()不能覆盖已有文件
			System.out.println("创建文件是否成功:" + file1.createNewFile()); // 创建文件
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅兮昂

你的支持是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值