简体   繁体   English

Java:如何导出使用.txt文件的程序?

[英]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.

相关问题 如何确定Java程序在启动时使用哪些库? - How do I find out which libraries a Java program uses on start-up? 如何获得程序以Java读取txt文件? - How do I get my program to read my txt file in Java? 如何对使用 Java UUID 的代码进行单元测试? - How do I unit test code which uses Java UUID? 如何在Java中下载/导出.txt文件? - How to download/export .txt file in java? 如何将使用数据库的Java桌面应用程序导出到其他PC? - how to export java desktop application which uses database to other pcs? 清除已在 Java 程序中使用的 a.txt 文件的内容 - Clear Contents of a .txt file which is already being used in Java Program 如何导出将在另一个Java项目中使用的jar以及jar使用的整个maven /外部依赖关系? - How do I export a jar that will be used in another Java project, along with the entire maven/external dependencies that the jar uses? 如何计算Java中txt文件中某个特定单词的出现? - How do I count an occurence of a specific word in a txt file in Java? 如何在 Java 中将 JTextField 输入保存到 .txt 文件? - How Do I Save JTextField Inputs to a .txt file in Java? 如何在 java 中将 a.txt 文件读入二维数组? - How do I read a .txt file into a 2D array in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM