通过 java 使用记事本打开子目录中的 txt
Opening txt in sub directories with notepad through java
我从昨天开始就一直在浏览这个网站,但我找不到任何可以回答我问题的东西,所以我决定问问。
我正在制作一个非常基本的 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
的内容。
目录结构:
\home
Rasercraft.jar
\docs
READTHIS.txt
代码:
// 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
*/
更新
这是一种快速而肮脏的方法,如果您希望我添加更优雅的解决方案,请告诉我。请注意,文件名及其相对路径是 hard-coded 作为字符串数组。
文件夹层次结构图:
代码:
注意 - 这仅适用于 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
class 打开一个对话框,允许用户 select 他们想在记事本中打开的文件的位置。
我刚刚使用您代码中的相关部分编写了这个快速示例:
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();
}
});
}
}
我还包含了 link 一些关于 FileChooser
API 的有用信息,由 Oracle 提供:How to use File Choosers。如果您在弄清楚代码时需要任何帮助,请通过评论告诉我,我会尽力提供帮助。
至于将 READTHIS.txt
包含在实际的 java 包中,请查看其他 Whosebug 问题:
Getting file from same package?
Reading a text file from a specific package?
How to include text files with executable jar?
Creating runnable jar with external files included?
Including a text file inside a jar file and reading it?
我从昨天开始就一直在浏览这个网站,但我找不到任何可以回答我问题的东西,所以我决定问问。
我正在制作一个非常基本的 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
的内容。
目录结构:
\home Rasercraft.jar \docs READTHIS.txt
代码:
// 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
*/
更新
这是一种快速而肮脏的方法,如果您希望我添加更优雅的解决方案,请告诉我。请注意,文件名及其相对路径是 hard-coded 作为字符串数组。
文件夹层次结构图:
代码:
注意 - 这仅适用于 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
class 打开一个对话框,允许用户 select 他们想在记事本中打开的文件的位置。
我刚刚使用您代码中的相关部分编写了这个快速示例:
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();
}
});
}
}
我还包含了 link 一些关于 FileChooser
API 的有用信息,由 Oracle 提供:How to use File Choosers。如果您在弄清楚代码时需要任何帮助,请通过评论告诉我,我会尽力提供帮助。
至于将 READTHIS.txt
包含在实际的 java 包中,请查看其他 Whosebug 问题:
Getting file from same package?
Reading a text file from a specific package?
How to include text files with executable jar?
Creating runnable jar with external files included?
Including a text file inside a jar file and reading it?