gpt4 book ai didi

java - 通过java用记事本打开子目录中的txt

转载 作者:可可西里 更新时间:2023-11-01 10:34:55 25 4
gpt4 key购买 nike

我从昨天开始就一直在浏览这个网站,但我找不到任何可以回答我问题的东西,所以我决定直接问问。

我正在制作一个非常基本的 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);
}
}

如果有人知道这样做的方法,那就太好了。

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

我已经离开 Java 很长时间了,所以我几乎忘记了任何东西,因此非常感谢更简单的解释。感谢任何阅读本文的人,任何帮助都会很棒。

最佳答案

更新2

所以我添加到ConfigBox.java 源代码并使jButton1 在记事本中打开home\doc\READTHIS.txt。我创建了一个可执行文件 jar 并通过 java -jar Racercraft.jar 执行了 jar,如下图所示。以我在 ConfigBox.java 中所做的为例,并将其应用于 NumberAdditionUI.java 的每个 JButtons,确保更改将 filePath 变量设置为您要打开的相应文件名。

注意:下图中JTextArea的内容在测试过程中发生了变化,我下面的代码没有改变JTextArea的内容。 Working jar...

目录结构:

\home    Rasercraft.jar    \docs        READTHIS.txt

Code:

// 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
*/


更新

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

文件夹层次结构的图像:

Directory structure.

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

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

}
}


替代方法

您可以使用 javax.swing.JFileChooser 类打开一个对话框,允许用户选择他们想要在记事本中打开的文件的位置。

JFileChooser dialog


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

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

}

我还提供了一个链接,指向一些有关 FileChooser API 的有用信息,由 Oracle 提供:How to use File Choosers .如果您在弄清楚代码时需要任何帮助,请通过评论告诉我,我会尽力提供帮助。

至于将 READTHIS.txt 包含在实际的 java 包中,看看这些其他 StackOverflow 问题:

关于java - 通过java用记事本打开子目录中的txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32752627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com