子符輸出流緩沖流
BufferedWrite繼承Writer
方法 writer() 寫(xiě)單個(gè)字符 字符數(shù)組 字符串
構(gòu)造方法:
BufferedReader(Reader r)
可以傳遞任意的字符輸出流
傳遞誰(shuí),就高效誰(shuí)
能傳遞的字符輸出流 FileWriter, OutputStreamWriter
BufferedWriter 具有自己特有的方法
void newLine() 寫(xiě)換行
newLine()文本中換行, \r\n也是文本換行
方法具有平臺(tái)無(wú)關(guān)性
Windows \r\n
Linux \n
newLine()運(yùn)行結(jié)果,和操作系統(tǒng)是相互關(guān)系
JVM: 安裝的是Windows版本,newLine()寫(xiě)的就是\r\n
安裝的是Linux版本,newLine()寫(xiě)的就是\n
代碼
public class BufferedWrierDemo {
public static void main(String[] args) throws IOException{
//創(chuàng)建字符輸出流,封裝文件
FileWriter fw = new FileWriter("c:\\buffer.txt");
BufferedWriter bfw = new BufferedWriter(fw);
bfw.write("你好");
bfw.newLine();
bfw.flush();
bfw.write("我好好");
bfw.newLine();
bfw.flush();
bfw.write("大家都好");
bfw.flush();
bfw.close();
}
}
字符輸入緩沖流
讀取功能 read() 單個(gè)字符,字符數(shù)組
構(gòu)造方法:
BufferedReader(Reader r)
可以任意的字符輸入流
FileReader InputStreamReader
BufferedReader自己的功能
String readLine() 讀取文本行 \r\n
方法讀取到流末尾,返回null
readLine()方法返回行的有效字符,沒(méi)有\(zhòng)r\n
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
int lineNumber = 0;
//創(chuàng)建字符輸入流緩沖流對(duì)象,構(gòu)造方法傳遞字符輸入流,包裝數(shù)據(jù)源文件
BufferedReader bfr = new BufferedReader(new FileReader("c:\\a.txt"));
//調(diào)用緩沖流的方法 readLine()讀取文本行
//循環(huán)讀取文本行, 結(jié)束條件 readLine()返回null
String line = null;
while((line = bfr.readLine())!=null){
lineNumber++;
System.out.println(lineNumber+" "+line);
}
bfr.close();
}
}
Properties類(lèi)
- 集合對(duì)象Properties類(lèi),繼承Hashtable,實(shí)現(xiàn)Map接口
可以和IO對(duì)象結(jié)合使用,實(shí)現(xiàn)數(shù)據(jù)的持久存儲(chǔ)
Properties集合的特有方法
1蟆炊、store(OutputStream out)
store(Writer w)
接收所有的字節(jié)或者字符的輸出流,將集合中的鍵值對(duì),寫(xiě)回文件中保存
public static void function()throws IOException{
Properties pro = new Properties();
pro.setProperty("name", "zhangsan");
pro.setProperty("age", "31");
pro.setProperty("email", "123456789@163.com");
FileWriter fw = new FileWriter("c:\\pro.properties");
//鍵值對(duì),存回文件,使用集合的方法store傳遞字符輸出流
pro.store(fw, "");
fw.close();
}
2饺鹃、load(InputStream in)
load(Reader r)
傳遞任意的字節(jié)或者字符輸入流
流對(duì)象讀取文件中的鍵值對(duì),保存到集合
public static void function_1()throws IOException{
Properties pro = new Properties();
FileReader fr = new FileReader("c:\\pro.properties");
//調(diào)用集合的方法load,傳遞字符輸入流
pro.load(fr);
fr.close();
System.out.println(pro);
}
使用Properties集合,存儲(chǔ)鍵值對(duì)
* setProperty等同與Map接口中的put
* setProperty(String key, String value)
* 通過(guò)鍵獲取值, getProperty(String key)
public static void function(){
Properties pro = new Properties();
pro.setProperty("a", "1");
pro.setProperty("b", "2");
pro.setProperty("c", "3");
System.out.println(pro);
String value = pro.getProperty("c");
System.out.println(value);
//方法stringPropertyNames,將集合中的鍵存儲(chǔ)到Set集合,類(lèi)似于Map接口的方法keySet
Set<String> set = pro.stringPropertyNames();
for(String key : set){
System.out.println(key+"..."+pro.getProperty(key));
}
}
對(duì)象的序列化和反序列化
IO流對(duì)象,實(shí)現(xiàn)對(duì)象Person序列化,和反序列化
- ObjectOutputStream 寫(xiě)對(duì)象,實(shí)現(xiàn)序列化
- ObjectInputStream 讀取對(duì)象,實(shí)現(xiàn)反序列化
public class ObjectStreamDemo {
public static void main(String[] args)throws IOException, ClassNotFoundException {
// writeObject();
readObject();
}
/*
* ObjectInputStream
* 構(gòu)造方法:ObjectInputStream(InputStream in)
* 傳遞任意的字節(jié)輸入流,輸入流封裝文件,必須是序列化的文件
* Object readObject() 讀取對(duì)象
*/
public static void readObject() throws IOException, ClassNotFoundException{
FileInputStream fis = new FileInputStream("c:\\person.txt");
//創(chuàng)建反序列化流,構(gòu)造方法中,傳遞字節(jié)輸入流
ObjectInputStream ois = new ObjectInputStream(fis);
//調(diào)用反序列化流的方法 readObject()讀取對(duì)象
Object obj =ois.readObject();
System.out.println(obj);
ois.close();
}
/*
* ObjectOutputStream
* 構(gòu)造方法: ObjectOutputStream(OutputSteam out)
* 傳遞任意的字節(jié)輸出流
* void writeObject(Object obj)寫(xiě)出對(duì)象的方法
*/
public static void writeObject() throws IOException{
//創(chuàng)建字節(jié)輸出流,封裝文件
FileOutputStream fos = new FileOutputStream("c:\\person.txt");
//創(chuàng)建寫(xiě)出對(duì)象的序列化流的對(duì)象,構(gòu)造方法傳遞字節(jié)輸出流
ObjectOutputStream oos = new ObjectOutputStream(fos);
Person p = new Person("lisi",25);
//調(diào)用序列化流的方法writeObject,寫(xiě)出對(duì)象
oos.writeObject(p);
oos.close();
}
}
person類(lèi)
public class Person implements Serializable{
public String name;
public int age极颓;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
注:1短蜕、序列化必須實(shí)現(xiàn)serializable接口
serializable接口沒(méi)有抽象方法,起標(biāo)記作用
2、transinent瞬態(tài)關(guān)鍵字
阻止成員變量序列化
String StringBuilder StringBuffer