//读取键盘录入的数据写到a.txt
//方式一
private static void method() throws IOException {
//创建输入流对象
InputStream is = System.in;
Reader r = new InputStreamReader(is);
//创建输出流对象
FileWriter fw = new FileWriter("a.txt");
//读取数据
byte[] bys = new byte[1024];
int len;
while((len = is.read(bys))!=-1)
{
fw.write(new String(bys, 0, len));
fw.flush();
}
fw.close();
is.close();
}
//方式二
private static void method2() throws IOException {
//创建输入流对象
InputStream is = System.in;
Reader r = new InputStreamReader(is);
//创建输出流对象
FileWriter fw = new FileWriter("a.txt");
//读取数据
char[] chs = new char[1024];
int len;
while((len = r.read(chs))!=-1)
{
fw.write(chs, 0 , len);
fw.flush();
}
fw.close();
r.close();
}
// 将文件中的数据输出到控制台
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//OutputStream os = System.out;
//Writer w = new OutputStreamWriter(System.out);
//BufferedWriter bw = new BufferedWriter(w);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line;
while((line = br.readLine())!=null)
{
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
//使用打印流复制文本文件
private static void method3() throws FileNotFoundException, IOException {
//创建输入流对象
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//创建打印流对象
PrintWriter pw = new PrintWriter(new FileWriter("b.txt"),true); // 此处true,是自动刷新
String line;
while((line = br.readLine())!=null)
{
pw.println(line);
}
pw.close();
br.close();
}
//使用字节流进行文件的复制(复制二进制文件)
private static void method() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("这里是一个文件名");
FileOutputStream fos = new FileOutputStream("另一个问件名");
// 一次读一个字节
// int by;
// while((by = fis.read() ) !=-1){
// fos.write(by);
// }
// 一次读取一个字符数组
int len;
byte[] bys = new byte[1024];
while((len = fis.read(bys))!=1){
fos.write(len);
}
fis.close();
fos.close();
}
对象操作流
可以用于读写任意类型的对象
ObjectOutputStream :对象输出字符流
WriteObject
ObjectInputStream :对象输入字符流
ReadObject
注意:
使用对象输出流写出对象,只能使用对象输入流来读取对象
只能将支持java.io.Serializable 接口的对象写入流中
Serializable:序列号,是一个标识接口,只起表示作用,没有方法
当一个类的对象需要进行IO流进行读写的时候,这个类必须实现此接口
//eg:创建用于文件读写的学生类对象
public class Student implements Serializable{
private static final long serialVersionUID = -4114259825335049236L; // 固定序列号
String name;
int age;
public Student(String name,int age) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
//将学生对象写入文件
private static void method() throws IOException, FileNotFoundException {
//创建对象输出流的对象
//FileOutputStream fos = new FileOutputStream("a.txt"); // 字节输出流
//ObjectOutputStream oos = new ObjectOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
//创建学生对象
Student s = new Student("zhangsan", 18);
Student s2 = new Student("lisi",19);
//写出学生对象
oos.writeObject(s);
oos.writeObject(s2);
//释放资源
oos.close();
}
//将文件中的学生对象读取出来
private static void method2() throws IOException, FileNotFoundException, ClassNotFoundException {
//创建对象输入流的对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
/* //读取对象
java.lang.Object obj = ois.readObject();
System.out.println(obj);
java.lang.Object obj2 = ois.readObject();
System.out.println(obj2);
java.lang.Object obj3 = ois.readObject();
System.out.println(obj3);*/
try{
while(true)
{
java.lang.Object obj = ois.readObject();
System.out.println(obj);
}
} catch(EOFException e){
System.out.println("读到了文件的末尾");
}
//释放资源
ois.close();
}
// 另一种方式写入
private static void method3() throws IOException, FileNotFoundException {
// 另一种写入对象文件的方式(将所有对象写入到集合对象中)
//创建对象输出流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.txt"));
//创建集合对象
ArrayList<Student> list = new ArrayList<Student>();
//添加学生对象
list.add(new Student("wangwu", 19));
list.add(new Student("zhaoliu", 20));
//写出集合对象
oos.writeObject(list);
//释放资源
oos.close();
}
//另一种读取方式
private static void method4() throws IOException, FileNotFoundException, ClassNotFoundException {
//创建对象输出流对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.txt"));
//读取数据
java.lang.Object obj = ois.readObject();
//System.out.println(obj);
// 向下转型,获取具体的子类对象
ArrayList<Student> list = (ArrayList<Student>) obj;
for(Student stu:list)
{
System.out.println(stu);
}
//释放资源
ois.close();
}