#读取文本文件
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public List<String> readDataFromFile() {
List<String> contentList = new ArrayList<String>();
String userDir = System.getProperty("user.dir");
File file = new File(userDir+"\\data\\read.txt");
try {
InputStream in = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(in,"UTF-8");
BufferedReader br=new BufferedReader(reader);
String line = "";
while ((line=br.readLine())!= null) {
contentList.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return contentList;
}
#写入文本文件
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public boolean writeDataToFile(String content, boolean isAppend) {
boolean isWriteOk = false; // 用于判断写入文件是否成功
String userDir = System.getProperty("user.dir");
String fileName = userDir + "\\data\\write.txt";
File file = new File(fileName);
try {
OutputStream in = new FileOutputStream(file, isAppend);
OutputStreamWriter writer = new OutputStreamWriter(in, "UTF-8");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(content, 0, content.length());
bw.flush();
bw.close();
writer.close();
in.close();
isWriteOk = true;
}
catch (Exception e) {
isWriteOk = false;
e.printStackTrace();
}
return isWriteOk;
}
#读取excel文件
ps:读取和写入excel,均需要导入jxl的jar包,如下图所示,该jar包可自行下载,也可在我的资源里下载:https://download.csdn.net/download/guaishounan/12062676,目前csdn官方已支持作者自行设置下载资源所需积分,该资源下载不需要积分哈!
import java.io.File;
import java.io.FileInputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class Demo {
public static void main(String[] args) {
Demo demo = new Demo();
String path = System.getProperty("user.dir");
demo.readExcel(path+"\\Demo.xls");
}
public void readExcel(String fileName){
File file = new File(fileName);
try{
FileInputStream fis = new FileInputStream(file);
jxl.Workbook rwb = Workbook.getWorkbook(fis);
// 获取到Excel中所有的Sheet
Sheet[] sheet = rwb.getSheets();
for (int i = 0; i < sheet.length; i++) {
// 获取到当前的Sheet
Sheet rs = rwb.getSheet(i);
// 遍历当前Sheet中的行
for (int j = 0; j < rs.getRows(); j++) {
Cell[] cells = rs.getRow(j);
// 遍历当前行中的每一列
for(int k=0;k<cells.length;k++) {
System.out.print(cells[k].getContents());
System.out.print("\t");
}
System.out.println();
}
}
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
#写入excel文件
import java.io.File;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Demo {
public static void main(String[] args) {
Demo demo = new Demo();
String path = System.getProperty("user.dir");
demo.writeExcel();
}
public void writeExcel(){
WritableWorkbook book = null;
String info[] = {"姓名","性别","年龄"};
try{
// 创建一个Excel表格
String fileName = System.getProperty("user.dir") + "\\Demo.xls";
book = Workbook.createWorkbook(new File(fileName));
// 创建一张Sheet,或者使用book.getSheet(i)取得一张现有的Sheet
WritableSheet sheet = book.createSheet("Demo.xls", 0);
// 添加表头
for(int j=0;j<3;j++){
Label label = new Label(j, 0, info[j]);
sheet.addCell(label);
}
// 为表格添加一条数据
sheet.addCell(new Label(0, 1, "guaishounan"));
sheet.addCell(new Label(1, 1, "男"));
sheet.addCell(new Label(2, 1, "18"));
// 写入数据并关闭文件
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
#数据库读取,写入,修改,删除(CRUD)
配置信息如下所示,其中,mysql jdbc驱动请自行下载,也可到我的资源中进行下载,下载地址: https://download.csdn.net/download/guaishounan/12074797
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class CRUDSample {
// 连接数据库url
static String url;
// 创建Properties对象
static Properties info = new Properties();
// 1.驱动程序加载
static {
// 获得属性文件输入流
InputStream input = CRUDSample.class.getClassLoader()
.getResourceAsStream("config.properties");
try {
// 加载属性文件内容到Properties对象
info.load(input);
// 从属性文件中取出url
url = info.getProperty("url");
// 从属性文件中取出driver
String driverClassName = info.getProperty("driver");
Class.forName(driverClassName);
System.out.println("驱动程序加载成功...");
} catch (ClassNotFoundException e) {
System.out.println("驱动程序加载失败...");
} catch (IOException e) {
System.out.println("加载属性文件失败...");
}
}
public static void main(String[] args) {
// 数据插入
create();
// 数据查询
// read();
// 数据更新
// update();
// 数据删除
// delete();
}
// 数据查询操作
public static void read() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 2.创建数据库连接
conn = (Connection) DriverManager.getConnection(url, info);
// 3. 创建语句对象
pstmt = (PreparedStatement) conn
.prepareStatement("select userid,username from "
+ "user where userid > ?");
// 4. 绑定参数
pstmt.setInt(1, 0);
// 5. 执行查询(R)
rs = pstmt.executeQuery();
// 6. 遍历结果集
while (rs.next()) {
System.out.print(rs.getInt(1));
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
} finally { // 释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
// 查询最大的用户Id
public static int readMaxUserId() {
int maxId = 0;
try (
// 2.创建数据库连接
Connection conn = (Connection) DriverManager.getConnection(url, info);
// 3. 创建语句对象
PreparedStatement pstmt = (PreparedStatement) conn
.prepareStatement("select max(userid) from user");
// 4. 执行查询(R)
ResultSet rs = pstmt.executeQuery()) {
// 5. 遍历结果集
if (rs.next()) {
maxId = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return maxId;
}
// 数据插入操作
public static void create() {
try ( // 2.创建数据库连接
Connection conn = (Connection) DriverManager.getConnection(url, info);
// 3. 创建语句对象
PreparedStatement pstmt = (PreparedStatement) conn
.prepareStatement("insert into user (userid, username) values (?,?)")) {
// 查询最大值
int maxId = readMaxUserId();
// 4. 绑定参数
pstmt.setInt(1, ++maxId);
pstmt.setString(2, "guaishounan" + maxId);
// 5. 执行修改(C、U、D)
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功插入%d条数据。\n", affectedRows);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 数据更新操作
public static void update() {
try ( // 2.创建数据库连接
Connection conn = (Connection) DriverManager.getConnection(url, info);
// 3. 创建语句对象
PreparedStatement pstmt = (PreparedStatement) conn
.prepareStatement("update user set username = ? where userid = ?")) {
// 4. 绑定参数
pstmt.setString(1, "guaishounan");
pstmt.setInt(2, 1);
// 5. 执行修改(C、U、D)
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功更新%d条数据。\n", affectedRows);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 数据删除操作
public static void delete() {
try ( // 2.创建数据库连接
Connection conn = (Connection) DriverManager.getConnection(url, info);
// 3. 创建语句对象
PreparedStatement pstmt = (PreparedStatement) conn
.prepareStatement("delete from user where userid = ?")) {
// 查询最大值
int maxId = readMaxUserId();
// 4. 绑定参数
pstmt.setInt(1, maxId);
// 5. 执行修改(C、U、D)
int affectedRows = pstmt.executeUpdate();
System.out.printf("成功删除%d条数据。\n", affectedRows);
} catch (SQLException e) {
e.printStackTrace();
}
}
}