java工程生成jar,配置文件放在jar外面,读写配置文件

public class Resource {
    public static void main(String[] args) {
        writeFile("562");
        readFileByChars();
    }

    public static void writeFile(String news_id) {
        FileOutputStream fsOut = null;
        String filePath = System.getProperty("user.dir")
                + "/conf/newsid.txt";
        try {
            fsOut = new FileOutputStream(filePath);
            byte[] b1 = news_id.getBytes();
            fsOut.write(b1);
            fsOut.close();
            System.out.println(news_id+"写入CT成功!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fsOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static String readFileByChars() {
        String weixinid = "";
        String filePath = System.getProperty("user.dir")
                + "/conf/newsid.txt";
        Reader reader = null;
        try {
            reader = new InputStreamReader(new FileInputStream(filePath));
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                if (((char) tempchar) != '
') {
                    // System.out.print((char) tempchar);
                    weixinid += (char) tempchar + "";
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return weixinid;
    }
}


原文地址:https://www.cnblogs.com/hzcya1995/p/13317841.html