Java 布尔值总是返回true

Java 布尔值总是返回true,java,boolean,Java,Boolean,我目前正在学习Java。我正在制作一个程序,获取给定URL的html代码,然后将代码保存为*.html。在代码末尾,我试图打印一条消息,说明文件是否已保存 我的问题是,确定文件是否保存的布尔值总是返回true 这是我的密码: public class URLClient { protected URLConnection connection; public static void main(String[] args){ URLClient client = new URLClien

我目前正在学习Java。我正在制作一个程序,获取给定URL的html代码,然后将代码保存为*.html。在代码末尾,我试图打印一条消息,说明文件是否已保存

我的问题是,确定文件是否保存的布尔值总是返回true

这是我的密码:

public class URLClient {
protected URLConnection connection;

public static void main(String[] args){
    URLClient client = new URLClient();
    Scanner input = new Scanner(System.in);
    String urlInput;
    String fileName;

    System.out.print("Please enter the URL of the web page you would like to download: ");
    urlInput = input.next();

    System.out.println("Save file As: ");
    fileName = input.next();


    String webPage = client.getDocumentAt(urlInput);
    System.out.println(webPage);
    writeToFile(webPage, fileName);

}

public String getDocumentAt (String urlString){
    StringBuffer document = new StringBuffer();

    try{
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String line = null;
        while ((line = reader.readLine()) != null)
            document.append(line + "\n");
        reader.close();
    }catch (MalformedURLException e){
        System.out.println("Unable to connect to URL: " + urlString);
    }catch (IOException e){
        System.out.println("IOException when connectin to URL: " + urlString);
    }

    return document.toString();
}

public static void writeToFile(String content, String fileName){
    boolean saved = false;

    try{
        OutputStream outputStream = new FileOutputStream(new File(fileName));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));

        try{
            writer.write(content);
                   boolean saved = true;
        } finally {
            writer.close();
        }
    } catch (IOException e){
        System.out.println("Caught exception while processing file: " + e.getMessage());
    }

    if (saved)
        System.out.print("Successfully saved " + fileName + ".html");

}
}

布尔值称为“已保存”,并位于writeToFile()方法中


非常感谢您的帮助:)

您需要了解局部变量和全局变量的概念。您已使用相同的数据类型对相同的变量名进行了两次初始化。我已经为您相应地更改了代码

public static void writeToFile(String content, String fileName){
boolean saved = false;

try{
    OutputStream outputStream = new FileOutputStream(new File(fileName));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));

    try{
        writer.write(content);
        saved = true;
    } finally {
        writer.close();
    }
} catch (IOException e){
    System.out.println("Caught exception while processing file: " + e.getMessage());
}
if (saved)
    System.out.print("Successfully saved " + fileName + ".html");
}

内部
保存的
前面不应该有
布尔值。您正在将外部的
已保存的
置于范围之外,该范围暂时被隐藏
saved=true
而不是
boolean saved=true
。在
saved
之前删除
boolean
内部
尝试从
boolean saved=true中删除
boolean
但您不使用任何IDE吗?因为这应该表示您正在编译error@pickypg:正确的建议,错误的解释。由于重复的局部变量
saved
@KyleWilson,文章中的代码无法编译。你认为它为什么应该是
false
?顶部
saved
不是一个全局变量。@pickypg第一行只是让用户清楚地知道首先要做什么。我知道它不是全局的,但在方法的上下文中,它可以从方法级别被视为全局的,从类级别被视为局部的。对于
来说,局部将是一个字段/成员,这将更类似于实例方法的全局。我不同意这个错误,但这并不能解释为什么它总是返回true:他的代码总是返回false。我怀疑这个标题不正确…:/@波希米亚人:为什么会出现
错误
(只要没有
IOException
)?@jlordo OP的代码,而不是代码中的代码answer@jlordo
saved
在OP的代码中始终为
false
,因为
boolean saved=false从未更改(循环中保存的新本地变量始终为真)@Bohemian:OPs代码中没有循环。保存的
布尔值=falsewriteToFile()
方法的
try
块中,code>不是新的局部变量,它是由于保存了重复的局部变量
而导致的编译器错误。
public static void writeToFile(String content, String fileName){
boolean saved = false;

try{
    OutputStream outputStream = new FileOutputStream(new File(fileName));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));

    try{
        writer.write(content);
        saved = true;
    } finally {
        writer.close();
    }
} catch (IOException e){
    System.out.println("Caught exception while processing file: " + e.getMessage());
}
if (saved)
    System.out.print("Successfully saved " + fileName + ".html");
}