使用notepad通过java在子目录中打开txt

时间:2015-09-24 03:02:54

标签: java windows user-interface filepath subdirectory

我从昨天起就一直在浏览网站,我看不到任何可以回答我问题的内容,所以我决定问一下。

我正在制作一个非常基本的java GUI,它被设计为与实际java包中不包含的文件一起运行以实现兼容性和更容易定制这些文件,我怀疑它们可以被包含在内,因为它们有拥有.jars和其他东西。

所以,我遇到的问题是GUI应用程序位于主文件夹中,我需要它来在记事本中找到并打开几个子文件夹的txt文件,而不需要完整的文件路径,因为我会在完成后将这个项目交给某些人。

目前我一直在使用它来打开文件,但只适用于主文件夹中的文件,并尝试在任何文件路径中编辑都不起作用。

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    Runtime rt=Runtime.getRuntime();

    String file;
    file = "READTHIS.txt";

    try {
        Process p=rt.exec("notepad " +file);
    } catch (IOException ex) {
        Logger.getLogger(NumberAdditionUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}  

如果有人知道如何做到这一点,那就太好了。

另一方面,我想在实际的java包中包含上面显示的文件(READTHIS.txt),我将把文件放在哪里,我应该如何指向它?

我已经离开java很长一段时间了,所以我几乎忘了什么,所以很简单的解释非常感谢。 感谢任何阅读此内容的人,任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

更新2

因此,我添加了ConfigBox.java源代码,并在记事本中将jButton1打开home\doc\READTHIS.txt。我创建了一个可执行文件jar,并通过jar执行了java -jar Racercraft.jar,如下图所示。只需举例说明我在ConfigBox.java中所做的操作,并将其应用于每个NumberAdditionUI.java的{​​{1}},并确保将JButtons变量更改为相应的文件名你想打开。

注意:下面图片中filePath的内容在测试期间已更改,我的下面的代码不会更改JTextArea的内容。 Working jar...

目录结构:

\home
    Rasercraft.jar
    \docs
        READTHIS.txt

<强>代码:

JTextArea


更新

这是快速而肮脏的方法,如果您希望我添加更优雅的解决方案,请告诉我。请注意,文件名及其相对路径是硬编码为字符串数组。

文件夹层次结构的图像:

Directory structure.

<强>代码:
注意 - 这仅适用于Windows。

// imports and other code left out

public class ConfigBox extends javax.swing.JFrame {
    // curDir will hold the absolute path to 'home\'
    String curDir; // add this line

    /**
     * Creates new form ConfigBox
     */
    public ConfigBox() 
    {
        // this is where curDir gets set to the absolute path of 'home/'
        curDir = new File("").getAbsolutePath(); // add this line

        initComponents();
    }

    /*
     * irrelevant code
     */

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Runtime rt = Runtime.getRuntime();

        // filePath is set to 'home\docs\READTHIS.txt'
        String filePath = curDir + "\\docs\\READTHIS.txt"; // add this line

        try {
            Process p = rt.exec("notepad " + filePath); // add filePath
        } catch (IOException ex) {
            Logger.getLogger(NumberAdditionUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    /*
     * irrelevant code
     */


替代方法

您可以使用import java.io.File; import java.io.IOException; public class Driver { public static void main(String[] args) { final String[] FILE_NAMES = {"\\files\\READTHIS.txt", "\\files\\sub-files\\Help.txt", "\\files\\sub-files\\Config.txt" }; Runtime rt = Runtime.getRuntime(); // get the absolute path of the directory File cwd = new File(new File("").getAbsolutePath()); // iterate over the hard-coded file names opening each in notepad for(String file : FILE_NAMES) { try { Process p = rt.exec("notepad " + cwd.getAbsolutePath() + file); } catch (IOException ex) { // Logger.getLogger(NumberAdditionUI.class.getName()) // .log(Level.SEVERE, null, ex); } } } } 类打开一个对话框,允许用户在记事本中选择要打开的文件的位置。

JFileChooser dialog


我刚刚使用代码中的相关部分编写了这个快速示例:

javax.swing.JFileChooser

我还提供了一些指向Oracle提供的有关import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Driver extends JFrame implements ActionListener { JFileChooser fileChooser; // the file chooser JButton openButton; // button used to open the file chooser File file; // used to get the absolute path of the file public Driver() { this.fileChooser = new JFileChooser(); this.openButton = new JButton("Open"); this.openButton.addActionListener(this); // add openButton to the JFrame this.add(openButton); // pack and display the JFrame this.pack(); this.setVisible(true); } public void actionPerformed(ActionEvent e) { // handle open button action. if (e.getSource() == openButton) { int returnVal = fileChooser.showOpenDialog(Driver.this); if (returnVal == JFileChooser.APPROVE_OPTION) { // from your code Runtime rt = Runtime.getRuntime(); try { File file = fileChooser.getSelectedFile(); String fileAbsPath = file.getAbsolutePath(); Process p = rt.exec("notepad " + fileAbsPath); } catch (IOException ex) { // Logger.getLogger(NumberAdditionUI.class.getName()) // .log(Level.SEVERE, null, ex); } } else { System.exit(1); } } } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { Driver driver = new Driver(); } }); } } API的有用信息的链接:How to use File Choosers。如果您需要任何帮助来搞清楚代码,请通过评论告诉我,我会尽力帮助您。

至于在实际的java包中加入FileChooser,请看看其他StackOverflow问题: