文件读写之其他流:
1)输入流
package com.huawei.honor;
import java.io.*;
import java.util.Scanner;
public class SystemInDemo {
public static void main(String[] args) throws IOException {
//public static final InputStream
InputStream is = System.in;
int by;
while((by=is.read())!=-1){
System.out.print((char)by);
}
//如何把字节流转换为字符流?用转换流
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
Scanner sc = new Scanner(System.in);
}
}
2)输出流
package com.huawei.honor;
import java.io.PrintStream;
public class SystemOutDemo {
public static void main(String[] args) {
//public static final PrintStream out;标准输出流
PrintStream ps = System.out;
//能够方便地打印各种数据值
ps.print("hello");
ps.print(100);
ps.println("hello");
ps.println(100);
System.out.println("hello");
System.out.println(100);
System.out.println();
}
}
3)字节打印流
package com.huawei.honor1;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class PrintStreamDemo {
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = new PrintStream(“myOtherStream\ps.txt”);
//写数据
//字节输出流有的方法
ps.write(97);
// 使用特有方法写数据
ps.print(97);
ps.println();
ps.print(98);
ps.println(97);
ps.println(98);
//释放资源
ps.close();
}
}
4)字符打印流
package com.huawei.honor1;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class PrintWriterDemo {
public static void main(String[] args) throws IOException {
// PrintWriter pw = new PrintWriter(“myOtherStream\pw.txt”);
// pw.write(“hello”);
// pw.write("\r\n");
// pw.flush();
// pw.write(“world”);
// pw.write("\r\n");
// pw.flush();
// pw.println(“hello”);
//相当于 pw.write(“hello”);
// pw.write("\r\n");
// pw.flush();
// pw.write(“world”);
// pw.flush();
PrintWriter pw = new PrintWriter(new FileWriter(“myOtherStream\pw.txt”),true);
pw.write("hello");
pw.write("world");
pw.println("hello");
/*pw.write("hello");
pw.write("\r\n");
pw.flush();*/
pw.println("world");
//释放资源
pw.close();
}
}
5)对象序列化
//学生类
package com.huawei.honor3;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 42L;
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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 “Student{” +
// “name=’” + name + ‘’’ +
// “, age=” + age +
// ‘}’;
// }
}
//测试类
package com.huawei.honor3;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
//NotSerializableException
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(“myOtherStream\oos.txt”));
Student s = new Student("林青霞",30);
oos.writeObject(s);
//释放资源
oos.close();
}
}
6)对象反序列化
//测试类
package com.huawei.honor3;
import java.io.*;
public class ObjectInputStreamDemo1 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(“myOtherStream\oos.txt”));
Object obj = ois.readObject();
Student s = (Student) obj;
System.out.println(s.getName()+","+s.getAge());
//释放资源
ois.close();
}
}