/**
* FileWriter 是一个写入字符文件的类 extends OutputStreamWriter
* 此类的构造方法默认字符和默认字节缓冲区的大小都是可接受的;文件是否可用或是可以被创建的取决于平台,如果是开发文件写入,在这种情况下,此类中的构造方法将失败
* 构造方法:
* public FileWriter(String name)
* 参数是文件路径,(例如:new FileWriter("D:\\a.txt"))
* 路径说明: 绝对路径->从盘符开始,直到文件名完整路径("D:\\abc\\...\\a.txt")
* 相对路径->相对当前文件夹开始("a.txt")
* 使用: 1.根据指定的文件路径名找到对应的文件,如果文件不存在,将会自动创建->底层会(new FileOutputStream(String name))
* 2.它的方法都是extends OutputStreamWriter --> (append, append, append, write, write)
* flush和close方法说明:当写入数据的是先把数据写到字符缓冲区,只有flush的时候才会写入文件中;当调用close的时候,在关闭流之前会先调用flush,所以说,在非长时期flush可以省略直接close
* FileWriter的五种重载方法:
* void write(String name) 写入一个字符串数据
* void write(String name,int index,int len) 写入一个字符串中的一部分,从哪里开始,写几个.
* void write(int或char) 这里写int类型好处是即可以写char类型的数据,也可以写char对应的int类型的值.'a'->97
* void write(char[] chs) 写一个字符数组数据 void write(char[] chs,int index,int len) 写一个字符数组的一部分,index开始,len结束
* ----------------------------------------------------------------------------------------------------------------
* FileReader也是一个类,是用来读取文件的,是一种字符输出流 extends InputStreamReader
* FileReader用于读取字符流,要读取字节流要考虑使用FileInputStream
* 构造方法跟FileWrider一样,就是一个写一个读
* 注意的是:FileReader对象fr.read是读取单个字符返回值是int
* 读取单个字符:
* int line = 0;
* while((line = fr.read()) != -1) //当读取返回不等于-1则输出
* 读取字符数组:
* char[] buf = new char[num];
* int len = 0;
* while((line=fr.read(buf)) != -1){
* String str = new String(buf,0,len);
* }
*/
public class FileWriteDemo {
public static void main(String[] args) throws IOException {
methodFileWriterFunction();
methidFileReaderFunction();
}
private static void methidFileReaderFunction() throws IOException {
FileReader fr = new FileReader("a.txt");
char[] buf = new char[20];
int len = 0;
while((len = fr.read(buf)) != -1){
String string = new String(buf,0,len);
System.out.println(string);//abc m lov hg
}
}
private static void methodFileWriterFunction() throws IOException {
FileWriter writer = new FileWriter("a.txt");
writer.write("abc");
writer.write("\t");
writer.append('m');
writer.write("\t");
writer.write("iloveyou", 1, 3);
writer.write("\t");
char[] ch = { 'h', 'g' };
writer.write(ch);
writer.close();
/*
* abc m lov hg
*/
}
}
/**
* BufferedWrider也是一个输出流,但它的底层有一个默认是8192的char[]字符数组,当做缓冲使用,不用单独new char[]数组了;
* 使用:
* 1.首先new一个普通的FileWriter,2.将这个普通FileWriter包装为字符缓冲BufferedWriter
* 自己的方法:
* newLine();写入一个行分隔符;
* 其他跟FileWriter方法一样.
* -------------------------------------------------------------------------------------
* BufferedReader也是一个输入流,跟FileReader差不多,也是读取文件的,底层也有一个8192的char[]字符数组,同样作为缓冲区
* 使用:如下.
* 自己的方法:
* String readLine(); 读取一行
*/
public class BufferedWriteDemo {
public static void main(String[] args) throws IOException {
methodBufWriter();
methodBufReader();
}
private static void methodBufReader() throws IOException {
FileReader reader = new FileReader("a.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
// char[] buf = new char[20];
// int len = 0;
// while((len = bufferedReader.read(buf)) != -1){
// System.out.println(new String(buf,0,len));
// }
String line = null;
while((line = bufferedReader.readLine()) != null){
System.out.println(line);
}
}
private static void methodBufWriter() throws IOException {
FileWriter writer = new FileWriter("a.txt");
BufferedWriter bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write("abc");
bufferedWriter.newLine();
bufferedWriter.write("love", 3, 1);
bufferedWriter.close();//abc e
}
}
//BufferedWriter底层声明的8192字符缓冲区
class BufferedWriterCharArr{
// private static int defaultCharBufferSize = 8192;
// public BufferedWriter(Writer out, int sz) {
// super(out);
// if (sz <= 0)
// throw new IllegalArgumentException("Buffer size <= 0");
// this.out = out;
// cb = new char[sz];
// nChars = sz;
// nextChar = 0;
//
// lineSeparator = java.security.AccessController.doPrivileged(
// new sun.security.action.GetPropertyAction("line.separator"));
// }
}
/**
* OutputStream 是字节输出流:把内存中的数据,以字节的方式,写入到文件中保存
* java.io.OutputStream 是所有字节输入流的父类
* 读、写的过程:java程序-->JVM-->操作系统-->方法-->读写
* 成员方法:
* abstract void write(int b) 写入一个字节
* void write(byte[] b) 写入字节数组
* void write(byte[] b, int off, int len) 写入字节数组,off是开始的索引,len写几个
* void close() 关闭此输出流并释放与此流有关的所有系统资源。
* void flush() 刷新此输出流并强制写出所有缓冲的输出字节。
* --------------------------------------------------------------------
* java.io.FileOutputStream:文件字节输出 extends OutputStream
* FileOutputStream(File file) 创建一个指定File对象表示的文件中写入数据的文件输出流.
* FileOutputStream(String name) 创建一个文件路径
* 构造方法:
* 1.如果构造方法中传递的目的地不存在,则会创建一个文件;如果目的地已经存在,会覆盖
* 续写(追加写)和换行
* 换行:可以使用换行符号
* Windows:\r\n
* linux:/n
* mac:/r
* -------------------------------------------------------------------
* java.io.BufferedOutputStream:字节缓冲输出流 extends OutputStream
* 有继承自父类OutputStream的成员方法
* abstract void write(int b) 写入一个字节
* void write(byte[] b) 写入字节数组
* void write(byte[] b, int off, int len) 写入字节数组,off是开始的索引,len写几个
* void close() 关闭此输出流并释放与此流有关的所有系统资源。
* void flush() 刷新此输出流并强制写出所有缓冲的输出字节。
* 构造方法:
* BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
* BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
* 参数:
* int size:指定的底层缓冲区(字节数组)的大小
* OutputStream out:字节输出流
* 我们可以传入FileOutputStream,就会给FileOutputStream增加一个缓冲区,提高FileOutputStream读取数据的效率
* ====================================================================
* java.io.InputStream:是所有字节输入流的父类
* 共性的成员方法:
* int read():读取一个字节并返回,没有字节返回-1.
* int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。
* void close() 关闭此输入流并释放与该流关联的所有系统资源。
* --------------------------------------------------------------------
* java.io.FileInputStream 文件字节输入流 extends InputStream
* 构造方法:
* FileInputStream(File file) 字节输入流对象
* FileInputStream(String name) 文件路径
* 读取数据read(byte[] byte) 返回int:读取一定的字节数,并存到字节数组中
* 明确两件事件:
* 1.方法返回值int是什么:是每次读取的有效字节个数
* 2.byte[]作用是起到缓冲作用
* ---------------------------------------------------------------------
* java.io.BufferedInputStream 字节缓冲流 extends InputStream
* 继续自父类InputStream中的成员方法
* int read():读取一个字节并返回,没有字节返回-1.
* int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。
* void close() 关闭此输入流并释放与该流关联的所有系统资源。
* 使用如下
*/
public class OutputStreamDemo {
public static void main(String[] args) throws IOException {
methodFileOutputStream();
methodBufFileOutStream();
methodFileInputStream();
methodBufInputStream();
}
private static void methodBufInputStream() throws IOException {
FileInputStream fis = new FileInputStream("b.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] bytes = new byte[1024];
int len = 0;
while((len = bis.read(bytes)) != -1){
System.out.println(new String(bytes,0,len));//这是字符缓冲流
}
}
private static void methodBufFileOutStream() throws IOException {
FileOutputStream fos = new FileOutputStream("b.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("这是字符缓冲流".getBytes());
bos.close();
}
private static void methodFileInputStream() throws IOException {
FileInputStream inputStream = new FileInputStream("b.txt");
byte[] bytes = new byte[10];
int len = 0;
while((len = inputStream.read(bytes)) != -1){
System.out.println(new String(bytes,0,len));
}
}
private static void methodFileOutputStream() throws FileNotFoundException, IOException {
FileOutputStream outputStream = new FileOutputStream("b.txt");
byte[] bytes = new byte[]{'a','b','3'};
outputStream.write(bytes);
bytes = "你好".getBytes();
outputStream.close();
}
}
/**
* java.io.InputStreamReader extends Reader 字符流
* 作用:是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。
*
* 继承自父类Reader中的成员方法
* int read() 读取单个字符。
* int read(char[] cbuf) 将字符读入数组。
* abstract void close() 关闭该流并释放与之关联的所有资源。 *
* 构造方法:
* InputStreamReader(InputStream in) 创建一个使用默认字符集的 InputStreamReader。
* InputStreamReader(InputStream in, String charsetName) 创建使用指定字符集的 InputStreamReader。
* 参数:
* InputStream in:字节输入流,用来读取文件中保存的字节
* String charsetName:编码表的名称,可以是"GBK","gbk","UTF-8","utf-8",...不区分大小写;不指定默认为系统编码(GBK) *
* 使用步骤:
* 1.创建字节输入流FileInputStream对象,构造方法中绑定要读取的数据源
* 2.创建转换流InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
* 3.使用InputStreamReader中的方法read,读取文件
* 4.释放资源 *
* 注意事项:指定的编码表名称要和读取的文件编码相同,否则会发生乱码
*/
public class Demo02InputStreamReader {
public static void main(String[] args) throws IOException {
read_gbk();
read_utf8();
}
/*
* 使用InputStreamReader读取UTF-8格式的文件
*/
private static void read_utf8() throws IOException {
//1.创建字节输入流FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("utf-8.txt");
//2.创建转换流InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//你好
//InputStreamReader isr = new InputStreamReader(fis,"GBK");//浣犲ソ
//3.使用InputStreamReader中的方法read,读取文件
char[] cs = new char[1024];
int len = 0;
while((len = isr.read(cs))!=-1){
System.out.println(new String(cs, 0, len));
}
//4.释放资源
isr.close();
}
/*
* 使用InputStreamReader读取GBK格式的文件
*/
private static void read_gbk() throws IOException {
//1.创建字节输入流FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("gbk.txt");
//2.创建转换流InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
InputStreamReader isr = new InputStreamReader(fis,"GBK");//你好
//InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//???
//3.使用InputStreamReader中的方法read,读取文件
int len = 0;
while((len = isr.read())!=-1){
System.out.println((char)len);
}
//4.释放资源
isr.close();
}
}
/**
* java.io.OutputStreamWriter extends Writer
* 作用:是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。
*
* 继承自父类Writer的成员方法
* abstract void close() 关闭此流,但要先刷新它。
* abstract void flush() 刷新该流的缓冲。
* void write(char[] cbuf) 写入字符数组。
* abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。
* void write(int c) 写入单个字符。
* void write(String str) 写入字符串。
* void write(String str, int off, int len) 写入字符串的某一部分。 *
* 构造方法:
* OutputStreamWriter(OutputStream out) 创建使用默认字符编码的 OutputStreamWriter。
* OutputStreamWriter(OutputStream out, String charsetName) 创建使用指定字符集的 OutputStreamWriter。
* 参数:
* OutputStream out:字节输出流,用于将转换后的字节写入到文件中
* String charsetName:编码表的名称,可以是"GBK","gbk","UTF-8","utf-8",...不区分大小写;不指定默认为系统编码(GBK) *
* 使用步骤:
* 1.创建字节输出流对象,构造方法中绑定要输出的目的地
* 2.创建转换流OutputStreamWriter,构造方法中传递字节输出流对象和指定的编码表名称
* 3.使用OutputStreamWriter中的方法write,把数据写入到内存缓冲区中
* 4.使用OutputStreamWriter中的方法flush或者close把内存缓冲区中的数据,刷新到文件中
* 5.释放资源
*/
public class OutputStreamWriterDemo {
public static void main(String[] args) throws IOException {
write_utf8();
write_gbk();
}
/*
* 使用OutputStreamWriter写UTF-8格式的文件
*/
private static void write_utf8() throws IOException {
//1.创建字节输出流对象,构造方法中绑定要输出的目的地
FileOutputStream fos = new FileOutputStream("utf-8.txt");
//2.创建转换流OutputStreamWriter,构造方法中传递字节输出流对象和指定的编码表名称
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
//3.使用OutputStreamWriter中的方法write,把数据写入到内存缓冲区中
osw.write("你好");
//4.使用OutputStreamWriter中的方法flush或者close把内存缓冲区中的数据,刷新到文件中
//osw.flush();
//5.释放资源
osw.close();
}
/*
* 使用OutputStreamWriter写GBK格式的文件
*/
private static void write_gbk() throws IOException {
//1.创建字节输出流对象,构造方法中绑定要输出的目的地
FileOutputStream fos = new FileOutputStream("gbk.txt");
//2.创建转换流OutputStreamWriter,构造方法中传递字节输出流对象和指定的编码表名称
//OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
OutputStreamWriter osw = new OutputStreamWriter(fos);//不指定编码表,默认使用GBK
//3.使用OutputStreamWriter中的方法write,把数据写入到内存缓冲区中
osw.write("你好");
//4.使用OutputStreamWriter中的方法flush或者close把内存缓冲区中的数据,刷新到文件中
//osw.flush();
//5.释放资源
osw.close();
}
}
/**
* java.io.ObjectOutputStream extends OutputStream 字节流
* 作用:把对象以流的方式,写入到文件中保存,称之为序列化
*
* 构造方法:
* ObjectOutputStream(OutputStream out) 创建写入指定 OutputStream 的 ObjectOutputStream。
* 参数:
* OutputStream out:字节输出流,可以传入FileOutputStream
*
* 成员方法:
* void writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。
*
* 使用步骤:
* 1.创建字节输出流FileOutputStream对象,构造方法绑定要写的目的地
* 2.创建对象的序列化流ObjectOutputStream对象,构造方法中传递FileOutputStream对象
* 3.调用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中
* 4.释放资源
*/
public class Demo01ObjectOutputStream {
public static void main(String[] args) throws IOException {
//1.创建字节输出流FileOutputStream对象,构造方法绑定要写的目的地
FileOutputStream fos = new FileOutputStream("person.txt");
//2.创建对象的序列化流ObjectOutputStream对象,构造方法中传递FileOutputStream对象
ObjectOutputStream oos = new ObjectOutputStream(fos);
//3.调用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中
oos.writeObject(new Person("柳岩",18));
//4.释放资源
oos.close();
}
}
/**
* java.io.ObjectInputStream extends InputStream 字节输入流
* 作用:把文件中保存的对象,以流的方式读取出来使用,称之为对象的反序列化
*
* 构造方法:
* ObjectInputStream(InputStream in) 创建从指定 InputStream 读取的 ObjectInputStream。
* 参数:
* InputStream in:字节输入流,可以使用FileInputStream
*
* 成员方法:
* Object readObject() 从 ObjectInputStream 读取对象。
*
* 使用步骤:
* 1.创建字节输入流FileInputStream对象,构造方法中绑定要读取的数据源
* 2.创建对象反序列流ObjectInputStream对象,构造方法中传递FileInputStream对象
* 3.使用ObjectInputStream对象中的方法readObject,读取文件
* 4.释放资源
*
* 注意:
* readObject方法声明了ClassNotFoundException,class文件找不到异常
* 如果反序列的对象,没有对应的class文件,则抛出此异常
*/
public class Demo02ObjectInputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1.创建字节输入流FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("person.txt");
//2.创建对象反序列流ObjectInputStream对象,构造方法中传递FileInputStream对象
ObjectInputStream ois = new ObjectInputStream(fis);
//3.使用ObjectInputStream对象中的方法readObject,读取文件
Object obj = ois.readObject();
System.out.println(obj);
//4.释放资源
ois.close();
}
}
/**
* java.io.Serializable接口:标记型接口 类通过实现 java.io.Serializable
* 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
* 类实现了Serializable就可以序列化和反序列化了,不实现就会抛出异常NotSerializableException
* java.lang.Cloneable接口:实现了这个就可以克隆(复制)
* 去市场买肉-->肉上有一个蓝色章(检测合格)-->放心购买-->炒菜,炖肉,包饺子...
*
* static修饰的成员属于类,不属于对象,也就不能被序列化和反序列化 瞬态关键字transient:可以阻止成员变量序列化和反序列化 ArrayList:
* private transient Object[] elementData;
*/
public class Person implements Serializable {
private String name;
// private static int age; //0
// private transient int age;//0
public int age;
// 自定义序列号,无论类是否修改,都会使用此序列号
// private static final long serialVersionUID = 123456L;
private static final long serialVersionUID = 4364292156193835471L;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
//geter/seter省略
}