gu123xin 2018-07-30 02:04 采纳率: 95.2%
浏览 1956
已采纳

Java指定路径文件的自动创建及写入

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代码可以在指定路径下生产文档

  • 写回答

4条回答

  • threenewbee 2018-07-30 02:38
    关注

    String fn = "E://test//test2.txt"; 这里就是文件名

    java根据全路径 直接创建文件
    看这个代码
    https://blog.csdn.net/zhangxiaomin1992/article/details/50863988

    把里面的几个函数复制粘贴过来
    main除外


    createNewFile(指定路径文件);
    fn = 指定路径文件;
    放在
    wf(fn, "ID,站点名称,站点");
    上面

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器