File操作文件
public static void main(String[] args) {
try {
//创建File对象
// 绝对路径 相对路径
String path = "D:/4072/1/";
String fileName = "1.txt";
File file = new File(path);
if(!file.exists()&&!file.isDirectory())
{
file.mkdirs();//创建多级
System.out.println("进来创建文件夹");
}
File file1 = new File(path,fileName);
if(!file1.exists()&&!file1.isFile())
{
System.out.println("进来创建文件....");
file1.createNewFile();
}
// System.out.println("判断文件或目录是否存在:"+file.exists());
// System.out.println("判断是否是文件:"+file.isFile());
// System.out.println("判断是否是文件夹:"+file.isDirectory());
// System.out.println("相对路径:"+file.getPath());
// System.out.println("绝对路径:"+file.getAbsolutePath());
// System.out.println("获得文件或目录名称:"+file.getName());
// System.out.println("获得文件字节数:"+file.length());
// System.out.println("删除文件或目录:"+file.delete());
} catch (Exception e) {
e.printStackTrace();
}
}
流的使用和分类
字节流:byte
输入流:input(读取)
InputStream->FileInputStream(路径|对象)
.available()获得文件字节数
.read() || .read(字节数组)
.close();//关闭流 释放资源
public static void main(String[] args) {
InputStream is = null;
try {
is = new FileInputStream("D:\\4072\\1/1.txt");
//定义字节数组 用于接收数据
byte b[] = new byte[is.available()];
is.read(b);//读取内容方入数组
//将字节数组转换为字符串
String str = new String(b,"GBK");
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
is.close();//释放资源
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出流:output(写入)
OutputStream->FileOutputStream(“路径||File对象”,boolean 是否追加内容)
os.write(字节数组) 写入内容 字符串通过getBytes()转为字节数组
os.close();//释放资源
os.flush():强制把缓冲区的数据写到输出流中
public static void main(String[] args) {
OutputStream os = null;
try {
os = new FileOutputStream("D:\\4072\\1/1.txt"
,true);
String msg = "张三";
//将字符串转换为字节数组 并调用write方法进行写入
os.write(msg.getBytes());
System.out.println("写入成功。。。。");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
复制文件内容代码示例:
public static void main(String[] args) {
InputStream is = null; //读
OutputStream os = null; //写
try {
is = new FileInputStream("D:\\4072\\1/1.txt");
os = new FileOutputStream("D:\\4072\\1/2.txt");
int b = -1;
while((b = is.read())!=-1){
os.write(b);
}
System.out.println("复制成功!!!!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//释放资源
if(is!=null) is.close();
if(os!=null) os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
字符流:char
输入流(读取)
Reader->InputStreamReader(字节流InputStream,编码)->FileReader(路径||File对象)
乱码问题:reader = new InputStreamReader(new FileInputStream("路径"), "GBK");
import java.io.*;
/**
* @Author: wzy
* @Date: 2024/9/10 9:22
* @Description: 读取 InputStreamReader 输入流 字符流
*/
public class InputReaderTest {
public static void main(String[] args) {
//Reader->InputStreamReader->FileReader
Reader reader = null;//字符流
try {
reader = new InputStreamReader(new FileInputStream("D:\\4072\\1\\1.txt"), "GBK");
char c[] = new char[100];
reader.read(c);
//将数组转换为字符串
String msg = new String(c);
System.out.println(msg);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
缓冲区:BufferedReader(字符流Reader)
import java.io.*;
/**
* @Author: wzy
* @Date: 2024/9/10 9:22
* @Description: 读取 BufferedReader(字符流) 缓冲区
*/
public class BufferedReaderTest {
public static void main(String[] args) {
//Reader->InputStreamReader->FileReader
Reader reader = null;//字符流
BufferedReader br = null;
try {
reader = new FileReader("D:\\4072\\1\\1.txt");
br = new BufferedReader(reader);
String msg = null;
while((msg = br.readLine())!=null){
System.out.println(msg);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出流(写入)
Writer->OutputStreamWriter(字节流OutputStream,编码)->FileWriter(路径||File对象,是否追加)
write(字符串) 写入方法
close() 释放资源
flush() 刷新缓冲区
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* @Author: wzy
* @Date: 2024/9/10 9:56
* @Description: 写入 Writer 输出 字符流
*/
public class OutputWriter {
public static void main(String[] args) {
//Writer->OutputStreamWriter(字节流OutputStream,编码)->FileWriter(路径||File对象,是否追加)
Writer writer = null;
try {
writer = new FileWriter("D:\\4072\\1\\1.txt"
,true);
//写入
writer.write("\n123");
System.out.println("写入成功!!!!");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
writer.close();//释放资源
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
缓冲区:BufferedWriter(Writer字符流)
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* @Author: wzy
* @Date: 2024/9/10 9:56
* @Description: 写入 BufferedWriter 缓冲区 输出 字符流
*/
public class BufferedWriterTest {
public static void main(String[] args) {
//Writer->OutputStreamWriter(字节流OutputStream,编码)->FileWriter(路径||File对象,是否追加)
Writer writer = null;
BufferedWriter bw = null;
try {
writer = new FileWriter("D:\\4072\\1\\1.txt"
,true);
//创建缓冲区
bw = new BufferedWriter(writer);
//写入信息到缓冲区
bw.write("数据结构");
bw.newLine();//换行
bw.write("软件名词解释");
bw.newLine();//换行
bw.write("Linux虚拟机安装vmware");
//将缓冲区数据刷新到字符流中
bw.flush();
System.out.println("写入成功!!!!");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
bw.close();
writer.close();//释放资源
} catch (IOException e) {
e.printStackTrace();
}
}
}
}