JAVA读文件:
String txtContent = "";
File file = new File("a.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
txtContent += tempString;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
JAVA写文件:
File file = new File("keywords.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
private Connection conn;
public void connectDb(String databaseName) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/" + databaseName;
String user = "root";
String password = "root";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
JAVA关闭Mysql数据库连接:
public void closeDb() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
正则表达式匹配任意字符:[\s\S].*? 断言前(?<=)后(?=)