javaoop 第10章

package bao1;

import java.io.Serializable;

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



package bao1;

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



   public class Test {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		List<Student> list=new  ArrayList<Student>();
		Student s1=new Student("王",20);
		Student s2=new Student("李",20);
		list.add(s1);
		list.add(s2);
		
		OutputStream os=new FileOutputStream("D:/save.bin");
		//java 提供类是   输出流 ObjectOutputStream 
		ObjectOutputStream oos=new ObjectOutputStream(os);
		
		oos.writeObject(list);
		oos.close();
		os.close();
		System.out.println("serialize ok!");
	}

}

import java.awt.BufferCapabilities.FlipContents;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;public class Frist {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stub//写文件File file=new File("E:\\S2226.txt");file.createNewFile(); //判定文件是否存在if (file.exists()) {System.out.println("exists");}File file2 = file.getAbsoluteFile() ;long length = file.length();long time = file.lastModified();System.out.println("文件大小"+length);System.out.println(time);System.out.println(file2); //读文件FileInputStream fis=new FileInputStream("E:/S2226.txt");byte[] bytes=new byte[1024];int data;while((data=fis.read(bytes))!=-1){ String temp=new String(bytes,0,data);System.out.println(temp);}fis.close();//2.Reader reader=new FileReader("E:/S2226.txt");BufferedReader br=new BufferedReader(reader);String line ;while((line= br.readLine())!=null){System.out.println(line);}br.close();reader.close();//写文件//1.String words="今天气很好啊";FileOutputStream fos=new FileOutputStream("E:/S2226.txt");byte[] bytess = words.getBytes(); fos.write(bytess); fos.close(); System.out.println("ok!"); //2.Writer writer=new FileWriter("E:\\S2226.txt");BufferedWriter bw=new BufferedWriter(writer);String word="你好";bw.write(word);bw.close();writer.close();
package bao;

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 Test {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		 InputStream is=new FileInputStream("C:/ming.png");
		   DataInputStream dis=new DataInputStream(is);
		   
		   //copy到目标路径
		   OutputStream os=new FileOutputStream("D:/ming.png");
		   DataOutputStream dos=new DataOutputStream(os);
		   byte[] bytes=new byte[1024];
		   
		   int data ;
		   while((data= dis.read(bytes))!=-1){
			   
			   dos.write(bytes, 0, data);
		   }
		   dos.close();
		   os.close();
		   dis.close();
		   is.close();
		   System.out.println("copy");
		   
		   
	}

}

}}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值