建立连接
选择流
操作(读取、写入)
释放资源
新建lib文件夹 -> 拷贝jar包到lib文件夹 -> jar包 add as Library
首先和文件建立联系
选择流操作,读取(以指定的字符集编码)文件内容到字符串中
public static void main(String[] args) throws IOException {
//和文件建立联系
File file = FileUtils.getFile("test.txt");
//选择流操作,读取(以指定的字符集编码)文件内容到字符串中
String str = FileUtils.readFileToString(file,"GBK");
//打印出读取到的字符串内容
System.out.println(str);//gp-高沛
}
和文件建立联系,创建文件
写文件:指定文件,内容,字符集
public static void main(String[] args) throws IOException {
//和文件建立联系,创建文件
File file = new File("a.txt");
//写文件:指定文件,内容,字符集
FileUtils.writeStringToFile(file,"你好呀","GBK");
}
(原始方法)指定读取的文件、指定写入的文件、读取出文件内容、将读取出的内容写入文件
public static void main(String[] args) throws IOException {
//指定读取的文件
File source = FileUtils.getFile("test.txt");
//指定写入的文件
File dest = FileUtils.getFile("c.txt");
//读取出文件内容
String str = FileUtils.readFileToString(source,"GBK");
//将读取出的内容写入文件
FileUtils.writeStringToFile(dest,str,"GBK");
}
(工具方法)public static void copyFile(srcFile, destFile)
public static void main(String[] args) throws IOException {
FileUtils.copyFile(FileUtils.getFile("a.txt"),FileUtils.getFile("b.txt"));
}
public static void main(String[] args) throws IOException {
//拷贝图片到指定目录下
FileUtils.copyFile(FileUtils.getFile("d.png"),FileUtils.getFile("out\\e.png"));
}
抽象类:InputStream Reader
.文件节点类:FileInputStream FileReader
单个字节读取
public static void main(String[] args) throws Exception {
//和文件建立连接
File file = new File("test.txt");
//选择流
InputStream in = new FileInputStream(file);
//读取
int i ;
while ((i = in.read()) != -1 ){ //i = -1 代表文件读取结束
System.out.println((char) i);
}
//关闭流,释放资源
in.close();
}
批量读取
public static void main(String[] args) throws IOException {
//和文件建立连接
File file = new File("a.txt");
// 选择流
InputStream is = new FileInputStream(file);
// 读取 一些
byte[] car = new byte[1024];
// 读取多个数据放入 byte[] 返回实际读取的数据个数,当返回的实际个数是-1表示文件读取结束
int realLen /*= is.read(car)*/;
while((realLen = is.read(car))!=-1){
String str = new String(car,0,realLen,"UTF-8");
System.out.println(str);//你好呀
// 再次带回去
// realLen = is.read(car);
}
// 释放资源
is.close();
}
单个字节读取
public static void main(String[] args) throws IOException {
//建立连接
File file = new File("a.txt");
//选择流
Reader r = new FileReader(file);
int i ;
while((i = r.read())!=-1){
System.out.println((char)i);
}
}
批量读取
public static void main(String[] args) throws IOException {
//建立连接
File file = new File("a.txt");
//选择流
Reader r = new FileReader(file);
//读取多个数据
char[] c = new char[20];
int len;//返回读取数据的个数,当返回的实际个数是-1表示文件读取结束
while ( (len = r.read(c))!= -1){//读取到的字符不为-1,说明文件内容没有结束,继续读取
System.out.println(Arrays.toString(c));//[, 你, 好, 呀,
抽象类:OutputStream Writer
文件节点类: FileOutputStream FileWriter
public static void main(String[] args) throws IOException {
//建立连接
File file = new File("c.txt");
//选择流
OutputStream os = new FileOutputStream(file);
//写入
os.write(99);
os.write(new byte[]{100,101,102});//字节数组
String str = "\n高沛啦啦啦";
byte[] bs ;
bs = str.getBytes();//
os.write(bs);
//刷新
os.flush();
//关闭
os.close();
}
public static void main(String[] args) throws IOException {
//建立连接
File file = new File("b.txt");
//选择流
Writer w = new FileWriter(file);
//操作 写入
w.write(97);//写入Unicode码
w.write("hahaha");//字符串
w.write(new char[]{'d','e','f'});//字符数组
//刷新
w.flush();
//关闭资源
w.close();
}
//读 输入流
FileReader fr = new FileReader("aaa.txt");
// 写 输出流
FileWriter fw = new FileWriter("acopy.txt");
char[] cs = new char[1024];
int len;
// 循环读取
while((len=fr.read(cs))!=-1){
fw.write(cs,0,len);
}
// 刷新和释放资源
fw.flush();
fr.close();
fw.close();
}
缓冲流:BufferedInputStream和 BufferedReader
public static void main(String[] args)throws Exception {
//建立联系
File file = new File("aaa.txt");
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] bs = new byte[1024];
int len ;
while((len=bis.read(bs))!=-1){
System.out.println(new String(bs,0,len,"UTF-8"));
}
//关闭
bis.close();
is.close();
}
File file = new File("aaa.txt");
Reader r = new FileReader(file);
BufferedReader br = new BufferedReader(r);
String str;
while((str = br.readLine())!=null){
System.out.println(str);
}
br.close();
r.close();
}
转换处理流: InputStreamReader
public static void main(String[] args) throws Exception{
// File
File file = new File("aaa.txt");
// 字节流 麻烦,但是可以解决问题
FileInputStream fs = new FileInputStream(file);
// 将指定字节流转成字符流
InputStreamReader fisr = new InputStreamReader(fs,"GBK");
// 变成字符流 简单一点
BufferedReader r = new BufferedReader(fisr);
char[] cs = new char[1024];
int len = r.read(cs);
String str = new String(cs);
System.out.println(str);
r.close();
fisr.close();
fs.close();
}
数据处理流:DataInputStream
public static void main(String[] args)throws Exception {
int i = 980;
// 文件
File file = new File("data.txt");
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
// 写出一个整数
dos.writeInt(i);
dos.flush();
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream(file));
int a = dis.readInt();
System.out.println(a*2);
dis.close();
}
对象处理流(反序列化):ObjectInputStream
public class Person implements Serializable {...}
ObjectOutputStream(序列化)
public static void main(String[] args)throws Exception {
// 对象
Person p = new Person("郑辉华",20,true);
// 文件
File file = new File("objFile.txt");
// 原始的输出流
OutputStream os = new FileOutputStream(file);
// 处理流
ObjectOutputStream ots = new ObjectOutputStream(os);
ots.writeObject(p);
ots.flush();
ots.close();
os.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Object obj = ois.readObject();
System.out.println(obj);
ois.close();
}
ew FileOutputStream(file);
// 处理流
ObjectOutputStream ots = new ObjectOutputStream(os);
ots.writeObject(p);
ots.flush();
ots.close();
os.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Object obj = ois.readObject();
System.out.println(obj);
ois.close();
}