经典笔试题-IO流及Socket篇

IO 流及Socket 部分:(共5 题:基础5 道)

91、什么是java 序列化,如何实现java 序列化?【基础】
答:序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题;序列化的实现:将需要被序列化的类实现Serializable 接口,该接口没有需实现的方法,implements Serializable 只是为了标注该对象是可被序列化的,然后使用一个输出流(如FileOutputStream)来构造一个ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream 对象的writeObject(Object obj)方法就可以将参数为obj 的对象写出(即保存其状态),要恢复的话则用输入流。

92、java 中有几种类型的流?JDK 为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?【基础】
答:字节流,字符流。字节流继承于InputStream、OutputStream,字符流继承于Reader、Writer。在java.io 包中还有许多其他的流,主要是为了提高性能和使用方便。

93、文件和目录(IO)操作:
1)如何列出某个目录下的所有文件?
2)如何列出某个目录下的所有子目录?
3)如何判断一个文件或目录是否存在?
4)如何读写文件?【基础】

答:

  1. 示例代码如下:
File file = new File("e:\\总结");
File[] files = file.listFiles();
for(int i=0; i<files.length; i++){
if(files[i].isFile())
	 System.out.println(files[i]);
}
  1. 示例代码如下:
File file = new File("e:\\总结");
File[] files = file.listFiles();
for(int i=0; i<files.length; i++){
if(files[i].isDirectory()) 
	System.out.println(files[i]);
}
  1. 创建File 对象,调用其exsit()方法即可返回是否存在,如:
System.out.println(new File("d:\\t.txt").exists());
  1. 示例代码如下:
		//读文件:
		FileInputStream fin = new FileInputStream("e:\\tt.txt");
		byte[] bs = new byte[100];
		while(true){
			int len = fin.read(bs);
			if(len <= 0) break;
			System.out.print(new String(bs,0,len));
		}
		fin.close();
		//写文件:
		FileWriter fw = new FileWriter("e:\\test.txt");
		fw.write("hello world!" + System.getProperty("line.separator"));
		fw.write("你好!北京!");
		fw.close();

94、写一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数。【基础】
答:代码如下:

	public int countWords(String file, String find) throws Exception {
		int count = 0;
		Reader in = new FileReader(file);
		int c;
		while ((c = in.read()) != -1) {
			while (c == find.charAt(0)) {
				for (int i = 1; i < find.length(); i++) {
					c = in.read();
					if (c != find.charAt(i)) break;
					if (i == find.length() - 1) count++;
				}
			}
		}
		return count;
	}

95、Java 的通信编程,编程题(或问答),用JAVA SOCKET 编程,读服务器几个字符,再写入本地显示?【基础】
答:Server 端程序:

public class Server{
	private ServerSocket ss;
	private Socket socket;
	private BufferedReader in;
	private PrintWriter out;
	public Server(){
		try {
			ss=new ServerSocket(10000);
			while(true){
				socket = ss.accept();
				String RemoteIP =
						socket.getInetAddress().getHostAddress();
				String RemotePort = ":"+socket.getLocalPort();
				System.out.println("A client come in!IP:"
						+ RemoteIP+RemotePort);
				in = new BufferedReader(new
						InputStreamReader(socket.getInputStream()));
				String line = in.readLine();
				System.out.println("Cleint send is :" + line);
				out =
						new PrintWriter(socket.getOutputStream(),true);
				out.println("Your Message Received!");
				out.close();
				in.close();
				socket.close();
			}
		}catch (IOException e){
			out.println("wrong");
		}
	}
	public static void main(String[] args){
		new Server();
	}
}

Client 端程序:

public class Client {
	Socket socket;
	BufferedReader in;
	PrintWriter out;
	public Client(){
		try {
			System.out.println("Try to Connect to
			127.0.0.1:10000");
			socket = new Socket("127.0.0.1",10000);
			System.out.println("The Server Connected!");
			System.out.println("Please enter some Character:");
			BufferedReader line = new BufferedReader(new
					InputStreamReader(System.in));
			out = new PrintWriter(socket.getOutputStream(),true);
			out.println(line.readLine());
			in = new BufferedReader(
					new InputStreamReader(socket.getInputStream()));
			System.out.println(in.readLine());
			out.close();
			in.close();
			socket.close();
		}catch(IOException e){
			out.println("Wrong");
		}
	}
	public static void main(String[] args) {
		new Client();
	}
}
发布了1310 篇原创文章 · 获赞 1029 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/weixin_42528266/article/details/104244971
今日推荐