[英]Java: How do I export a program which uses a .txt file?
I created a program which uses a .txt file and wan't to know how I can use the .txt file when my program is exported. 我创建了一个使用.txt文件的程序,并且不知道导出程序时如何使用.txt文件。
Just ignore the comments in the code 只需忽略代码中的注释
Scanner cl;
// Creating a JFrame for the change-log
JFrame changelogFrame;
JTextArea changelogText;
JScrollPane scroll;
// Open the change-log file & setup changelogFrame, changelogText and scroll
public void openChangelog() {
// Setting up the changelogFrame
changelogFrame = new JFrame();
changelogFrame.setVisible(false);
changelogFrame.setDefaultCloseOperation(HIDE_ON_CLOSE);
changelogFrame.setSize(500, 500);
changelogFrame.setLocationRelativeTo(null);
changelogFrame.setTitle("Change-log");
// Setting up the changelogText
changelogText = new JTextArea();
changelogText.setEditable(false);
changelogText.setMargin(new Insets(5, 5, 5, 5));
// Setting up the scroll (Scroll-pane for changelogText)
scroll = new JScrollPane(changelogText, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
changelogFrame.add(scroll);
try{
cl = new Scanner(new File("Change-log.txt"));
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "An error occurred while loading the changelog...", "Error loading changelog", JOptionPane.ERROR_MESSAGE);
}
}
// Read the change-log file
public void readChangelog() {
cl.useDelimiter("\\Z");
String loadChangelog = cl.next();
changelogText.setText(loadChangelog);
changelogFrame.setVisible(true);
}
// Close the change-log file
public void closeChangelog() {
cl.close();
}
When I export my program to a Runnable JAR file
and try to load the Change-log.txt
it won't load. 当我将程序导出到Runnable JAR file
并尝试加载Change-log.txt
,它将不会加载。 I get the error message. 我收到错误消息。
How can I export my program and use the Change-log.txt
without any errors? 如何导出程序并使用Change-log.txt
而没有任何错误? Is the Change-log.txt
not being exported with the source code? 是否未将Change-log.txt
与源代码一起导出?
You should include the exceptions message in your error-message. 您应该在错误消息中包含例外消息。 That way, you'll have more information on what goes wrong, and can debug this yourself. 这样,您将获得有关发生问题的更多信息,并可以自己进行调试。
I would assume that the file you want to read out from does not exist in the folder your exported program is executed in. In that case, you either need to handle this case, or create it, depending on your end-goal. 我假设您要读取的文件在执行导出程序的文件夹中不存在。在这种情况下,您需要处理此情况,还是根据最终目标创建它。
Simple and short method of doing this (while cutting some corners) using part of your code is: 使用部分代码来执行此操作的简单而简短的方法是:
String path = System.getProperty("user.home");
path = (path+"\\Change-log.txt")
cl = new Scanner(new File(path));`
This way you always place the file into users home directory, where the read/write privileges are ussually given, and you can also write that file there with some other method, following the same structure I have given you 这样,您总是将文件放置在用户的主目录中,该目录通常具有读/写权限,并且您也可以按照我给您的相同结构,用其他方法在该目录中写入该文件。
You will probably also need to write a piece of code that generates the changelog for the user, if it is not present in the home dir. 如果主目录中不存在更改日志,则可能还需要编写一段代码来为用户生成更改日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.