首页 > 其他分享 >文件

文件

时间:2022-11-04 21:22:27浏览次数:32  
标签:文件 File System file println out

什么是文件?

文件是我们保存数据的地方。

文件流

文件在程序中是以流的形式来操作的。

流:数据在数据源(文件)和程序(内存)之间经历的路径

输入流:数据从数据源(文件)到程序(内存)的路径

输出流:数据从程序(内存)到数据源(文件)的路径

常用的文件操作

1、创建文件对象相关构造器和方法(源代码放在随笔最后附件一)

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

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

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

createNewFlie()//创建文件

创建文件方式一: 

创建文件方式二: 

 

创建文件方式三: 

2、获取文件相关信息(源代码放在随笔最后附件二)

常见的 File 相关方法

getName()/getAbsolutePath/getParent/length/exists/isFile/isDirectory

获取文件名、获取绝对路径

UTF-8一个英文一个字节,一个汉字三个字节

可以通过一下操作进行查询文件相关资料:

3、目录的操作(源代码放在随笔最后附件三)

创建一级目录:mkdir,创建多级目录:mkdirs,delete 删除空目录或者文件

 

 

 

 

 

 附件一:

import org.testng.annotations.Test;

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

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

//方式1
@Test
public void create1() {
String filePath = "D:\\CDH1.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 = "CDH2.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 = "CDH3.txt";
File file = new File(parentPath, filePath);
try {
file.createNewFile();
System.out.println("文件3创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

附件二:

import org.testng.annotations.Test;

import java.io.File;

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

}
//获取文件信息
@Test
public void lnfo(){
//先创建文件对象
File file = new File("D:\\CDH1.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());
}
}

附件三:

import org.testng.annotations.Test;

import java.io.*;

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

}

@Test
//删除文件
public void fileDelete() {
String filePath = "D:\\CDH1.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:\\CDH1.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
}

 

标签:文件,File,System,file,println,out
From: https://www.cnblogs.com/cdh55/p/16859104.html

相关文章