一线互联网大厂Java核心面试题库
正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!
下面我用一个代码示范:
package io;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
// 实现对.properties 文件的读取
public class PropertiesBlog01 {
// 读入.properties文件
public void readProperties() throws IOException {
Properties properties = new Properties();
// 把文件的数据读入内存
properties.load(new FileInputStream(“blogtest.properties”));
// 输出文件的内容
System.out.println(properties.getProperty(“name”)); // 获取姓名
System.out.println(properties.getProperty(“age”)); // 获取年龄
System.out.println(properties.getProperty(“gender”)); // 获取性别
}
// main方法调用测试
public static void main(String[] args) throws IOException {
PropertiesBlog01 propertiesBlog01 = new PropertiesBlog01();
propertiesBlog01.readProperties();
}
}
输出结果:
jack
18
man
写入操作就有点意思了,这里写入分为三种情况:
-
写入新数据覆盖之前的数据
-
写入新数据不会覆盖之前的数据,但是如果有重复键值不会覆盖
-
写入新数据不会覆盖之前的数据,且有重复的键值会覆盖
这三种情况一般按需求使用,但是我感觉最常用的还是第三种(只是个人观点);
下面我用代码示范一下,代码中已有详细注释;
package io;
import java.io.*;
import java.util.Properties;
// 实现对.properties 文件的写入
public class PropertiesBlog02 {
// 写入.properties 文件
public void writeProperties() throws IOException {
Properties properties = new Properties();
// 下面是三种写入情况
// 1,写入新数据覆盖之前的数据
// 设置文件增添内容
properties.setProperty(“IdNum”, “123456789”);
properties.store(new FileOutputStream(“blogtest.properties”), “this is a comment”);
// 输出结果:
// 原文件内容: 写入新数据覆盖后为:
// name=jack #this is a comment
// age=18 #Fri Nov 19 19:58:08 CST 2021
// gender=man IdNum=123456789
// 2,写入新数据不会覆盖之前的数据,但是如果有重复键值不会覆盖
// 设置文件增添内容
properties.setProperty(“IdNum”, “repeat”);
properties.store(new FileOutputStream(“blogtest.properties”, true), “this is a comment”);
// 输出结果:
// 原文件内容: 写入新数据后为:
// #this is a comment #this is a comment
// #Fri Nov 19 19:58:08 CST 2021 #Fri Nov 19 20:06:03 CST 2021
// IdNum=123456789 IdNum=123456789
// #this is a comment
// #Fri Nov 19 20:06:03 CST 2021
// IdNum=repeat
// 3,写入新数据不会覆盖之前的数据,且有重复的键值会覆盖
properties.load(new FileInputStream(“blogtest.properties”));
properties.setProperty(“IdNum”, “newIdNum”);
properties.store(new FileOutputStream(“blogtest.properties”), “this is a comment”);
// 输出结果:
// 原文件内容: 写入新数据后为:
// #this is a comment #this is a comment
// #Fri Nov 19 20:06:03 CST 2021 #Fri Nov 19 20:13:22 CST 2021
// IdNum=123456789 IdNum=newIdNum
// #this is a comment
// #Fri Nov 19 20:06:03 CST 2021
// IdNum=repeat
}
public static void main(String[] args) throws IOException {
PropertiesBlog02 propertiesBlog02 = new PropertiesBlog02();
propertiesBlog02.writeProperties();
}
}
如果不太理解就记住就好了,忘记了就看看,写多了说不定有一天就会理解了;
这个操作调用的remove方法,这个有点意思,因为你在API文档中找不到Properties类有这个方法,那么就可以看一下源码:
可以发现它实际调用了别人的remove方法,那么是谁的呢?我们再往下看:
这个其实就是ConcurrentHashMap的方法,所以这个remove方法并没有记在Properties类中;
其实学会怎么使用就行了,这个使用也很简单,同样用代码示范一下:
package io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
// 实现对.properties 文件内容的删除
public class PropertiesBlog03 {
public void deleteProperties() throws IOException {
Properties properties = new Properties();
最后的话
无论是哪家公司,都很重视Spring框架技术,重视基础,所以千万别小看任何知识。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。
同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,好了希望这篇文章对大家有帮助!
部分截图:
司,好了希望这篇文章对大家有帮助!**
部分截图:
[外链图片转存中…(img-DOuurMlz-1715415720076)]