JAVAオブジェクトを指定して作成したファイルに書き込み、読み込みます.


Javaの文字ストリームとバイトストリームを使用してデータを保存することは以前に知られていましたが、バイトストリームと文字ストリームを使用してオブジェクトを保存することはできません.javaはオブジェクト言語向けのすべての問題をオブジェクトと見なしています.オブジェクトを保存するには、データストリームとオブジェクトストリームを使用します.
 
データ入力ストリーム:DataInputStream
 
オブジェクト出力ストリーム:ObjectInputStream
 
次のクラスで、オブジェクトを保存する方法を示します.
 
 
public class Student {

	String name;//   
	byte age;//   
	float score;//   
	int num;//   
	String desc;//     

	public Student(String name, byte age, float score, int num, String desc) {
		this.name = name;
		this.age = age;
		this.score = score;
		this.num = num;
		this.desc = desc;
	}

}

オブジェクト・データ・ストリームを使用する場合は、文字列の処理(文字列をバイトに変換して処理し、オブジェクト・ストリームでは不要)に注意して、文字列の長さ-->長さに基づいてバイト配列を作成-->バイト配列の長さ-->文字の書き込み
//        
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 *      
 * 
 * @author
 * 
 */
public class StudentDB {

	public static void main(String args[]) {
		File file = new File("F:\\abc\\student.db");

		//    4+1+4+4+6
		Student stu = new Student("  ", (byte) 20, 95.5F, 20120101, "youxiu");

		saveStudent(file, stu);
		System.out.println("    !");
		
		//           ,         
		Student stu2 = getStudent(file);

		System.out.println(stu2.name);
		System.out.println(stu2.age);
		System.out.println(stu2.score);
		System.out.println(stu2.num);
		System.out.println(stu2.desc);
		
		

	}

	/**
	 *              
	 * 
	 * @param file
	 *                   
	 * @param stu
	 *                    
	 */
	public static void saveStudent(File file, Student stu) {
		try {
			//        
			FileOutputStream fos = new FileOutputStream(file);
			//                 
			DataOutputStream dos = new DataOutputStream(fos);

			//         ,         
			// //         
			byte[] names = stu.name.getBytes();
			//            
			dos.writeInt(names.length);
			//    
			dos.write(names);

			//    
			dos.writeByte(stu.age);
			//    
			dos.writeFloat(stu.score);
			//   
			dos.writeInt(stu.num);
			//   
			byte[] descs = stu.desc.getBytes();
			dos.writeInt(descs.length);
			dos.write(descs);

			//     
			dos.flush();
			//    
			fos.close();

		} catch (Exception ef) {
			ef.printStackTrace();
		}

	}

	/**
	 *           
	 * 
	 * @param file
	 *                    
	 * @return          
	 */
	public static Student getStudent(File file) {
		try {
			//        
			FileInputStream fis = new FileInputStream(file);
			//           
			DataInputStream dis = new DataInputStream(fis);

			//        
			int len = dis.readInt();
			//       
			byte[] names = new byte[len];
			//            
			dis.read(names);
			//           
			String name = new String(names);

			//     
			byte age = dis.readByte();
			float score = dis.readFloat();
			int num = dis.readInt();

			//   
			int len2 = dis.readInt();
			byte[] descs = new byte[len2];
			dis.read(descs);
			String desc = new String(descs);

			//             
			Student stu = new Student(name, age, score, num, desc);
			return stu;
		} catch (Exception ef) {
			ef.printStackTrace();
		}
		return null;
	}

}

 
分析:データストリームがオブジェクトを保存する難点は文字列の操作にある.上記の例では、名前とプロフィールは文字です.ストリング文字列を読み込むには、次の手順に従います.
文字列の長さを取得-->取得長に基づいてバイト配列を作成-->ストリームからバイトを配列に読み込み-->バイトを文字列に組み立てる
読み出された文字列を書き出すには、まず配列で読み出された文字列のバイト配列を取得します-->読み出されたバイト配列の長さを書き出します-->文字列を書き出します.
 
 
//          
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *      
 * 
 * @author
 * 
 */
public class StudentDB {

	public static void main(String args[]) {
		File file = new File("F:\\abc\\student.db");

		//    4+1+4+4+6
		Student stu = new Student("  ", (byte) 20, 95.5F, 20120101, "youxiu");

		saveStudent(file, stu);
		System.out.println("    !");
		
		//        
		Student stu2 = getStudent(file);
		System.out.println(stu2.name);
		System.out.println(stu2.age);
		System.out.println(stu2.score);
		System.out.println(stu2.num);
		System.out.println(stu2.desc);
		
		

	}

	/**
	 *              
	 * 
	 * @param file
	 *                   
	 * @param stu
	 *                    
	 */
	public static void saveStudent(File file, Student stu) {
		try {
			//        
			FileOutputStream fos = new FileOutputStream(file);
			//             
			ObjectOutputStream dos = new ObjectOutputStream(fos);

			
			//    
			dos.writeObject(stu.name);

			//    
			dos.writeByte(stu.age);
			//    
			dos.writeFloat(stu.score);
			//   
			dos.writeInt(stu.num);
			//   
			dos.writeObject(stu.desc);

			//     
			dos.flush();
			//    
			fos.close();

		} catch (Exception ef) {
			ef.printStackTrace();
		}

	}

	/**
	 *           
	 * 
	 * @param file
	 *                    
	 * @return          
	 */
	public static Student getStudent(File file) {
		try {
			//        
			FileInputStream fis = new FileInputStream(file);
			//         
			ObjectInputStream dis = new ObjectInputStream(fis);

			
			//     
                         //            Object   ,      
			String name = (String)dis.readObject();

			//     
			byte age = dis.readByte();
			float score = dis.readFloat();
			int num = dis.readInt();

			//   
			String desc = (String)dis.readObject();

			//             
			Student stu = new Student(name, age, score, num, desc);
			return stu;
		} catch (Exception ef) {
			ef.printStackTrace();
		}
		return null;
	}

}

解析:1,オブジェクトストリームを用いてオブジェクトを保存し,文字列の保存時に強制的にString String desc=(String)disに変換する.readObject(); 文字列を書くにはobjectを書くだけでいいwriteObject()です.
2,読み書きの方法では,いずれも対象の属性をそれぞれ読み出し,終日の読み出しではない.
 
 
//          2

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *      
 * 
 * @author
 * 
 */
public class StudentDB2 {

	public static void main(String args[]) {
		File file = new File("F:\\abc\\student.db");

		//    4+1+4+4+6
		Student stu = new Student("  ", (byte) 20, 95.5F, 20120101, "youxiu");

		saveStudent(file, stu);
		System.out.println("    !");

		//         
		Student stu2 = getStudent(file);
		System.out.println(stu2.name);
		System.out.println(stu2.age);
		System.out.println(stu2.score);
		System.out.println(stu2.num);
		System.out.println(stu2.desc);

	}

	/**
	 *              
	 * 
	 * @param file
	 *                   
	 * @param stu
	 *                    
	 */
	public static void saveStudent(File file, Student stu) {
		try {
			//        
			FileOutputStream fos = new FileOutputStream(file);
			//             
			ObjectOutputStream dos = new ObjectOutputStream(fos);

			//        
			dos.writeObject(stu);

			//     
			dos.flush();
			//    
			fos.close();

		} catch (Exception ef) {
			ef.printStackTrace();
		}

	}

	/**
	 *           
	 * 
	 * @param file
	 *                    
	 * @return          
	 */
	public static Student getStudent(File file) {
		try {
			//        
			FileInputStream fis = new FileInputStream(file);
			//         
			ObjectInputStream dis = new ObjectInputStream(fis);
			//    
			Student stu = (Student) dis.readObject();
			return stu;
		} catch (Exception ef) {
			ef.printStackTrace();
		}
		return null;
	}

}

 
分析:上記のコードは直接読み取る対象である.