Java文件的创建、删除

1.如何创建文件?

 

  •  创建文件对象相关构造器

new File(String pathname)//根据路径构造一个File对象

new File(File parent, String child)//根据父目录文件+子路径构建

new File(String parent, String child)//根据父目录+子路径构建

  • 方法

  createNewFile()//创建文件

 

代码:

import org.testng.annotations.Test;

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

public class FileCreate {
public static void main(String[] args) {

}

//创建文件
@Test
public void createFile() {

String filePath = "d:\\file1.txt";

File file = new File(filePath);

try {
file.createNewFile();
System.out.println("创建文件成功");
} catch (IOException e) {
e.printStackTrace();
}
}

//
@Test
public void creat2() {
File parentFile = new File("d:\\");
String fileName = "file2.txt";
File file = new File(parentFile, fileName);

try {
file.createNewFile();
System.out.println("创建文件成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

//
@Test
public void create3() {
String parentPath = "d:\\test\\";
String filePath = "file3.txt";

File file = new File(parentPath, filePath);

try {
file.createNewFile();
System.out.println("创建文件成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

2.获取文件相关信息

  

import org.testng.annotations.Test;

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

public class FileInformation {
public static void main(String[] args) {

}
//获取文件信息
@Test
public void Info(){
//先创建文件对象

File file = new File("D:\\file1.txt");
try {
file.createNewFile();

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

//调用相应的方法,得到对应的信息
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());
}
}

3.目录操作

import org.testng.annotations.Test;

import java.io.File;

public class FileDirectory {
public static void main(String[] args) {

}
//创建多级目录
@Test
public void CreateDir () {
String dirPath = "D:\\test\\dir1";
File d = new File(dirPath);
if(d.mkdirs())
System.out.println("目录创建成功");
}
//删除文件
@Test
public void FileDelete() {
String filePath = "D:\\test\\file3.txt";
File file = new File(filePath);

if (file.exists()) {
if (file.delete()) {
System.out.println(filePath + "删除成功");
} else {
System.out.println(filePath + "删除失败");
}
} else {
System.out.println("文件不存在");
}
}

//删除目录
@Test
public void fileDelete() {
String filePath = "D:\\file1.txt";
File file = new File(filePath);
if (file.exists()){
if (file.delete()) {
System.out.println(filePath + "删除成功");
} else {
System.out.println(filePath + "删除失败");
}
}else{
System.out.println("目录不存在");
}

}

//判断目录是否存在,不在就创建
@Test
public void fileDeleteD1(){
String dirPath = "D:\\test\\dir1";
File file = new File(dirPath);

if(file.exists()){
System.out.println(dirPath+"该目录已存在");
}else {
if(file.mkdirs()){
System.out.println("创建成功");
}else{
System.out.println("创建失败");
}
}
}
}

4.Scanner和printfln

  1.基本键盘的输入

  

import java.util.Scanner;

public class scanPrintTest {
public static void main(String[] args) {
//创建Scanner对象,接受从控制台输入
Scanner input= new Scanner(System.in);
//接受String类型
String str = input.next();
//输出结果
System.out.println(str);
System.out.println("hello world");
}

}

  2.常见键盘输入类型

  

import java.util.Scanner;

public class scanTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//double类型的数据
System.out.println("请输入一个double类型的数:");
double d = input.nextDouble();
System.out.println(d);

//int类型的数据
System.out.println("请输入一个int类型的数:");
int i = input.nextInt();
System.out.println(i);

//字符串类型的数据
System.out.println("请输入一个string类型的数:");
String s = input.next();
System.out.println(s);
}
}
posted @   抹月披风  阅读(395)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示