文件基本介绍
文件是保存数据地方,例如word文档、txt文件、excle文件等等,它既可以保存文字、声音、视频等。
文件流
文件在程序中是以流的形式来操作的;
java程序 (内存) |
输出流⬇ ⬆输入流
文件 (磁盘) |
流:数据在数据源(文件)和程序(内存)之间经历的路径
输入流:数据从数据源(文件)到程序(内存)的路径
输出流:数据从程序(内存)到数据源(文件)的路径
常用的文件操作
创建文件对象相关的构造器和方法:
//一个路径代表一个文件
new File(String pathname) //根据路径构建一个File对象
new File(File parent, String child) //根据父目录文件+子路径构建一个File对象
new File(String parent, String child) //根据父目录+子路径构建一个File对象
createNewFile()方法,创建新文件
★File类实现了Serializable和Comparable接口
package com.pero.io_;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
/**
* 演示创建文件
*
* @author Pero
* @version 1.0
*/
public class File_ {
public static void main(String[] args) {
}
//方式一:new File(String pathName)
@Test
public void create01(){
//创建文件路径
String filePath = "d:\\news1.txt"; //或者"d:/news1.txt"
//创建路径对象
File file = new File(filePath);
try {
//创建文件
file.createNewFile(); //把文件创建到硬盘上
System.out.println("文件创建成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
//方式二:new File(File parent, String child) //根据父目录文件+子路径构建
public void create02(){
//创建父目录
File parentFile = new File("d:/"); //或者"d:\\"
//定义子路径
String fileChildName = "news2.txt";
//以父目录+子路径形式创建路径对象
File file = new File(parentFile, fileChildName);
//创建文件
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
//方式三:new File(String parent, String child) //根据父目录+子路径构建
public void create03(){
String parentName = "d:\\";
String childName = "news3.txt";
File file = new File(parentName, childName);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
获取文件信息
获取文件的相关信息:getName,getAbsolutePath,getParent,length,exists,isFile,isDirectory等等;
package com.pero.io_;
import org.junit.jupiter.api.Test;
import java.io.File;
/**
* File常用方法
*
* @author Pero
* @version 1.0
*/
public class FileInformation {
public static void main(String[] args) {
}
@Test
//获取文件信息
public void info(){
//先创建文件路径
File file = new File("d:\\news1.txt"); //获取文件
//调用相应方法,得到对应信息
//获取文件名
System.out.println("文件名称:"+file.getName());
//获取绝对路径
System.out.println("文件绝对路径:"+file.getAbsolutePath());
//获取父目录
System.out.println("文件父目录:"+file.getParent());
//获取文件大小
System.out.println("文件大小(字节):"+file.length());
//判断文件是否存在
if(file.exists()){
System.out.println("文件存在");
}else {
System.out.println("文件不存在");
}
//判断此抽象路径名表示的文件是否为普通文件
if (file.isFile()){
System.out.println("此抽象路径名表示的文件是普通文件");
}else {
System.out.println("此抽象路径名表示的文件不是普通文件");
}
//判断是不是一个目录
if (file.isDirectory()){
System.out.println("是一个目录");
}else {
System.out.println("不是目录");
}
File file1 = new File("d:\\");
if (file1.isDirectory()){
System.out.println("是一个目录");
}else {
System.out.println("不是目录");
}
}
}
常用文件操作
1)目录操作和文件删除
mkdir创建一级目录,mkdirs创建多级目录,delete删除空目录或文件;
package com.pero.io_;
import org.junit.jupiter.api.Test;
import java.io.File;
/**
* @author Pero
* @version 1.0
*/
public class Directory_ {
public static void main(String[] args) {
}
@Test
//判断文件是否存在,如果存在则删除文件
public void test1() {
File file = new File("d:\\news03.txt");
if (file.exists()) {
System.out.println("文件存在");
boolean delete = file.delete();
if (delete) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} else {
System.out.println("文件不存在");
}
}
@Test
//判断目录是否存在,如果存在则删除目录(在java编程中,目录也当做文件)
public void test2() {
File file = new File("d:\\demo");
if (file.isDirectory()) {
System.out.println("目录存在");
boolean delete = file.delete();
if (delete) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} else {
System.out.println("目录不存在");
}
}
@Test
//判断目录是否存在,如果存在就删除,如果不存在就创建
public void test3(){
String pathName = "d:\\demo\\test\\news";
File file = new File(pathName);
if (file.isDirectory()){
System.out.println("目录存在");
if (file.delete()){
System.out.println("news目录删除成功");
}else {
System.out.println("news目录删除失败");
}
}else {
System.out.println("目录不存在");
if (file.mkdirs()){ //创建多级目录;如果要创建一级目录则调用mkdir();
System.out.println("目录创建成功");
} else {
System.out.println("目录创建失败");
}
}
}
}