serverio.java

来自「JAVA实现的聊天工具,可以容纳最多10个用户 1.本系统需要JDK1.5 」· Java 代码 · 共 66 行

JAVA
66
字号
//to write and read object of ServerDatabase from a file
import java.io.*;
//import java.net.*;
public class ServerIO{
	private static ObjectOutputStream oos=null;
	private static ObjectInputStream ois=null;
		
	private static String file="serverDatabase";
			
	static void writeServerDatabase(ServerDatabase end){
		
		try{
			/*BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file));
			oos=new ObjectOutputStream(bos);*/
			oos=new ObjectOutputStream(new BufferedOutputStream(
					new FileOutputStream(file)));
			oos.writeObject(end);
			//bos.flush();
			oos.flush();
			oos.close();
		}catch(IOException e){
			System.out.println(e+" "+e.getMessage());
		}
	}
	
	static ServerDatabase readServerDatabase(){
		
		try{
			/*BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
			ois=new ObjectInputStream(bis);*/
			ois=new ObjectInputStream(new BufferedInputStream(
					new FileInputStream(file)));
			return (ServerDatabase)ois.readObject();
		}catch(Exception e){
			System.out.println(e+" "+e.getMessage());
			return null;
		}
	}
	
	static boolean checkFile(){ // to check if none of data has been written in
		File temp=new File(file);
		boolean signal=temp.exists();
		if(!signal) //file doesn't exit,the initial status
			return false;
		else return true;
	}
	
	static int getInt() throws IOException{
		return Integer.parseInt(getString());
	}
	
	static long getLong() throws IOException{
		return Long.parseLong(getString());
	}
	
	static String getString() {
		BufferedReader br; 
		br=new BufferedReader(new InputStreamReader(System.in));
        try{
        	return br.readLine();
        }catch (IOException e){
        	System.out.println(e+" "+e.getMessage());
        	return null;
        }
	}		
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?