java程序,在idea中正常运行,打成jar包后不能运行。

问题描述:正在使用Java GUI做一个程序,目的是可以在程序中,添加和删除数据。在IDEA下可以正常运行(读、写本地数据都正常)。但是导出为jar包后,能读取本地txt文件,而在程序(.jar 文件)中把数据写入到本地文件时,不能保存所写入的数据。在cmd中运行无报错信息。

相关代码块:

BufferedWriter bw = null;
try {
    File file = new File("./src/name_list.txt");
    bw = new BufferedWriter(new FileWriter(file));
    for (String name : tempList) {
        bw.write(name);
        bw.newLine();
        bw.flush();
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (bw != null) {
        try {
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

操作环境: windows

File file = new File("./src/name_list.txt");
这个路径有点问题,打包后没有src文件夹了。

第3行文件路径的问题哈
可以直接写成:
File file = new File("name_list.txt");

这样文件会生成到jar同级目录