布尔文件在线文件

时间:2015-06-18 16:13:06

标签: java url boolean

我想在线阅读文本文件,然后检查文本文件第一行内的布尔值。我正在使用它来检查应用程序的更新。

这是一个想法 -

    public static boolean needsUpdate()
{
    URL url;
    boolean needs = false;
    try
    {
        url = new URL("https://github.com/../../blob/master/update.txt");
        Scanner s = new Scanner(url.openStream());

        boolean getUpdate = s.hasNextBoolean();

        if (getUpdate = true)
        {
            needs = true;
        }
        else
        {
            needs = false;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return needs;
}

文件中的文字为update = falseupdate = true

1 个答案:

答案 0 :(得分:2)

这样做:

try {
    URL url = new URL("https://github.com/../../blob/master/update.txt");
    Scanner s = new Scanner(url.openStream());
    String text = s.nextLine();
    s.close();
    text = text.replaceAll("update = ", "");
    Boolean getUpdate = Boolean.parseBoolean(text);
    //do something with the boolean
} catch(Exception ex) {
    ex.printStackTrace();
}