将生成的文件输入到程序中 [英] Input a generated file back into the program

查看:136
本文介绍了将生成的文件输入到程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,我需要从html表中插入数据到数据库。我已经想通过首先将html表转换为.csv,然后从csv导入到数据库很简单。



我发现一个书面的脚本在线转换将html表格转换成csv。 TableToCSV



但是,它有一个局限性,就是在用户选择.html文件的同一文件夹中生成输出文件.csv。因此,我不知道如何将这个新生成的csv文件给我的BufferedReader。


$ b

 final JFileChooser fileDialog = new JFileChooser ; 
JButton btnInputFile = new JButton(Input File);
btnInputFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
$ b $ int returnVal = fileDialog.showOpenDialog(rootPane);
if(returnVal == JFileChooser.APPROVE_OPTION){
java.io.File file = fileDialog.getSelectedFile();

String name = file.getName(); $ b $ name = name.substring (0,name.lastIndexOf(。));
name + =.html;
File newFile = new File(file.getParentFile(),name);
if file.renameTo(newFile)){
try {
TableToCSV tableToCSV = new TableToCSV(newFile,',','\,'#',CSV.UTF8Charset);
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

try
{
BufferedReader br = new BufferedRead呃(新的FileReader(newFile));
字符串行; ((line = br.readLine())!= null)


String [] value = line.split(,);
字符串sql =插入到主(票证号,状态,优先级,部门,帐户名称)
+values('+ value [0] +','+ value [ 1] +, ' +值[2] +', ' +值[3] +', ' +值[4] +', ' +值[5] +') ;

PreparedStatement pst = DatabaseConnection.ConnectDB()。prepareStatement(sql);
pst.executeUpdate();

}
br.close();


$ b catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}

}

}
});
btnInputFile.setFont(new Font(Segoe UI Symbol,Font.BOLD,15));
btnInputFile.setBounds(34,31,122,51);
getContentPane()。add(btnInputFile);

正如您所看到的,在BufferedReader中输入newFile是不正确的。请建议一种方法来编辑TableToCSV文件,这样我就不能把输出文件放到一个我可以进一步放到我的BufferedReader中的位置。 方案


我在网上发现了一个脚本,将html表转换成csv。 TableToCSV

然而,它有一个局限性,它会在同一个文件夹中生成输出文件.csv


从我所了解的Roedy Green(编写代码的人)中,我预计源代码旨在教你如何做一些事情。这个东西是创建一个CSV。这个想法是你可以自由地把它改变,以适应你的使用情况。事实上,许可证是在顶部:


这个软件可以被复制和自由地用于 任何目的但是军事。

我建议改变方法签名来接受 2个文件其中一个是现有的输入文件,第二个是预定的输出文件(并且不要在您的ICBM中使用它)。


I'm making a application in which I need to insert values from a html table to a database. I've figured its possible by first converting the html table to a .csv and then importing from csv to a database is simple.

I found a written script online that converts the html table into a csv. TableToCSV

However, it has a limitation that it produces the output file .csv in the same folder as, where user selects the .html file. And hence I do not know how to give this newly generated csv file to my BufferedReader.

 final JFileChooser  fileDialog = new JFileChooser();
JButton btnInputFile = new JButton("Input File");
btnInputFile.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        int returnVal = fileDialog.showOpenDialog(rootPane);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
           java.io.File file = fileDialog.getSelectedFile();

           String name = file.getName();
           name = name.substring(0, name.lastIndexOf("."));
           name += ".html";
           File newFile = new File(file.getParentFile(), name);
           if (file.renameTo(newFile)) {
               try {
                TableToCSV tableToCSV = new TableToCSV(newFile, ',', '\"', '#', CSV.UTF8Charset );
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           }

           try
           {
            BufferedReader br=new BufferedReader(new FileReader(newFile));
            String line;

            while((line=br.readLine())!=null)
            {
                String[]value = line.split(",");
                String sql = "INSERT into main ( , Ticket #, Status, Priority, Department, Account Name) "
                 + "values ('"+value[0]+"','"+value[1]+"','"+value[2]+"','"+value[3]+"','"+value[4]+"','"+value[5]+"')";

                 PreparedStatement pst = DatabaseConnection.ConnectDB().prepareStatement(sql);
                 pst.executeUpdate();

            }
            br.close();

           }

           catch(Exception e)
           {
            JOptionPane.showMessageDialog(null, e);
           }

        }

    }
});
btnInputFile.setFont(new Font("Segoe UI Symbol", Font.BOLD, 15));
btnInputFile.setBounds(34, 31, 122, 51);
getContentPane().add(btnInputFile);

As you can see, entering newFile inside BufferedReader is not correct. Please suggest a way to either edit the TableToCSV file such that I can't bring the output file into a location where I can further put it in my BufferedReader.

解决方案

I found a written script online that converts the html table into a csv. TableToCSV

However, it has a limitation that it produces the output file .csv in the same folder

From what I know of Roedy Green (who wrote that code), I expect the source code is intended to teach you how to do something. That 'something' is 'create a CSV'. The idea is you are free to take it and alter it to the point where it works for your use-case. In fact, the license is stated at the top:

This software may be copied and used freely for any purpose but military.

I'd suggest altering the method signature to accept 2 files where one is the existing input file, and the 2nd is the intended output file (and don't use it in your ICBM).

这篇关于将生成的文件输入到程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆