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 @ 2022-11-06 19:47  xxxxdm  阅读(141)  评论(0)    收藏  举报