java程序(内存)——>文件(磁盘) 输出流
<—— 输入流
文件创建import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
public class FileCreate {
public static void main(String[] args) {
FileCreate fileCreate = new FileCreate();
fileCreate.create02();
}
//方法一
private void create01(){
String filePath = "D:\computer language\java\wei.c";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//方法2
public void create02(){
File parentFile = new File("D:\computer language\");
String fileName = "wei2.txt";
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
file方法
javapublic class FileCreate {
public static void main(String[] args) {
FileCreate fileCreate = new FileCreate();
fileCreate.create01();
}
//方法一
private void create01(){
String filePath = "D:\computer language\java\wei.c";
File file = new File(filePath);
//调用相应的方法,得到相应信息
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());
}
目录操作
//删除文件
public class FileCreate {
public static void main(String[] args) {
FileCreate fileCreate = new FileCreate();
fileCreate.create01();
}
//方法一
private void create01(){
String filePath = "D:\computer language\java\wei.c";
File file = new File(filePath);
if (file.exists()){
if (file.delete()){
System.out.println(filePath + "删除成功");
}else {
System.out.println("删除失败");
}
}else {
System.out.println("文件不存在");
}
}
}
//目录也被当做文件
IO流
字节流
//字节输入流
//FileInputStream的使用(字节输入流 文件程序)
//单个字节的读取效率低
public class FileInputStream_ {
public static void main(String[] args) {
//FileInputStream fileInputStream = new FileInputStream();
}
@Test
public void readFile01(){
String filePath = "D:\computer language\java\wei.txt";
int readDate;//用于接收
FileInputStream fileInputStream = null;
try {
//
fileInputStream = new FileInputStream(filePath);
//如果返回-1,读取完毕
while( (readDate = fileInputStream.read())!=-1 ){//一个一个字节读
System.out.print((char) readDate);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件流释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile02(){
String filePath = "D:\computer language\java\wei.txt";
int readDate;//用于接收
byte[] buf = new byte[8];//一次读取八个
int readLen = 0;
FileInputStream fileInputStream = null;
try {
//
fileInputStream = new FileInputStream(filePath);
//读取最多b.length字节到字节数组
//如果返回-1,读取完毕
//如果读取正常,返回实际读取的字节数
while( (readLen = fileInputStream.read(buf)) !=-1 ){
System.out.print(new String(buf, 0, readLen) );
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件流释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//字节输出流
public class FileOutputStream_ {
public static void main(String[] args) {
}
//FileOutputStream将数据写到文件中
//如果该文件不存在,创建文件
@Test
public void writeFile(){
//创建FileOutputStream对象
String filePath = "D:\computer language\java\wei.txt";
FileOutputStream fileOutputStream = null;
try {
//new java.io.FileOutputStream(filePath)是覆盖
//new java.io.FileOutputStream(filePath, true);是追加在后边
fileOutputStream = new java.io.FileOutputStream(filePath, true);
//写入一个字节
//fileOutputStream.write('a');//char可自动妆化成int
//写入字符串
String str = "Helle,World!";
//str.getBytes() 可以把字符串转成字符数组
fileOutputStream.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//FileWriter使用后,必须要关闭(close)或者刷新(flush),否者写入不到文件中
文件拷贝
public class FileCopy {
public static void main(String[] args) {
//完成文件拷贝
//记录分析
//1、创建文件输入流,将文件读入到程序
//2、创建文件输出流,将读取的文件,写入指定文件
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
String srcFilePath = "D:\HonOR Magic-link\Magic-link\123.jpg";
String destFilePath = "D:\computer language\java\1.jpg";
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePath);
//定义一个字节数组,提高读写效率
byte[] buf = new byte[1024];
int readLen = 0;
while ( (readLen = fileInputStream.read(buf)) != -1 ){
//读取到后就写入到文件
//边读边写
fileOutputStream.write(buf, 0, readLen);//一定要用这个方法
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter
public class FIleWriter_ {
public static void main(String[] args) {
String filePath = "D:\computer language\java\wei.txt";
//创建FileWriter对象
FileWriter fileWriter = null;
char[] chars = {'v', 'i', 's', 'i', 'o', 'n'};
try {
fileWriter = new FileWriter(filePath);
//写入单个字节
fileWriter.write("V");
//指定数组
fileWriter.write(chars);
//指定数组指定部分
fileWriter.write("Vision魏".toCharArray(), 0, 3);
//写入整个字符串
fileWriter.write("魏长彬");
//写入字符串的指定部分
fileWriter.write("黑龙江天津,0,3");
//数据多可以重复使用
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//fileWriter.flush();
//关闭文件流,等价于flush()+ 关闭
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("创建成功。。。。");
}
}
字节流和处理流
-
节点流:可以从一个特定的数据源读写数据,
访问文件:FileReader、Filewtiter ,比较底层的
访问数组:ByteArrayInputStrea,CharArrayReader
访问通道:PipedInputStream,PipedReader
-
处理流(包装流):BufferedReader/BufferedWriter
可以是文件/数组/数据源,BufferedReader类中,有属性Reader,即可以封装一个字节流,该节点流可以是任意的,只要是Reader的子类就可以
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u5yBwz0Z-1636693478151)(assets/1636689538325.png)]
处理流



