Java面试题(十四)

1、Java 中如何实现序列化,有什么意义

序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决对象流读写操作时可能引发的问题(如果不进行序列化可能会存在数据乱序的问题)。

要实现序列化,需要让一个类实现 Serializable 接口,该接口是一个标识性接口,标注该类对象是可被序列化的,然后使用一个输出流来构造一个对象输出流并通过 writeObject(Object)方法就可以将实现对象写出(即保存其状态);如果需要反序列化则可以用一个输入流建立对象输入流,然后通过 readObject 方法从流中读取对象。序列化除了能够实现对象的持久化之外,还能够用于对象的深度克隆

2、Java 中有几种类型的流

字节流和字符流。字节流继承于 InputStream、OutputStream,字符流继承于Reader、Writer。在 java.io 包中还有许多其他的流,主要是为了提高性能和使用方便。关于 Java 的 I/O 需要注意的有两点:一是两种对称性(输入和输出的对称性,字节和字符的对称性);二是两种设计模式(适配器模式和装潢模式)。另外 Java 中的流不同于 C#的是它只有一个维度一个方向。

3、编程实现文件拷贝

java 复制代码
public final class MyUtil {
    private MyUtil() {
        throw new AssertionError();
    }
    public static void fileCopy(String source, String target) throws IOException {
        try (InputStream in = new FileInputStream(source)) {
            try (OutputStream out = new FileOutputStream(target)) {
                byte[] buffer = new byte[4096];
                int bytesToRead;
                while((bytesToRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
                }
            }
        }
    }
    public static void fileCopyNIO(String source, String target) throws IOException {
		try (FileInputStream in = new FileInputStream(source)) {
			try (FileOutputStream out = new FileOutputStream(target)) {
				FileChannel inChannel = in.getChannel();
  				FileChannel outChannel = out.getChannel();
                ByteBuffer buffer = ByteBuffer.allocate(4096);
                while(inChannel.read(buffer) != -1) {
                buffer.flip();
                outChannel.write(buffer);
                buffer.clear();
				}
			}
		}
	}
}

4、写一个方法**,**输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数

java 复制代码
public static int countWordInFile(String filename, String word) {
	int counter = 0;
	try (FileReader fr = new FileReader(filename)) {
		try (BufferedReader br = new BufferedReader(fr)) {
            String line = null;
            while ((line = br.readLine()) != null) {
                int index = -1;
                while (line.length() >= word.length() && (index = line.indexOf(word)) >= 0) {
                    counter++;
                    line = line.substring(index + word.length());
                }
			}
		}
	} catch (Exception ex) {
		ex.printStackTrace();
	}
	return counter;
}

5、阐述 JDBC 操作数据库的步骤

  • 加载驱动
java 复制代码
Class.forName("oracle.jdbc.driver.OracleDriver");
  • 创建连接
java 复制代码
Connection con =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott", "tiger");
  • 创建语句
java 复制代码
PreparedStatement ps = con.prepareStatement("select * from empwhere sal between ? and ?");

ps.setInt(1, 1000);

ps.setInt(2, 3000);
  • 执行语句
java 复制代码
ResultSet rs = ps.executeQuery();
  • 处理结果
java 复制代码
while(rs.next()) {

	System.out.println(rs.getInt("empno") + " - " +rs.getString("ename"));

}
  • 关闭资源
java 复制代码
finally {

    if(con != null) {

        try {

            con.close();

            } catch (SQLException e) {

            	e.printStackTrace();

        }

    }

}
相关推荐
学编程的小虎2 分钟前
用 Python + Vue3 打造超炫酷音乐播放器:网易云歌单爬取 + Three.js 波形可视化
开发语言·javascript·python
€8118 分钟前
Java入门级教程23——配置Nginx服务器、轻量级HTTP服务开发、前后端分离实现完整应用系统
java·开发语言·仓颉·生成验证码
yunson_Liu11 分钟前
编写Python脚本在域名过期10天内将域名信息发送到钉钉
开发语言·python·钉钉
星秀日30 分钟前
框架--SpringMVC
java·开发语言·servlet
小蒜学长34 分钟前
springboot餐厅信息管理系统设计(代码+数据库+LW)
java·数据库·spring boot·后端
Jabes.yang1 小时前
Java大厂面试实录:从Spring Boot到微服务的技术探讨
java·spring boot·spring cloud·微服务·技术面试
布林模型1 小时前
缠论工具czsc快速使用入门(二)
python·缠论·快速入门·czsc
高山上有一只小老虎1 小时前
idea字体大小设置
java
邂逅you1 小时前
用python操作mysql之pymysql库基本操作
数据库·python·mysql
啊森要自信1 小时前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash