学习目标:
掌握今日学习内容并理解,可以用自己语言描述,可以自己验证过程
学习内容:
字节流读写、字符流读写、字符串缓冲池、字符流读写对象、序列化与反序列化、System类、流转换。
学习时间:
2020 11/23
学习产出:
字节流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class zijieliu {
public static void main(String[] args) throws IOException {
inputStreamMethod();
}
/**
* 写入
* 抛出IO异常
* @throws IOException
*/
public static void outputStreamMethod() throws IOException {
File file = new File("e:/耗子尾汁.txt");
//参数二:true代表接着文件的末尾续写
OutputStream os = new FileOutputStream(file,true);
//写数字
os.write(18);
os.write(97);
//写字符串---> .getBytes()将字符串转换为字节数组
os.write("马老师发生甚么事了".getBytes());
//关于流的操作,一定要有关闭流的动作
os.close();
}
/**
* 读取
* @throws IOException
*/
public static void inputStreamMethod()throws IOException {
File file = new File("e:/writer.txt");
InputStream it = new FileInputStream(file);
// 每次只读一个字节
// int result = it.read();
// System.out.println(result);
byte[] bytes =new byte[1024];
while(it.read(bytes)!=-1) {
System.out.println(new String(bytes));
}
it.close();
}
}
字符流
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.CharBuffer;
//字符流是由多个字节流组成
public class zifuliu {
public static void main(String[] args) throws IOException {
readerMethod();
}
/**
* 写功能
*/
public static void writerMethod() throws IOException{
Writer writer = new FileWriter("e:/writer.txt");
writer.write(26469);
writer.write("来偷袭我69岁的老同志");
writer.write("这好吗?");
writer.write("这不好!");
writer.close();//流关闭后不能再书写
}
/**
* 字符读取
* @throws IOException
*/
public static void readerMethod() throws IOException {
File file = new File("e:/writer.txt");
Reader reader = new FileReader(file);
//一次读取一个,默认读取是一个int值即ASCII码
// char result = (char)reader.read();
// System.out.println(result);
char [] cbuf = new char[6];
while(reader.read(cbuf)!=-1) {
System.out.println(new String(cbuf));
}
reader.close();
}
}
字符串缓冲池
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* 字符流缓冲对象BufferedReader
*/
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
bufferedWriterMethod();
}
/**
* 字符流写对象
* @throws IOException
*/
public static void bufferedWriterMethod() throws IOException {
//完全获取路径
File file = new File("e:/writercopy.txt");
//创建字符流写对象--需要文件路径
Writer writer= new FileWriter(file,true);
//创建字符流缓冲对象--需要一个字符流写对象
BufferedWriter bw = new BufferedWriter(writer);
bw.write("dadadadahaejh");
bw.close();
writer.close();
}
/**
* 字符缓冲对象读取
* @throws IOException
*/
public static void bufferedReaderMethod() throws IOException {
//完全获取路径
File file = new File("e:/writer.txt");
//创建字符流读取对象--需要文件路径
Reader reader= new FileReader(file);
//创建字符流缓冲对象--需要一个字符流读对象
BufferedReader br = new BufferedReader(reader);
//按行读取---可以解决缓冲池的浪费问题
String result = null;
while((result = br.readLine())!=null) {
System.out.println(result);
}
//关闭流对象需要进行倒系关闭
br.close();
reader.close();
}
}
字符流读写对象
package string;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* String是一个字符串对象,是一个常量
* StringBuilder(线程不安全) StringBuffer(线程安全)
* 两者只在安全性上存在差别,其他完全一致
*
* String.StringBuilder.StringBuffer的区别
* String修饰的是常量,StringBuilder和StringBuffer修饰的是变量
* StringBuilder的效率相比较StringBuffer高
* StringBuffer的安全性相比较StringBuilder高
*/
public class StringBuilderDemo {
public static void main(String[] args) throws IOException {
readerMethod();
}
/**
* 使用缓冲池读取
* 注意:使用字符数组作为缓冲池进行读取时,会出现显示多余或者不完整的情况
* StringBuilder是可变的字符序列,变化规则是当值超出了容器默认的大小时,容器会自动扩容原有的一半
* @throws IOException
*/
public static void readerMethod() throws IOException {
File file = new File("e:/writer.txt");
Reader reader = new FileReader(file);
//创建字符串缓冲池对象
StringBuilder sb = new StringBuilder();
char [] cbuf = new char[30];
while(reader.read(cbuf)!=-1) {
sb.append(cbuf);
}
System.out.println(sb);
reader.close();
}
}
序列化与反序列化
package io.obj;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
/**
* 持久化存储,就是将数据进行本地存储,可以多次使用
* 默认的序列化只能写入类、类签名、非瞬态和非静态的变量
* 只有transient修饰的才是瞬态变量
*
* 实现Serializable的过程是序列化,进行读取的操作是反序列化
* tips:说一说系列化和反序列化
* 序列化就是将对象通过流的形式写入到文件中进行持久化存储的过程
* 反序列化就是通过流的形式从文件中将对象提取出来转换为实体类进行使用
*/
public class WriterObjectDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
readerMethod();
}
public static void readerMethod()throws IOException, ClassNotFoundException {
//创建File对象指定地址
File file= new File("c:/ffffff.txt");
//创建一个用于读的流对象
InputStream is = new FileInputStream(file);
//创建一个用于读对象的对象流
ObjectInputStream ois = new ObjectInputStream(is);
//读对象
Object object = ois.readObject();
Item a = (Item)object;
System.out.println(a);
ois.close();
is.close();
}
/**
* 写对象 ObjectOutputStream
* 读对象 ObjectInputStream
* @throws IOException
* NotSerializableException未序列化异常
*/
public static void writerMethod() throws IOException {
//创建File对象指定地址
File file = new File("c:/ffffff.txt");
//创建一个用于写的流对象
OutputStream os = new FileOutputStream(file);
//创建写对象的对象流
ObjectOutputStream oos = new ObjectOutputStream(os);
//创建要存储的对象
Item item = new Item("咖啡",3.9,"蓝山");
oos.writeObject(item);
oos.close();
os.close();
//包名+类名---》类签名↓
// sr ***io.obj.Item***尌 w鷇A? D priceL brandt Ljava/lang/String;L nameq ~ xp@C€ t 钃濆北t 鍜栧暋
}
}
流转换
package changeff;
import java.util.Date;
public class Change {
//最简单基础的测试
public static void main(String[] args) {
System.out.println(System.currentTimeMillis());
System.out.println(new Date().getTime());
System.out.println(System.getProperties());
}
}
package changeff;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
* 字符流转换
* InputStreamReader 字节流转换字符流
* OutputStreamWriter 字符流转换字节流
*
* 获取控制台输入内容,再将其输出在控制台
*/
public class ioChange {
public static void main(String[] args) throws IOException {
//拿到控制台的内容
InputStream is = System.in;
//将字节流转换为字符流对象进行读取
InputStreamReader isr = new InputStreamReader(is);
//使用字符缓冲流对象按行读取
BufferedReader bf = new BufferedReader(isr);
//将读取到的结果输出到控制台
OutputStream os = System.out;
//创建一个字节流对象进行写操作
OutputStreamWriter osw = new OutputStreamWriter(os);
while(true) {
//从控制台进行读取
String content = bf.readLine();
if("over".equals(content)){
break;
}
//向控制台写
osw.write(content);
osw.flush();
}
osw.close();
os.close();
bf.close();
isr.close();
is.close();
}
}