P598 IO流-缓冲流课后练习1
P599 IO流-缓冲流课后练习2
P600 IO流-转换流概述与InputStreamReader的使用
@Test
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("X.txt");
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1){
String str = new String(cbuf, 0, len);
System.out.print(str);
}
isr.close();
}
P601 IO流-转换流实现文件的读入和写出
@Test
public void test2() throws IOException {
File file1 = new File("XX.txt");
File file2 = new File("XXX.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1){
osw.write(cbuf, 0, len);
}
isr.close();
osw.close();
}
P602 IO流-多种字符集编码的说明
P603 IO流-标准的输入、输出流
@Test
public void test1(){
BufferedReader br = null;
try {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true){
String data = br.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
P604 IO流-打印流的使用
P605 IO流-数据流的使用
P606 IO流与网络编程-复习:每天一考
P607 IO流与网络编程-复习:IO流概述
P608 IO流与网络编程-复习:节点流
P609 IO流与网络编程-复习:缓冲流与转换流
P610 IO流与网络编程-复习:其他几个处理流
P611 IO流与网络编程-对象序列化机制的理解
P612 IO流与网络编程-对象流序列化与反序列化字符串操作
package com.io;
import org.junit.Test;
import java.io.*;
public class ObjectInputOutputStreamTest {
@Test
public void testObjectOutputStream(){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("我爱徐州"));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testObjectInputStream(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String) obj;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}