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();

}
}
posted @   xxxxdm  阅读(121)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示