java文件夹的创建与文件信息的查询操作
创建:
第一种:import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
public class createFile {
public static void main(String[] args) {
}
@Test
public void create1(){
String pathname = "D://file1.txt";
File file = new File(pathname);
try {
file.createNewFile();
System.out.println("6");
}catch (IOException e){
throw new RuntimeException(e);
}
}
}
第二种:
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
public class createFile {
public static void main(String[] args) {
}
@Test
public void create2(){
File parenFile = new File("D:\\");
String fileNane = "file2.txt";
File file = new File(parenFile,fileNane);
try {
file.createNewFile();
System.out.println("6");
}catch (IOException e){
throw new RuntimeException(e);
}
}
}
第三种:
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
public class createFile {
public static void main(String[] args) {
@Test
public void create3(){
String parentPath = "d:\\";
String filePath = "file3.txt";
File file = new File(parentPath,filePath);
try {
file.createNewFile();
System.out.println("6");
}catch (IOException e){
throw new RuntimeException(e);
}
}
文件信息查询:
import org.testng.annotations.Test;
import java.io.File;
public class find {
public static void main(String[] args){
}
@Test
public void lnfo(){
File file = new File("D:\\file 1.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());
}
}
Scanner和Println:
import java.util.Scanner;
public class demo04 {
public static void main(String[] args) {
Scanner scan = new Scanner(Scanner.in);
System.out.println("使用next方式接收:");
String str = Scanner.nextLine();
System.out.println("输出的内容为:"+str);
Scanner.close();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!