Java读取文件存储到mysql

  写了一批Lua脚本,要放到数据库里面,调用的时候进行计算,由于有太多lua脚本,就写了个程序来录入。

  下面主要分三个部分:

public static String readToString(String fileName)

public static void wirteToMYSQL(String string) throws FileNotFoundException

public static List<String> getFileList(String strPath)

readToString,用作从lua脚本,也就是文件中读取script出来,传送一个参数就是文件夹的绝对路径。

writeToMYSQL主要进行数据库写的函数,main函数里面也就调用了这一个函数,也就是说这个函数调用了另外两个函数。

getFileList,对指定路径的文件夹进行遍历,找遍所有的lua脚本,并将lua脚本的绝对路径处理(加上,因为java处理绝对路径需要加上)放到一个list里面。

writeToMYSQL本来是打算传个参数作为路径进去的,想想就用一次,就直接在里面写死了绝对路径。

上完整代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import com.mysql.jdbc.PreparedStatement;

public class Test {
    public static void main(String[] args) throws IOException {

        wirteToMYSQL("");

    }

    public static String readToString(String fileName) {
        String encoding = "UTF-8";
        File file = new File(fileName);
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(filecontent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return null;
        }
    }

    public static void wirteToMYSQL(String string) throws FileNotFoundException {

        final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        final String DB_URL = "jdbc:mysql://192.168.**.***:3306/cqc";
        final String USER = "***";
        final String PASS = "*****";

        Connection conn = null;
        Statement stmt = null;

        List<String> id = getFileList("C:\Users\yefeifan\Desktop\lua");
        List<String> fileList = new LinkedList<>();
        for (int i = 0; i < id.size(); i++) {
            fileList.add(id.get(i));
        }

        // 得到 文件名list也就是id list C001002000_001
        for (int i = 0; i < id.size(); i++) {
            id.set(i, id.get(i).substring(35, 49));
        }

        // 得到script内容
        List<String> scripts = new LinkedList<>();
        for (int i = 0; i < fileList.size(); i++) {
            // System.out.println(readToString(fileList.get(i)));
            scripts.add(readToString(fileList.get(i)));
        }
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            PreparedStatement pstmt;
            // 执行查询
            for (int i = 0; i < scripts.size(); i++) {
                String sql = "update capability set function_script=' " + scripts.get(i) + "' where id='" + id.get(i)
                        + "'";
                pstmt = (PreparedStatement) conn.prepareStatement(sql);
                pstmt.executeUpdate();
                pstmt.close();
            }

            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            } // 什么都不做
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }

    public static List<String> getFileList(String strPath) {
        List<String> filelist = new LinkedList<String>();
        File dir = new File(strPath);
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (files[i].isDirectory()) { // 判断是文件还是文件夹
                    return null; // 获取文件绝对路径
                } else if (fileName.endsWith("lua")) { // 判断文件名是否以.avi结尾
                    StringBuffer strFileName = new StringBuffer(files[i].getAbsolutePath());
                    strFileName.insert(2, '\');
                    strFileName.insert(9, '\');
                    strFileName.insert(19, '\');
                    strFileName.insert(28, '\');
                    strFileName.insert(33, '\');
                    filelist.add(strFileName.toString());
                } else {
                    continue;
                }
            }
        }
        return filelist;
    }
}
原文地址:https://www.cnblogs.com/GoForMyDream/p/8625355.html