一、创建一个maven项目Demp,构建结构如下图所示:
1、pom文件设置
因为是连接eimysql,首先肯定要有mysql驱动,这里用的maven管理的,所以添加对应依赖即可
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
代码部分
2、读取mysql数据库的数据,封装为User对象。
重新toString()方法,用特定分隔符进行组装。
package example.dao;
import java.util.ArrayList;
import java.util.List;
public class User {
private String id;
private String name;
private String password;
public User() {
this.id = id;
this.name = name;
this.password = password;
}
public String getId() {
return id;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
/**
* 生成具有特定分割符的to_string方法
* @return
* @param serp
*/
public String toStringNew(String serp) {
// System.out.println("分割符是"+serp);
String message = null;
message=String.join(serp,id,name,password);
// System.out.println("message:"+ message);
return message;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3、执行读取数据库操作,并写入本地txt文件
package example.Operation;
import example.dao.User;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* note:测试读取数据库文件,并以特定分割符serp,输出到dat文件中
*
*/
public class serpTest {
static Connection conn =null;
static PreparedStatement ps=null;
static ResultSet rst=null;
private static ArrayList<User> userList=null;
/**
* 获得数据库的连接
* @return 数据库的数据对象
*/
public static ArrayList<User> getConnection(){
ArrayList<User> userList = new ArrayList<User>();
try {
System.out.println("Data base connection test:");
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8"; //访问test库
conn= DriverManager.getConnection(url,"root","root");
System.out.print(conn);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement =conn.createStatement();//操作数据库
String sql ="select * from user";//要执行的sql语句
ResultSet rs=statement.executeQuery(sql);
System.out.println("id"+"\t"+"姓名"+"\t"+"密码");
while(rs.next()){
User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
userList.add(user);
// System.out.println(userList);
// System.out.println(rs.getString("id")+"\t"+rs.getString("name")+"\t"+rs.getString("password"));
}
} catch (Exception e) {
System.out.println("数据库连接失败!");
}
System.out.println(userList);
return userList;
}
/**
* 根据输出的路径和文件名称创建file对象
* @param path 写入文件的路径
* @param fileName 写入文件的路径
* @throws SQLException
*/
public String createFile(String path, String fileName ){
File f = new File(path);
if (!f.exists()) {
f.mkdirs(); //创建目录
}
File file = new File(path, fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file.getAbsolutePath();
}
/**
* 根据某个路径读取文件
* @param userList 把数据库的用户数组读取到文件中
* @throws SQLException
*/
private void writeFile(List<User> userList,String filePath,String serp){
System.out.println("分割符"+serp);
BufferedWriter bufferedWriter=null;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
for(int i = 0; i < userList.size(); i++ ) {
try {
System.out.print("输出list的数据:"+userList.get(i).toStringNew(serp));
bufferedWriter.write(userList.get(i).toStringNew(serp));
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedWriter.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
String path="C:\\Users\\migu\\Desktop\\test";
String fileName="test.dat";
serpTest serpTest = new serpTest();
String filePath=serpTest.createFile(path,fileName);
System.out.println("文件的全路径是:"+filePath); //输出文件的绝对路径
ArrayList<User> userList= serpTest.getConnection();
System.out.println(userList);
// serpTest.writeFile(userList,filePath, "|");
int splitChar = 31; //找到特定分割符对应的十进制数字
String splitString = String.valueOf((char)splitChar);
serpTest.writeFile(userList,filePath,splitString); //特殊十进制=31的对应分隔符
}
}
3、验证测试数据
1)数据库的数据
2)本地文件的数据,并验证对应的十六进制是否是‘0x1f’,
特定分隔符分割的文件生成验证成功