练习打卡,日常签到!【如有不足,希望得到老师的斧正】

来源:2-7 自由编程

报告老师

2020-06-14 12:13:25

package com.io.file;

import java.io.File;
import java.io.IOException;

public class IoFlieTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file=new File("c:\\imooc\\File\\Monday.docx");
		//判断File目录是否存在
		if(!file.getParentFile().exists()) {
			file.getParentFile().mkdir();
		}
		if(file.getParentFile().exists()) {
			if(!file.exists()) {
				try {
					file.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(file.exists()) {
					System.out.println("创建文件成功!");
					System.out.println("文件名称:"+file.getName());
					System.out.println("文件上一级目录:"+file.getParentFile().getName());
				}else
					System.out.println("创建文件失败!");
			}
			if(file.exists()) {
				System.out.print("文件/目录:");
				if(file.isFile())
					System.out.println("这是一个文件");
				else if(file.isDirectory())
					System.out.println("这是一个目录");
				System.out.print("读写性:");
				if(file.canRead()&&file.canWrite())
					System.out.println("这个文件既可以读还可以写");
				else if(file.canRead())
					System.out.println("这个文件只可读");
				else if(file.canWrite())
					System.out.println("这个文件只可写");
				else
					System.out.println("这个文件读写均不可以");}
			else
				System.out.println("此文件不存在!");
		}else
			System.out.println("目录不存在!");
	}

}


写回答

1回答

好帮手慕阿满

2020-06-14

同学的代码完成的不错,继续加油。祝:学习愉快~

0

0 学习 · 16556 问题

查看课程