在Java程序中使用args运行Java程序

在Java程序中使用args运行Java程序

本文介绍了Java - 在Java程序中使用args运行Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个打开JFileChooser的JButton,然后选择一个文件到一个名为file的变量中。

我想将所选文件重命名为Best .html,然后将其提供给(java TableToCSV.class Best.html),将选定的文件转换为.csv格式。



这是我的代码 - $ / b
$ 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();



$ b});

注意 - 我想重命名,因为TableToCSV.java文件只输入文件扩展名为.html。
$ b

注意 - TableToCSV.java位于与我的java程序相同的文件夹中。

解决方案

您有两个选择...



您可以...



使用 TableToCSV main 方法...

  TableToCSV.main(new String [] {file.getAbsolutePath()}); 

从命令行调用它基本上相同

或者您可以...



使用 TableToCSV 构造函数...

  TableToCSV tableToCSV = new TableToCSV(file,',','\'','#',CSV.UTF8Charset); 

问题在于, TableToCSV 文件名与4个字符的扩展名...所以,如果你要传递一个文件扩展名为 .txt ,结果文件不会像预期的那样出现,并且实际上可能导致一些危险。



在这种情况下,您可以使用

  String name = file.getName(); 
name = name.subString(0,name.lastIndexOf(。)); $ b $文件newFile = new File(file.getParentFile(),name);
if(file.renameTo(newFile)){
TableToCSV tableToCSV = new TableToCSV(newFile,',','\','#', CSV.UTF8Charset);
}

但是我讨厌重命名文件...

I have a JButton which opens JFileChooser and that in turn selects a file into a variable called "file".

I want to rename the selected file to "Best.html" and then provide it to TableToCSV.java (java TableToCSV.class Best.html), which will convert the selected file to a .csv format.

Here is my code -

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();

            }

        }
    });

Note - I want to rename because the TableToCSV.java file only inputs file with a .html extension.

Note - TableToCSV.java lies in the same folder as my java program.

解决方案

You have two options...

You Could...

Use TableToCSV main method...

TableToCSV.main(new String[]{file.getAbsolutePath()});

Which is essentially the same as calling it from the command line

Or You Could...

Use the TableToCSV constructor...

TableToCSV tableToCSV = new TableToCSV( file, ',', '\"', '#', CSV.UTF8Charset );

The problem with this is, TableToCSV is expecting a file name with a 4 char extension...So if you were to pass it a File with an extension of .txt, the resulting file won't appear as you expect it and could actually lead to some danger.

In this case you could use

String name = file.getName();
name = name.subString(0, name.lastIndexOf("."));
name += ".html";
File newFile = new File(file.getParentFile(), name);
if (file.renameTo(newFile)) {
    TableToCSV tableToCSV = new TableToCSV( newFile, ',', '\"', '#', CSV.UTF8Charset );
}

But I do hate renaming files...

这篇关于Java - 在Java程序中使用args运行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:22