b站闫海玉老师视频Java程序设计_哔哩哔哩_bilibili
学习笔记
目录
1.FileOutputStream and FileInputStream
3.缓冲流BufferedOutputStream and BufferedInputStream
2.缓冲流PrintWriter and BufferedReader
一、字符流
1.FileOutputStream and FileInputStream
(1).使用FileOutStream输出数据到文件
package IO;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 文件字节流文件读写
* FileInputStream
* FileOutputStream
* 实现从指定文件中读取字节和将字节写入指定文件
* @author 惊蛰
*
*/
public class TestStream {
public static void main(String[] args) {
//使用输出流写入文件
try {
//1.创建文件输出流
FileOutputStream fount = new FileOutputStream("e:\\temp.txt",true);//表示追加到文档末尾
//2.准备写入数据
String str = "everyone!";
//3.转换为字节
byte[] buf = str.getBytes();
//4.写入文件
fount.write(buf);
//5.关闭输入流
fount.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
执行两次:
(2).使用FileInpuStream从文件读入数据
package IO;
/**
* 使用fin.read,fount.write读写数据前
* 要先准备字节缓冲区:byte[] buf = new byte[长度]
*/
import java.io.FileInputStream;
public class TextFileInputStream {
public static void main(String[] args) {
try {
//1.创建文件输入流
FileInputStream fin = new FileInputStream("e:\\temp.txt");
//创建字节数组
byte[] buf = new byte[1024];
String instr = "";//接受字节转换为字符串
//2.使用字节数组,接收从文件中读取的字节
int length = fin.read(buf);//fin.read();无参,只读取一个数据
//将字节转换为字符串
instr = new String(buf,0,length);
System.out.println(instr);
//3.关闭输入流
fin.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
2.复制文件
package IO;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 复制机制
* 1.循环的目的是不清源文件的大小
* 2.在循环读写过程中不关闭流,会持续对文件进行读写
* @author 惊蛰
*
*/
public class TestCopy {
public static void main(String[] args) {
try {
//创建输出流和输出流
FileOutputStream fout = new FileOutputStream("e:\\提琴2.jpg");
FileInputStream fin = new FileInputStream("e:\\image\\提琴.jpg");
//准备字节缓冲区
byte[] buf = new byte[1024];//8位
//循环读写
int length = 0;
while((length = fin.read(buf))!=-1)//全部读完返回-1
{
fout.write(buf,0,length);
}
//关闭流
fout.close();
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.缓冲流BufferedOutputStream and BufferedInputStream
package IO;
import java.io.*;
public class TestBuffered {
public static void main(String[] args) {
try {
//1.创建输入流,输出流
FileOutputStream fout = new FileOutputStream("e:\\提琴2.jpg");
FileInputStream fin = new FileInputStream("e:\\image\\提琴.jpg");
//2.根据字节流创建缓冲流
BufferedOutputStream bufout = new BufferedOutputStream(fout);
BufferedInputStream bufin = new BufferedInputStream(fin);
//3.循环读写
int length = 0;
while((length = bufin.read())!=-1) {
bufout.write(length);
}
//关闭流
bufout.close();
bufin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、字符流
1.FileWriter and FileReader
(1).使用FileWriter写入数据
package IO2;
import java.io.FileWriter;
public class TestRandW {
public static void main(String[] args) {
try {
//1.创建文件字符输出流
FileWriter fw = new FileWriter("e:\\temp3.txt");
//准备写入文件的字符
int id = 1;
String name = "酸梅汤";
String category = "饮料";
int store = 100;
String descriprion = "营养健康";
String str_id = Integer.toString(id);
String str_store = Integer.toString(store);
//2.写文件,把字符串数据写到输出流对象相应的文件
fw.write(str_id);
fw.write(name);
fw.write(category);
fw.write(str_store);
fw.write(descriprion);
//3.关闭流
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
(2).使用FileReader读入数据
package IO2;
/**
* 可以读写字符
* 可以读写字符数组
* 可以读写字符串
* 可以读写整数
*/
import java.io.FileReader;
public class TestRandR {
public static void main(String[] args) {
try {
//1.创建文件字符输入流
FileReader fr = new FileReader("e:\\temp3.txt");
//准备字符数组
char[] buf = new char[1000];
//2.读取文件内容到buf,返回长度
int length = fr.read(buf);
//3.关闭流
fr.close();
//将字符数组转换为字符串
String str = new String(buf,0,length);
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.缓冲流PrintWriter and BufferedReader
辅助:products类
package IO2;
/**
* 商品类
* @author 惊蛰
*
*/
public class products {
private int id;//编号
private int stock;//库存
private String name;//名称
private String category;//种类
private String description;//描述
private double price;//价格
private int state;//状态
public products() {
super();
}
/**
* 有参的构造方法,用于初始化
* @param id
* @param stock
* @param name
* @param category
* @param description
* @param price
* @param state
*/
public products(int id, String name, String category, double price, int stock, String description, int state) {
super();
this.id = id;
this.stock = stock;
this.name = name;
this.category = category;
this.description = description;
this.price = price;
this.state = state;
}
/**
* 获取编号
* @return
*/
public int getId() {
return id;
}
/**
* 设置编号
* @param id
*/
public void setId(int id) {
this.id = id;
}
/**
* 获取库存
* @return
*/
public int getStock() {
return stock;
}
/**
* 设置库存
* @param stock
*/
public void setStock(int stock) {
this.stock = stock;
}
/**
* 获取商品名
* @return
*/
public String getName() {
return name;
}
/**
* 设置商品名
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取商品种类
* @return
*/
public String getCategory() {
return category;
}
/**
* 设置商品种类
* @param category
*/
public void setCategory(String category) {
this.category = category;
}
/**
* 获取商品描述
* @return
*/
public String getDescription() {
return description;
}
/**
* 设置商品描述
* @param description
*/
public void setDescription(String description) {
this.description = description;
}
/**
* 获取商品价格
* @return
*/
public double getPrice() {
return price;
}
/**
* 设置商品价格
* @param price
*/
public void setPrice(double price) {
this.price = price;
}
/**
* 获取商品状态
* @return
*/
public int getState() {
return state;
}
/**
* 设置商品状态
* @param state
*/
public void setState(int state) {
this.state = state;
}
/**
* 打印全部商品信息
*/
@Override
public String toString() {
//int id, String name, String category, double price, int stock, String description, int state
return id+","+name+","+category+","+price+","+stock+","+description+","+state;
}
}
(1).使用PrintWriter做输出缓冲区
package IO2;
import java.io.*;
public class TestBuffered {
public static void main(String[] args) {
try {
//1.创建字符文件输出流
FileWriter fw = new FileWriter("e:\\Produt.txt");
//2.创建数据缓冲流
PrintWriter pw = new PrintWriter(fw);
// int id =1;
// String name = "酸梅汤";
// String category = "饮料";
// double price = 3.4;
// int store = 100;
// String description = "营养健康";
products p1 = new products(1,"酸梅汤","饮料",3.4,100,"营养健康", 1);
//3.通过缓冲写入文件
// pw.print(id);
// pw.print(name);
// pw.print(category);
// pw.print(price);
// pw.print(store);
// pw.print(description);
pw.println(p1.toString());//输出一行
//3.关闭流
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
(2).使用BufferedReader做读入缓冲区
package IO2;
import java.io.*;
public class TestBufferedRead {
public static void main(String[] args) {
try {
//1.创建字符输入流
FileReader fr = new FileReader("e:\\Produt.txt");
//2.创建缓冲流
BufferedReader br = new BufferedReader(fr);
//3.读入文件
String line = br.readLine();//读一行
//4.关闭流
br.close();
System.out.println(line);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.商品类存读
(1).使用集合存入excel文件
package IO2;
import java.io.*;
import java.util.ArrayList;
public class ProductFile {
public static void main(String[] args) {
//准备数据
ArrayList<products> list = new ArrayList<products>();
list.add(new products(1,"酸梅汤","饮料",3.4,100,"营养健康",1));
list.add(new products(2,"馒头","主食",3.4,100,"营养健康",1));
list.add(new products(3,"米饭","主食",3.4,100,"营养健康",1));
list.add(new products(4,"葡萄","水果",3.4,100,"营养健康",1));
list.add(new products(5,"玉米","主食",3.4,100,"营养健康",1));
//写文件
try {
//1.创建文件字符输出流
FileWriter fw = new FileWriter("e:\\Product.csv");//存入excel
//2.创建数据缓冲区
PrintWriter pw = new PrintWriter(fw);
//3.输出数据
for(products p1 : list) {
pw.println(p1);
}
//4.关闭流,写入结束必须关闭流,不然会写入失败
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
(2).将excel文件读入集合
package IO2;
import java.io.*;
import java.util.ArrayList;
public class ProductFIleR {
public static void main(String[] args) {
//准备数据
ArrayList<products> list = new ArrayList<products>();
try {
//1.创建文件字符输入流
FileReader fr = new FileReader("e:\\Product.csv");
//2.创建缓冲流
BufferedReader br = new BufferedReader(fr);
//3.读取数据
while(true) {
String line = br.readLine();
//System.out.println(line);
if(line == null) {
break;
}
//把读回的行转为对象,存入集合中
String[] buf = line.split(",");
int id = Integer.parseInt(buf[0]);
String name = buf[1];
String category = buf[2];
double price = Double.parseDouble(buf[3]);
int store =Integer.parseInt(buf[4]);
String description = buf[5];
int state = Integer.parseInt(buf[6]);
//组合对象
products p = new products(id,name,category,price,store,description,state);
//把对象存入集合
list.add(p);
}
//关闭流
br.close();
//遍历集合信息
for(products p1 : list) {
System.out.println(p1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
三、文件相关类
File类被定义为“文件和目录路径名的抽象表示”
这是因为FIle类既可以表示“文件”也可以表示“目录”,他们通过对应的路径来描述
通过构造函数创建一个File类对象,则该对象就是指定文件的引用,可以通过该对象对文件进行操作
package IO3;
/**
* File类没有提供文件读写,必须通过流进行文件读写
*/
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import IO2.products;
public class TestFile {
public static void main(String[] args) {
File f = new File("e:\\文件\\百度.txt");
//如果文件不存在,则新建文件
if(!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//file使用缓冲流读入文件
try {
//准备数据
ArrayList<products> list = new ArrayList<products>();
list.add(new products(1,"酸梅汤","饮料",3.4,100,"营养健康",1));
list.add(new products(2,"馒头","主食",3.4,100,"营养健康",1));
list.add(new products(3,"米饭","主食",3.4,100,"营养健康",1));
list.add(new products(4,"葡萄","水果",3.4,100,"营养健康",1));
list.add(new products(5,"玉米","主食",3.4,100,"营养健康",1));
//创建缓冲流
PrintWriter pw = new PrintWriter(f);
//输出数据
for(products p : list) {
pw.println(p);
}
//关闭流
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("是文件吗"+f.isFile());
System.out.println("是目录吗"+f.isDirectory());
//查看目录中的文件列表
System.out.println(Arrays.toString(f.list()));
System.out.println("上一层目录"+f.getParent());
System.out.println("名称"+f.getName());
System.out.println("路径"+f.getPath());
System.out.println("绝对路径"+f.getAbsolutePath());
System.out.println("最后修改时间"+new Date(f.lastModified()));
System.out.println("文件大小"+f.length()+"字节");
System.out.println("是否可读"+f.canRead());
System.out.println("是否可写"+f.canWrite());
}
}