java oop 第十章 IO

本文详细介绍了Java中IO流的基本操作,包括字节流和字符流的读写、缓冲流的使用、二进制文件的处理以及对象的序列化过程。通过具体的代码示例展示了如何进行文件的读取和写入,并提供了对不同流类型的深入理解。
package cn.happy.IO;

import java.io.FileInputStream;

public class 字节流 {

	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub

		FileInputStream fis=new FileInputStream("E:\\S2226.txt");
		byte[] bytes=new byte[1024];
		int date=fis.read(bytes);
		while(date!=-1){
			String topString=new String(bytes,0,date);
			System.out.println(topString);
			date=fis.read(bytes);
		}
	}

}

package cn.happy.IO;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class 字节流写入 {

	public static void main(String[] args) throws IOException {
		FileOutputStream fos=new FileOutputStream("E:\\sss6.txt");
		String word="hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh";
		byte[] bytes=word.getBytes();
		fos.write(bytes);
		fos.close();

	}

}

------字符流
package cn.happy.IO.b;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class 字符流读取 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
FileReader reader=new FileReader("E:\\S2226.txt");
char[] chars=new char[1024];
int date;
while((date=reader.read(chars))!=-1){
	String top=new String(chars,0,date);
	System.out.println(top);
}
reader.close();
	}

}

package cn.happy.IO.b;

import java.io.FileWriter;
import java.io.IOException;

public class 字符流写入 {

	public static void main(String[] args) throws IOException {
		FileWriter fWriter=new FileWriter("E:\\S2226.txt");
		String word="升高的困扰姐咖啡奶粉 的看法那我开三年的 ";
		fWriter.write(word);
		fWriter.close();

	}

}

-
package cn.happy.IO.c;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class buffer {

	public static void main(String[] args) throws IOException {
		Reader reader=new FileReader("E:\\S2226.txt");
		BufferedReader bf=new BufferedReader(reader);
		String line;
		while((line=bf.readLine())!=null){
			System.out.println(line);
		}
		bf.close();
		reader.close();
		
		
	}

}

package cn.happy.IO.c;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class buffereddd {

	public static void main(String[] args) throws IOException {
	 Writer writer=new FileWriter("E:\\S226.txt");
	 String ss="wowowowowowwwwwwwwwwwwwwwwwwwwwwwwwww";
	 BufferedWriter bf=new BufferedWriter(writer);
	 bf.write(ss);
	 bf.close();
	 writer.close();
	 
	}

}

----二进制
package cn.happy.IO.d;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class date {

	public static void main(String[] args) throws IOException {
		InputStream is=new FileInputStream("E:\\QQ图片20170414115705.jpg");
         DataInputStream dis=new DataInputStream(is);
         
         OutputStream os=new FileOutputStream("F:\\S2222.jpg");
         DataOutputStream dos=new DataOutputStream(os);
         
         byte[] bytes=new byte[1024];
         int date;
         while((date=dis.read(bytes))!=-1){
        	 os.write(bytes, 0, date);
         }
         dos.close();
         os.close();
         dis.close();
         is.close();
         System.out.println("成功");
	}

}

序列化---------------
package cn.happy.IO.e;

import java.io.Serializable;

public class Student implements Serializable{

	private String name;
	private int 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;
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}

package cn.happy.IO.e;

import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Test {

	public static void main(String[] args) throws Exception {
		List<Student> list=new ArrayList<Student>();
		Student stu=new Student("ddd",1);
		Student stu1=new Student("eee",2);
		list.add(stu);
		list.add(stu1);
		OutputStream osOutputStream=new FileOutputStream("E:\\S226.txt");
		ObjectOutputStream dos=new ObjectOutputStream(osOutputStream);
		dos.writeObject(list);
		dos.close();
		osOutputStream.close();
		
		System.out.println("成功");
		

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值