经验:所有的输出流,当文件不存在时都会创建文件
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("ggc.txt"));
//2.写入数据
//osw.write(97);//写入ASCII
//char[] cs = {'a','b','c','我','爱','你'};
//osw.write(cs);//写入数组
//osw.write(cs,2,3);//写入字符数组,偏移量,长度
//osw.write("abc我爱你");//写入字符串
osw.write("abc我爱你", 3, 3);//写入字符串,偏移量,长度
//3.关闭资源
osw.close();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流对象
OutputStreamWriter ows = new OutputStreamWriter(new FileOutputStream("ggc.txt",true));
//2.写入数据
ows.write("abx我爱你",2,3);//写入字符串,偏移量,长度
//3.关闭资源
ows.close();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.FileNotFoundException;
public class Test03 {
public static void main(String[] args) {
OutputStreamWriter ows = null;
try {
//1.创建流对象 + 指定编码格式
//osw = new OutputStreamWriter(new FileOutputStream("hhy.txt",true),Charset.forName("UTF-8"));
ows = new OutputStreamWriter(new FileOutputStream("ggc.txt",true),"GBK");
//2.写入数据
ows.write("abx我爱你",2,3);//写入字符串,偏移量,长度
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//3.关闭资源
if (ows != null) {
try {
ows.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
经验:所有的输入流,当文件不存在时都会报文件未找到异常 – FileNotFoundException
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test04 {
public static void main(String[] args) throws IOException {
//1.创建流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("ggc.txt"));
//2.读取数据
//isr.read(); -- 读取的是字符的ASCII,读取到文件末尾就返回-1
int read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println(read);
//3.关闭资源
isr.close();
}
}
改进方案1
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test05 {
public static void main(String[] args) throws IOException {
//1.创建数据
InputStreamReader isr = new InputStreamReader(new FileInputStream("ggc.txt"));
//2.读取数据
int read;
while ((read = isr.read()) != -1) {
System.out.println((char)read);
}
//3.关资源
isr.close();
}
}
改进方案2
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test06 {
public static void main(String[] args) throws IOException {
//1.创建流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("ggc.txt"));
//2.读取数据
//len = isr.read(cs) -- 读取cs长度的数据,并把数据存入到字符数组中并返回读取到的有效字符数,如果读取到文件末尾则返回-1
char[] cs = new char[1024];
int len;
while ((len = isr.read(cs)) != -1) {
System.out.println(new String(cs, 0, len));
}
//3.关闭资源
isr.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class Test07 {
public static void main(String[] args){
InputStreamReader isr = null;
try {
//1.创建流对象
//isr = new InputStreamReader(new FileInputStream("ggc.txt"),"GBK");
isr = new InputStreamReader(new FileInputStream("ggc.txt"),Charset.forName("UTF-8"));
//2.读取数据
//len = isr.read(cs) -- 读取cs长度的数据,并把数据存入到字符数组中并返回读取到的有效字符数,如果读取到文件末尾则返回-1
char[] cs = new char[1024];
int len;
while ((len = isr.read(cs)) != -1) {
System.out.println(new String(cs, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//3.关闭资源
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Copy {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("ggc.txt"),"GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("copy.txt"),"GBK");
char[] cs = new char[1024];
int len;
while ((len = isr.read(cs)) != -1) {
osw.write(cs, 0, len);
}
isr.close();
osw.close();
}
}
经验:所有的输出流,当文件不存在时都会创建文件
import java.io.FileWriter;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流的对象
//FileWriter fw = new FileWriter("ggc.txt");
//1.创建流对象 + 末尾追加
FileWriter fw = new FileWriter("ggc.txt",true);
//2.写入数据
fw.write("123我爱你");
//3.关闭资源
fw.close();
}
}
经验:所有的输入流,当文件不存在时都会报文件未找到异常 – FileNotFoundException
import java.io.FileReader;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流的对象
FileReader fs = new FileReader("ggc.txt");
//2.读取数据
char[] cs = new char[1024];
int len;
while ((len = fs.read(cs)) != -1) {
System.out.println(new String(cs, 0, len));
}
//3.关闭资源
fs.close();
}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("IO笔记.txt");
FileWriter fw = new FileWriter("copy.txt");
char[] cs = new char[1024];
int len;
while((len = fr.read(cs)) != -1){
fw.write(cs, 0, len);
}
fr.close();
fw.close();
}
}
经验:所有的输出流,当文件不存在时都会创建文件
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流对象(文件字符输出流 --> 带有缓冲区的字符输出流)
//默认缓冲区大小:8192字符
//BufferedWriter bw = new BufferedWriter(new FileWriter("ggc.txt"));
//1.创建流对象(文件字节输出流 --> 字符输出转换流 --> 带有缓冲区的字符输出流)
//默认缓冲区大小:8192字符
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("ggc.txt"),"GBK"));
//1.创建流对象(文件字符输出流 --> 带有缓冲区的字符输出流) + 末尾追加
//默认缓冲区大小:8192字符
//BufferedWriter bw = new BufferedWriter(new FileWriter("hhy.txt",true));
//1.创建流对象(文件字节输出流 --> 字符输出转换流 --> 带有缓冲区的字符输出流) + 末尾追加
//默认缓冲区大小:8192字符
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("hhy.txt",true),"GBK"));
//1.创建流对象(文件字符输出流 --> 带有缓冲区的字符输出流)
//自定义缓冲区大小:2048字符
BufferedWriter bw = new BufferedWriter(new FileWriter("hhy.txt"),2048);
//2.写入数据
bw.write("123abc我爱你");
//3.关闭资源
bw.close();
}
}
经验:所有的输入流,当文件不存在时都会报文件未找到异常 – FileNotFoundException
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流对象(文件字符输入流 --> 带有缓冲区的字符输入流)
//默认缓冲区大小:8192字符
//BufferedReader br = new BufferedReader(new FileReader("hhy.txt"));
//1.创建流对象(文件字节输入流 --> 字符输入转换流 --> 带有缓冲区的字符输入流)
//默认缓冲区大小:8192字符
//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("hhy.txt"), "GBk"));
//1.创建流对象(文件字符输入流 --> 带有缓冲区的字符输入流)
//自定义缓冲区大小:2048字符
BufferedReader br = new BufferedReader(new FileReader("hhy.txt"),2048);
//2.读取数据
char[] cs = new char[1024];
int len;
while ((len = br.read(cs)) != -1) {
System.out.println(new String(cs, 0, len));
}
//3.关闭资源
br.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy01 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("ggc.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
char[] cs = new char[1024];
int len;
while ((len = br.read(cs)) != -1) {
bw.write(cs, 0, len);
}
br.close();
bw.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy02 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("ggc.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
//br.readLine() -- 读取一行数据,如果读取到文件末尾则返回null
String readLine;
while ((readLine = br.readLine()) != null) {
bw.write(readLine);
bw.newLine();//换行
}
br.close();
bw.close();
}
}
abstract class Reader -- 字符输入流的基类(抽象类)
abstract class Writer -- 字符输出流的基类(抽象类)
class InputStreamReader extends Reader -- 字符输入转换流
class OutputStreamWriter extends Writer - 字符输出转换流
作用:将字节流转换为字符流
class FileReader extends InputStreamReader -- 文件字符输入流
class FileWriter extends OutputStreamWriter - 文件字符输出流
class BufferedReader extends Reader -- 带缓冲区的字符输入流
class BufferedWriter extends Writer -- 带缓冲区的字符输出流
缓冲区大小为:8192字符
可以将程序中的对象写入到文件里,也可以将文件中的对象取出到程序中
1.序列化/钝化:将程序中的对象写入到文件中的过程
2.反序列化/活化:将文件中的对象读取到程序中的过程
3.对象要想通过对象输入流写入到文件中,对象所属的类就必须实现序列化接口(Serializable)
4.序列化接口中没有任何的代码,这种接口称之为标记型接口
5.transient修饰了属性,该属性不会随着对象而写入到文件中
6.static修饰了属性,该属性不会随着对象而写入到文件中(静态属性不属于对象的属性)
class ObjectInputStream --- 对象输入流
class ObjectOutputStream -- 对象输出流
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;
public class Test01 {
public static void main(String[] args) throws FileNotFoundException, IOException {
//1.创建流的对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ggc.txt"));
//2.写入数据
oos.writeInt(100);
oos.writeDouble(123.123);
oos.writeBoolean(true);
oos.writeUTF("abc123我爱你");
oos.writeObject(new Date());
//3.关闭资源
oos.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;
public class Test02 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//1.创建流的对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ggc.txt"));
//2.读取数据
int readInt = ois.readInt();
double readDouble = ois.readDouble();
boolean readBoolean = ois.readBoolean();
String readUTF = ois.readUTF();
Date date = (Date) ois.readObject();
System.out.println(readInt);
System.out.println(readDouble);
System.out.println(readBoolean);
System.out.println(readUTF);
System.out.println(date);
//3.关闭资源
ois.close();
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Test03 {
public static void main(String[] args) throws FileNotFoundException, IOException {
//1.创建流的对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ggc.txt"));
//2.写入数据
oos.writeObject(new User("12345678","123456","卧龙","诸葛亮",1000,300));
oos.writeObject(new User("12345679","123455","大都督","周瑜",1000,350));
oos.writeObject(new User("12345677","123444","江东小霸王","孙策",2000,500));
oos.writeObject(new User("12345676","123333","常山赵子龙","赵云",1000,400));
oos.writeObject(null);
//3.关闭资源
oos.close();
}
}
import java.io.Serializable;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String name;
private String role;
private double hp;
private double mp;
public User() {
}
public User(String username, String password, String name, String role, double hp, double mp) {
this.username = username;
this.password = password;
this.name = name;
this.role = role;
this.hp = hp;
this.mp = mp;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public double getHp() {
return hp;
}
public void setHp(double hp) {
this.hp = hp;
}
public double getMp() {
return mp;
}
public void setMp(double mp) {
this.mp = mp;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", name=" + name + ", role=" + role + ", hp="
+ hp + ", mp=" + mp + "]";
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Test04 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//1.创建流对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ggc.txt"));
//2.读取数据
User user;
while ((user =(User) ois.readObject()) != null) {
System.out.println(user);
}
//3.关闭资源
ois.close();
}
}
注意:内存流是关闭不掉的(因为程序员没有权限关闭流到内存的通道)
应用场景:频繁使用的数据不会存入文件中,因为内存与硬盘交互的越多程序性能越低,所以可以将频繁使用的数据通过内存流存入到内存中
class ByteArrayInputStream --- 内存输入流
class ByteArrayOutputStream -- 内存输出流
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流的对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//2.写入数据
baos.write("123abc".getBytes());
//获取对象中的数据 -- 以字节数组的形式
byte[] byteArray = baos.toByteArray();
System.out.println(new String(byteArray));
//获取对象中的数据 -- 以字符串的形式
String string = baos.toString();
System.out.println(string);
//内存流是关闭不掉的(因为程序员没有权限关闭流到内存的通道)
baos.close();
}
}
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流对象(创建流时,将数据存储到流对象中)
ByteArrayInputStream bais = new ByteArrayInputStream("123abcd".getBytes());
//2.读取数据
byte[] bs = new byte[1024];
int len;
while ((len = bais.read(bs)) != -1) {
System.out.println(new String(bs, 0, len));
}
//内存流是关闭不掉的(因为程序员没有权限关闭流到内存的通道)
bais.close();
}
}
class PrintStream -- 字节打印流 (输出流)
注意:可以将字节流转换为字节打印流
class PrintWriter -- 字符打印流 (输出流)
注意:可以将字节流/字符流转换为字符打印流
注意:系统标准输入、输出、错误输出流的重定向
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Test01 {
public static void main(String[] args) throws FileNotFoundException {
//1.创建流的对象
//PrintStream ps = new PrintStream("ggc.txt");
//1.创建流对象(文件字节输出流 --> 字节打印流) + 末尾追加内容
PrintStream ps = new PrintStream(new FileOutputStream("ggc.txt"));
//2.写入数据
ps.println("123456adc我爱你");
//3.关闭资源
ps.close();
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流的对象
//PrintWriter pw = new PrintWriter("ggc.txt");
//1.创建流对象(文件字节输出流 --> 字符打印流) + 末尾追加内容
//PrintWriter pw = new PrintWriter(new FileOutputStream("ggc.txt",true));
//1.创建流对象(文件字符输出流 --> 字符打印流) + 末尾追加内容
PrintWriter pw = new PrintWriter(new FileWriter("ggc.txt",true));
//2.写入数据
pw.println("123abc牛头人");
//3.关闭资源
pw.close();
}
}
重新定义系统标准的输入流/输出流及错误输出流的方向
重定向 – System.setIn(new FileInputStream(“hhy.txt”));
重定向后(方向:文件->程序)
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) throws IOException {
//重定向
System.setIn(new FileInputStream("ggc.txt"));
Scanner scan = new Scanner(System.in);
String next = scan.next();
System.out.println(next);
scan.close();
}
}
重定向 – System.setOut(new PrintStream(“hhy.txt”));
重定向后(方向:程序->文件)
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class Test04 {
public static void main(String[] args) throws IOException {
//重定向
System.setOut(new PrintStream(new FileOutputStream("ggc.txt",true)));
PrintStream out = System.out;
out.println("小赵小可爱,皇冠给你带~~~");
}
}
重定向 – System.setErr(new PrintStream(“hhy.txt”));
重定向后(方向:程序->文件)
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class Test05 {
public static void main(String[] args) throws IOException {
//重定向
System.setErr(new PrintStream(new FileOutputStream("ggc.txt",true)));
PrintStream err = System.err;
err.println("小赵小可爱,皇冠给你带~~~");
}
}
该类认为文件是一个大型的byte数组,底层由隐藏的指针(下标),指针默认为0(可以设置指针的位置),
可以从指针的位置开始读取或写入(说明该流有两个方向)
r -- 读 rw - 读写
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流的对象
RandomAccessFile r = new RandomAccessFile("ggc.txt","rw");
//2.写入数据
r.write("123abc我爱你".getBytes());
//3.关闭资源
r.close();
}
}
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流的对象
File file = new File("ggc.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
//设置指针位置
raf.seek(file.length());//将指针设置到文件末尾
//2.读取数据
raf.write("123abcd我爱你哟".getBytes());
//3.关闭资源
raf.close();
}
}
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test03 {
public static void main(String[] args) throws IOException {
//1.创建流对象
RandomAccessFile raf = new RandomAccessFile("ggc.txt", "r");
//2.读取数据
byte[] b = new byte[1024];
int len;
while ((len = raf.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
//3.关闭资源
raf.close();
}
}
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test04 {
public static void main(String[] args) throws IOException {
//1.创建流的对象
RandomAccessFile raf = new RandomAccessFile("ggc.txt", "r");
//设置文件指针
raf.seek(3);
//2.读取数据
byte[] b = new byte[1024];
int len;
while ((len = raf.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
//3.关闭资源
raf.close();
}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("ggc.txt");
FileWriter fw = new FileWriter("copy.txt");
char[] cs = new char[1024];
int len;
while ((len = fr.read(cs)) != -1) {
fw.write(cs, 0, len);
}
fr.close();
fw.close();
}
}