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

        }

    }

}
相关推荐
ZHOU_WUYI8 分钟前
旋转位置编码 (2)
pytorch·python·深度学习
程序员~小强28 分钟前
让知识触手可及!基于Neo4j的机械设备知识图谱问答系统
人工智能·python·django·知识图谱·neo4j
Forget the Dream30 分钟前
设计模式之迭代器模式
java·c++·设计模式·迭代器模式
咩咩觉主37 分钟前
C# &Unity 唐老狮 No.7 模拟面试题
开发语言·unity·c#
大丈夫在世当日食一鲲37 分钟前
Java中用到的设计模式
java·开发语言·设计模式
DanCheng-studio41 分钟前
智科 机器学习毕业设计题目指导
python·毕业设计·毕设
A-Kamen42 分钟前
Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实战指南
java·spring boot·后端
练川44 分钟前
Stream特性(踩坑):惰性执行、不修改原始数据源
java·stream
狂奔小菜鸡1 小时前
Java运行时数据区
java·jvm·后端
trymoLiu1 小时前
SpringBoot 实现 RSA+AES 自动接口解密!
java·spring boot