JavaIO流一部分代码实现

创建文件代码

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

public class Main {

public static void main(String[] args) {
// write your code here
Main main=new Main();
main.create1();
main.create2();
main.create3();
}
public void create1(){
String filePath = "D:\\file1.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("创建文件 1 成功");
} catch (IOException e) {
e.printStackTrace();
}
}
public void create2(){
File parentFile = new File("D:\\");
String fileNane = "file2.txt";


File file = new File(parentFile, fileNane);
try {
file.createNewFile();
System.out.println("文件 2 创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void create3(){
String parentPath = "d:\\";
String filePath = "file3.txt";
File file = new File(parentPath, filePath);
try {
file.createNewFile();
System.out.println("文件 3 创建成功");


} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

实现截图:

 

 

获取文件信息:

package com.company;
import java.io.File;
import java.io.IOException;

public class Main {

public static void main(String[] args) {
// write your code here
Main main=new Main();
main.Info();
}
public void Info(){
File file = new File("D:\\file1.txt");
System.out.println("文件名称:"+file.getName());
System.out.println("文件绝对路径:"+file.getAbsolutePath());
System.out.println("文件父目录:"+file.getParent());
System.out.println("文件大小(字节):"+file.length());


System.out.println("文件是否存在:"+file.exists());
System.out.println("是否是文件:"+file.isFile());
System.out.println("是否是目录:"+file.isDirectory());
}
}

实现截图:

 

posted @ 2022-11-06 22:13  MarK文  阅读(17)  评论(0)    收藏  举报