随笔 - 4,  文章 - 1,  评论 - 0,  阅读 - 225
创建文件的三种方法
//方式 1
@Test
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();
}
}
//方式 2
@Test
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);
}
}
//方式三
@Test
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);
}
}
}
2、获取文件的相关信息
常见的 File 相关方法
getName()/getAbsolutePath/getParent/length/exists/isFile/isDirectory
获取文件名、获取绝对路径
UTF-8 一个英文一个字节,一个汉字三个字节
import org.testng.annotations.Test;import java.io.File;
public class FileInformation {
public static void main(String[] args) {
}
//获取文件信息
@Test
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());
}
}
3、目录的操作
创建一级目录:mkdir,创建多级目录:mkdirs ,delete 删除空目录或者文件
import org.testng.annotations.Test;
import java.io.*;
public class fileDirectory {
public static void main(String[] args) {
}
@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 fileDeleteD(){
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.txt";
File file = new File(dirPath);
if(file.exists()){
System.out.println(dirPath+"该目录已经存在");
}else{
if(file.mkdirs()){
System.out.println("创建成功");
}else {System.out.println("创建失败");
};
}
}
//InputStream
//OutputStream
//Writer
//Reader
}
Scanner 与 Println
import org.testng.annotations.Test;
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");
}
}
import java.util.Scanner;
public class scanTest {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);//double 类型的数据
System.out.print("请输入一个 double 类型的数:");
double d = input.nextDouble();
System.out.println(d);
//int 类型的数据
System.out.print("请输入一个 int 类型的数:");
int i = input.nextInt();
System.out.println(i);
//字符串类型的数据
System.out.print("请输入一个 string 类型的数:");
String s = input.next();
System.out.println(s);
}
}
posted on   KoNaki  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示