package webtest;
import java.sql.*;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
public class test3 {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/gumysql";
static final String USER = "ggx";
static final String PASS = "123456";
public static void wf(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
out.write(conent + "\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, name, url FROM websites";
ResultSet rs = stmt.executeQuery(sql);
String fn = "E://test//test2.txt";
wf(fn, "ID,站点名称,站点");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String url = rs.getString("url");
System.out.print("ID: " + id);
System.out.print(", 站点名称: " + name);
System.out.print(", 站点 URL: " + url);
System.out.print("\n");
wf(fn, id + "," + name + "," + url);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
现在已经连接到mysql数据库,到需要一段java代码可以在指定路径下生产文档
String fn = "E://test//test2.txt"; 这里就是文件名
java根据全路径 直接创建文件
看这个代码
https://blog.csdn.net/zhangxiaomin1992/article/details/50863988
把里面的几个函数复制粘贴过来
main除外
把
createNewFile(指定路径文件);
fn = 指定路径文件;
放在
wf(fn, "ID,站点名称,站点");
上面
在我发的代码上加上可以生成文档的代码
指定需要创建的文件夹路径以及文件名,若文件存在则不创建,若文件及路径不存在就会自动创建路径和文件
public class Test {
public static void main(String[] args) {
String directory="myfile/b";
String filename="a.txt";
//注释掉的为方法一
/*File file=new File(directory,filename);
if (file.exists()) {
System.out.println(file.getAbsolutePath());
System.out.println(file.getName());
}else {
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
//此为方法二:
File file=new File(directory);
if (!file.exists()) {
file.mkdirs();
}
File file2=new File(directory,filename);
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//向指定文件中写入文字
FileWriter fileWriter;
try {
fileWriter = new FileWriter("myfile/b/a.txt");
//使用缓冲区比不使用缓冲区效果更好,因为每趟磁盘操作都比内存操作要花费更多时间。
//通过BufferedWriter和FileWriter的连接,BufferedWriter可以暂存一堆数据,然后到满时再实际写入磁盘
//这样就可以减少对磁盘操作的次数。如果想要强制把缓冲区立即写入,只要调用writer.flush();这个方法就可以要求缓冲区马上把内容写下去
BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
bufferedWriter.write("this is a.txt");
bufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}