/*1.流的分类
* 按照数据流向的不同:输入流 输出流
* 按照处理数据的单位的不同:字节流 字符流(处理的文件文本)
* 按照角色的不同,节点流(直接作用于文件)处理流
* 2.IO的体系
* 抽象基类 节点流(文件流) 缓冲流(处理流的一种)
* InputStream FileInputStream BufferedInputStream
* OutputStream FileOutputStream BufferedOutputStream
* Reader FileReader BufferedReader
* Writer FileWriter BufferedWriter
*/
public class TestFileInputOutputStream {
//从硬盘存在的一个文件中,读取其内容到程序中。使用FileInputStream
@Test
public void testFileOutputStream(){
//1.创建一个File对象,表明要写入的文件位置
//输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
File file = new File("hello2.txt");
//2.创建一个FileOutpStream的对象,将file的对象作为形参传递给FileOutpStream的构造器中
FileOutputStream fOutputStream = null;
try {
fOutputStream = new FileOutputStream(file);
//3.写入的操作
fOutputStream.write(new String("I love China!!!!!!!!").getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally{
//4.关闭输出流
if (fOutputStream != null) {
try {
fOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileInputStream1() {
//1.创建一个File类的对象
File file = new File("hello.txt");
//2.创建一个FileInputStream类的对象
FileInputStream fiStream = null;
try {
fiStream = new FileInputStream(file);
//3.写入的操作
byte[] b = new byte[5];//读取到的数据要写入的数组。
int len;//每次读入到byte中的字节的长度
while ((len = fiStream.read(b)) != -1) {
for (int i = 0; i < len; i++) {
System.out.println((char)b[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
//4.关闭相应的流
if(fiStream != null){
try {
fiStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/*
* 标准的输入输出流:
* 标准的输出流:System.out
* 标准的输入流:System.in
*
* 题目:
* 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,
* 直至当输入“e”或者“exit”时,退出程序。
*/
@Test
public void test2(){
BufferedReader br = null;
try {
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String str;
while(true){
System.out.println("请输入字符串:");
str = br.readLine();
if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
break;
}
String str1 = str.toUpperCase();
System.out.println(str1);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
* 如何实现字节流与字符流之间的转换:
* 转换流:InputStreamReader OutputStreamWriter
* 编码:字符串 --->字节数组
* 解码:字节数组--->字符串
*/
@Test
public void test1(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//解码
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "GBK");
br = new BufferedReader(isr);
//编码
File file1 = new File("hello3.txt");
FileOutputStream fos = new FileOutputStream(file1);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
bw = new BufferedWriter(osw);
String str;
while((str = br.readLine()) != null){
bw.write(str);
bw.newLine();
bw.flush();
}
}catch (IOException e) {
e.printStackTrace();
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class TestObjectInputOutputStream {
// 对象的反序列化过程:将硬盘中的文件通过ObjectInputStream转换为相应的对象
@Test
public void testObjectInputStream() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(
"person.txt"));
Person p1 = (Person)ois.readObject();
System.out.println(p1);
Person p2 = (Person)ois.readObject();
System.out.println(p2);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(ois != null){
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 对象的序列化过程:将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中
@Test
public void testObjectOutputStream() {
Person p1 = new Person("小米", 23,new Pet("花花"));
Person p2 = new Person("红米", 21,new Pet("小花"));
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
oos.writeObject(p1);
oos.flush();
oos.writeObject(p2);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/*
* 要实现序列化的类: 1.要求此类是可序列化的:实现Serializable接口
* 2.要求类的属性同样的要实现Serializable接口
* 3.提供一个版本号:private static final long serialVersionUID
* 4.使用static或transient修饰的属性,不可实现序列化
*/
class Person implements Serializable {
private static final long serialVersionUID = 23425124521L;
static String name;
transient Integer age;
Pet pet;
public Person(String name, Integer age,Pet pet) {
this.name = name;
this.age = age;
this.pet = pet;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", pet=" + pet + "]";
}
}
class Pet implements Serializable{
String name;
public Pet(String name){
this.name = name;
}
@Override
public String toString() {
return "Pet [name=" + name + "]";
}
}