目录
4.1 字符流FileReader加BufferedReader实现读取
4.2 字符流FileWriter加BufferedWriter实现写出
1.File的简介
1.1 生活中的文件
- 文件的作用,持久化(瞬时状态的对立面)
- 文件的定义:一堆数据的集合
- 文件存储的位置:磁盘,硬盘,软盘,U盘等等
1.2 计算机中的文件File
- File的定义:java.io中的File类
- 使用File:new File(); //创建文件实例
- File 属性:文件的位置,文件的名称,文件的内容
1.3 File文件的增删查
(1)新建文件——createNewFile()
//实例化File文件对象,对象中参数存放有文件的路径和文件的名称
//文件的路径D:\\或D:/
File file = new File("/Users/yihongda/Downloads/jojo.txt");
Demo001 demo001 = new Demo001();
demo001.addFile(file);
public void addFile(File file) throws IOException {
//新增文件
//调用file对象的createNewFile()方法,获得返回值
//如果某个磁盘路径没有对应文件,则可以创建文件,返回值为true
//如果已经存在,则不能创建文件,返回值为false
//exists() 判断某路径下文件是否存在
if (file.exists()){
System.out.println("该文件名已存在!");
}else {
boolean flag = file.createNewFile();
if (flag){
System.out.println("创建文件成功");
}else{
System.out.println("创建文件失败");
}
}
}
(2)查询文件——exists()
public void findFile(File file){
//查询文件名称
if (!file.exists()){
System.out.println("该文件不存在!!!");
}else {
System.out.println(file.getName()); //获取文件名称
System.out.println(file.getAbsoluteFile()); //获取文件绝对路径
}
}
(3)删除文件——delete()
public void delFile(File file){
//删除文件
if(file.exists()){
boolean flag = file.delete();
if (flag){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}else {
System.out.println("该文件不存在!!!");
}
}
1.4 生活中的流
人流、车流 类似于 人或车的集合
1.5 计算机中流
- 定义数据集合【机制:先进先出】
- 流的传递方向:从源数据源传递到目标数据源
1.6 流的指向
- 读入【文件 读入到 程序中】
- 写出【程序 写出到 文件中】
1.7 流的分类
- 按流向分:输出流:OutputStream和Writer
输入流:InputStream和Reader
- 按处理单元分:
字节流:InputStream和OutputStream
字符流:Reader和Writer
2.字节流
2.1 FileInputStream字节输入流实现读取
(1)读取第一位数据
/*
* 利用FileInputStream实现读入
* */
public class Demo002 {
public static void main(String[] args) throws IOException {
//步骤1:实例化FileInputStream对象
FileInputStream fileInputStream = new FileInputStream("/Users/yihongda/Downloads/jojo.txt");
//步骤2:fileInputStream对象的read()方法获取文件中数据的第一个字节的内容对应的ascii码十进制的值【int类型的数值】
int read = fileInputStream.read();
//步骤3:对int类型read强制转换为char类型,则可原样输出
System.out.println((char) read);
fileInputStream.close();
}
}
(2)读取所有数据(一个字节一个字节地读取)
/*
* 利用FileInputStream实现读入
* */
public class Demo002 {
public static void main(String[] args) throws IOException {
//步骤1:实例化FileInputStream对象
FileInputStream fileInputStream = new FileInputStream("/Users/yihongda/Downloads/jojo.txt");
//步骤2:定义临时变量data
int data = 0;
//步骤3:循环遍历文件中的read(),判断如果返回值为-1 则读取结束
while((data = fileInputStream.read()) != -1){
System.out.print((char)data);
}
fileInputStream.close();
}
}
(3)读取所有数据(以字节数组为单位地读取)
一般数组的大小是1024的倍数
public class Demo003 {
public static void main(String[] args) throws IOException {
//步骤1:实例化FileInputStream对象
FileInputStream fileInputStream = new FileInputStream("/Users/yihongda/Downloads/jojo.txt");
//步骤2:定义临时变量
int data = 0;
//步骤3:定义数组
byte[] b = new byte[1024];
//步骤4:循环读取到数组中
while ((data = fileInputStream.read(b)) != -1){
for (int i = 0 ; i < data ; i++){
System.out.print((char)b[i]);
}
}
fileInputStream.close();
}
}
2.2 FileInputStream字节输出流实现写入
(1)读取数据并写入新的文件(复制)
public class Demo004 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("/Users/yihongda/Downloads/jojo.txt");
FileOutputStream fileOutputStream = new FileOutputStream("/Users/yihongda/Downloads/jojo_copy.txt");
//1.定义临时变量
int data = 0;
//2.定义字节数组
byte[]b = new byte[1024];
//3.循环读取输入流fileInputStream并写到输出流fileOutputStream对象中
while ((data = fileInputStream.read(b)) != -1){
fileOutputStream.write(b,0,data);
}
fileOutputStream.close();
fileInputStream.close();
}
}
(2)拓展:本章的代码都是在整个main方法上抛个异常,这其实是不规范的,不利于维护
正常都是以try- catch去抛异常
public class Demo005 {
public static void main(String[] args){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("/Users/yihongda/Downloads/jojo.txt");
fileOutputStream = new FileOutputStream("/Users/yihongda/Downloads/jojo_copy.txt");
//1.定义临时变量
int data = 0;
//2.定义字节数组
byte[]b = new byte[1024];
//3.循环读取输入流fileInputStream并写到输出流fileOutputStream对象中
while ((data = fileInputStream.read(b)) != -1){
fileOutputStream.write(b,0,data);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fileOutputStream != null){
fileOutputStream.close();
}
}catch (IOException e1){
e1.printStackTrace();
}
try {
if (fileInputStream != null){
fileInputStream.close();
}
}catch (IOException e1){
e1.printStackTrace();
}
}
}
}
3.字符流
3.1 FileReader字符输入流实现读取
public class Demo006 {
public static void main(String[] args) {
FileReader fileReader = null;
try {
//步骤1:实例化FileReader对象
fileReader = new FileReader("/Users/yihongda/Downloads/jojo.txt");
//步骤2:定义临时变量data
int data = 0;
//步骤3:定义char数组
char []c = new char[1024];
//步骤4:循环读取数组中的数据
while ((data = fileReader.read(c)) != -1){
//步骤5:输出数组中的数据
for (int i = 0 ; i < data ; i++)
System.out.print(c[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//步骤6:判断fileReader对象不为空【如果流存在的话】
if (fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.2 FileWriter字符输出流实现写入
public class Demo007 {
public static void main(String[] args) {
FileWriter fileWriter = null;
//拓展1:fileWriter = new FileWriter("/Users/yihongda/Downloads/jojo.txt",true);代表每次拼接字符串到文件中
try {
fileWriter = new FileWriter("/Users/yihongda/Downloads/jojo.txt",true);
fileWriter.write("杰洛齐贝林");
} catch (Exception e) {
e.printStackTrace();
} finally {
//拓展2:字符输出流写出时必须close(),否则方法失效
try {
fileWriter.close();
//拓展3:如果不加close(),则可以添加Flush()强制刷新,也可以实现效果
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.缓冲流
4.1 字符流FileReader加BufferedReader实现读取
核心要点:BufferReader类似于在FileReader外层套一层管道
public class Demo008 {
public static void main(String[] args) {
Reader fr = null;
BufferedReader br = null;
try {
//步骤1:实例化FileReader对象
fr = new FileReader("/Users/yihongda/Downloads/jojo.txt");
//步骤2:实例化BufferReader对象,参数是FileReader对象
br = new BufferedReader(fr);
String data = null;
while ((data = br.readLine()) != null){
System.out.println(data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
4.2 字符流FileWriter加BufferedWriter实现写出
public class Demo009 {
public static void main(String[] args) {
Writer fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("/Users/yihongda/Downloads/jojo.txt",true);
bw = new BufferedWriter(fw);
bw.write("jojo");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.复制
5.1 字符流实现复制
字符流+缓冲流实现对一个文件的复制,这里对视频,图片的复制丢失很大
public class Demo010 {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
try {
//步骤1:实例化FileReader对象
fr = new FileReader("/Users/yihongda/图/薇尔莉特.jpeg");
//步骤2:实例化BufferedReader对象,参数是FileReader对象
br = new BufferedReader(fr);
//步骤3:实例化FileWriter对象
fw = new FileWriter("/Users/yihongda/图/薇尔莉特new.jpeg");
//步骤4:实例化BufferedWriter对象,参数是FileWriter对象
bw = new BufferedWriter(fw);
//步骤5:定义临时变量
String data = null;
//步骤6:定义可编辑字符串
StringBuffer sb = new StringBuffer();
//步骤7:循环读取流中的数据
while ((data = br.readLine()) != null){
sb.append(data);
}
//步骤8:读出数据到文件中
bw.write(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
fw.close();
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5.2 复制二进制文件(图片,视频)
使用DataInputStream传输,对文件的丢失损失很小,现在虽然很少直接使用它,但是大部分二进制文件的复制传输类的技术都是用它作为底层技术实现的
public class Demo011 {
public static void main(String[] args) {
FileInputStream fis = null;
DataInputStream dis = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
try {
fis = new FileInputStream("/Users/yihongda/图/薇尔莉特.jpeg");
dis = new DataInputStream(fis);
fos = new FileOutputStream("/Users/yihongda/图/薇尔莉特new.jpeg");
dos = new DataOutputStream(fos);
byte[]b = new byte[1024];
int len = 0;
while ((len = dis.read(b)) != -1){
dos.write(b,0 , len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
fos.close();
dis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}