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();

        }

    }

}
相关推荐
颜如玉17 分钟前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
困鲲鲲1 小时前
Python中内置装饰器
python
摩羯座-185690305941 小时前
Python数据可视化基础:使用Matplotlib绘制图表
大数据·python·信息可视化·matplotlib
程序员的世界你不懂2 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年2 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
lingchen19062 小时前
MATLAB的数值计算(三)曲线拟合与插值
开发语言·matlab
爱隐身的官人2 小时前
cfshow-web入门-php特性
python·php·ctf
gb42152872 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
THMAIL2 小时前
量化股票从贫穷到财务自由之路 - 零基础搭建Python量化环境:Anaconda、Jupyter实战指南
linux·人工智能·python·深度学习·机器学习·金融
~-~%%2 小时前
从PyTorch到ONNX:模型部署性能提升
人工智能·pytorch·python