目录
一、文件基础知识
什么是文件?
什么是文件流?
二、常用的文件操作
1.创建文件
package com.feiyang.basic15_file;
import java.io.File;
import java.io.IOException;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description:创建文件
*/
public class FileCreate {
public static void main(String[] args) throws IOException {
//方式一:根据路径
File file1 = new File("d:\\new1.txt");
if(file1.createNewFile()){
System.out.println("创建ok~");
}else{
System.out.println("创建失败~");
}
//方式二:根据父目录文件+子路径
File file2 = new File(new File("d:\\"), "new2.txt");
if(file2.createNewFile()){
System.out.println("创建ok~");
}else{
System.out.println("创建失败~");
}
//方式三:根据父目录+子路径
File file3 = new File("d:\\", "new3.txt");
if(file3.createNewFile()){
System.out.println("创建ok~");
}else{
System.out.println("创建失败~");
}
}
}
2.获取文件信息
package com.feiyang.basic15_file;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description:获取文件信息
*/
public class FileInformation {
public static void main(String[] args) throws IOException {
File file = new File("d:\\hello.txt");
//file.createNewFile();//真正的创建文件
System.out.println("文件名==" + file.getName());
System.out.println("文件大小(字节数)==" + file.length()); //只针对文件有效
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd HHmmss");
System.out.println("文件最后一次修改时间:" + file.lastModified());
Date date = new Date();
date.setTime(file.lastModified());
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println("文件最后一次修改时间:" + format);
System.out.println("文件绝对路径:" + file.getAbsolutePath());
System.out.println("文件父路径:" + file.getParent());
//判断
System.out.println(file.exists());//是否存在
System.out.println(file.isFile());//是否是文件
System.out.println(file.isDirectory());//是否是目录
}
}
3.目录操作
package com.feiyang.basic15_file;
import org.junit.jupiter.api.Test;
import java.io.File;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description:目录操作
*/
public class Directory_ {
public static void main(String[] args) {
}
//判断文件是否存在,存在就删除,否则提示不存在
@Test
public void m1(){
File file = new File("d:\\new1.txt");
if(file.exists()){
if(file.delete()){
System.out.println("文件删除成功~");
}else{
System.out.println("文件删除失败~");
}
}else{
System.out.println("文件不存在~");
}
}
//判断目录d:\demo1是否存在,存在就删除,不存在就提示
//java中,目录也被当作文件
@Test
public void m2(){
File file = new File("d:\\demo1");
if(file.exists()){
if(file.delete()){
System.out.println("删除成功~");
}else{
System.out.println("删除失败~");
}
}else{
System.out.println("该目录不存在~");
}
}
//判断目录是否存在d:\demo\a\b\c,不存在就创建
@Test
public void m3(){
File file = new File("d:\\demo\\a\\b\\c");
if(file.exists()){
System.out.println("该文件已存在");
}else{
boolean mkdirs = file.mkdirs();
if(mkdirs){
System.out.println("多级目录创建成功");
}else{
System.out.println("多级目录创建失败");
}
}
}
}
三、IO流原理及流的分类
1. I/O流原理与分类
2. I/O流体系图
3.字节流
1.FileInputStream
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description:字节输入流(文件 → 程序)
*/
public class FileInputStream_ {
public static void main(String[] args){
}
@Test
public void read01(){
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("d:\\new2.txt"));
//从该输入流读取一个字节的数据,如果没有输入可用该方法将阻止
//如果返回-1,表示读取完毕
int read;
while((read = fis.read()) != -1){
System.out.print((char)read);
}
System.out.println("========================");
//
byte[] b = new byte[1024 * 2];
int len;
while((len = fis.read(b)) != -1){
System.out.print(new String(b,0,len));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void read02() {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("d:\\new2.txt"));
byte[] b = new byte[1024]; //一次读取1024个字节
int len;
//从输入流读取最多b.length个字节的数据到字节数组
while((len = fis.read(b)) != -1){
System.out.print(new String(b,0,len));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.FileOutPutStream
import org.junit.jupiter.api.Test;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: 字节输出流
*/
public class FileOutPutStream_ {
@Test
public void method1() throws IOException {
String file = "d:\\a.txt";
FileOutputStream ost = null;
try {
//ost = new FileOutputStream(file);//会覆盖
ost = new FileOutputStream(file,true);//不会会覆盖
//写入一个字节
//ost.write('E');
//写入多个字符
String s = "Hello World!";
byte[] bytes = s.getBytes();
//ost.write(bytes);
//写入指定范围的字符串
ost.write(bytes,0,5);
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
ost.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
}
}
3.文件拷贝
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: 文件拷贝
*/
public class FileCopy {
public static void main(String[] args) {
//完成思路:先读再写
FileInputStream fis;
FileOutputStream fos;
String srcFile = "d:\\aaa.jpg";
String destFile = "d:\\aaa2.jpg";
byte[] bytes = new byte[1024];
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
int readLen = 0;
while((readLen = fis.read(bytes)) != -1){
fos.write(bytes,0,readLen);
}
System.out.println("拷贝ok~");
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.字符流
1.FileReader
package com.feiyang.basic15_file;
import java.io.FileReader;
import java.io.IOException;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: 字符输入流
*/
public class FileReader_ {
public static void main(String[] args) {
String filePath = "d:\\hello.txt";
FileReader fileReader = null;
int data = 0;
try {
//创建流对象
fileReader = new FileReader(filePath);
//读取,每次读取单个字符,如果文件到末尾返回-1
/*while((data=fileReader.read()) != -1){
System.out.print((char)data);
}*/
//批量读取多个字符到char[],返回读到的实际字符个数
char[] chars = new char[5];
while((data=fileReader.read(chars)) != -1){
System.out.print(new String(chars,0,data));
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.FileWriter
package com.feiyang.basic15_file;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: 字符输出流
*/
public class FileWriter_ {
public static void main(String[] args) {
String filePath = "d:\\note.txt";
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(filePath,true);//追加模式
fileWriter.write('H'); //写入单个字符
char[] chars = {'飞','扬'};
fileWriter.write(chars); //写入字符数组
fileWriter.write("公众号:".toCharArray(),0,4); //写入指定长度的字符数组
fileWriter.write("程序员飞扬"); //写入整个字符串
fileWriter.write("java全栈知识体系公众号",0,13); //写入指定长度的字符串
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fileWriter != null){
//fileWriter.flush();
fileWriter.close(); //切记一定要刷新或关闭流,否则无法写入文件
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}