Java 布尔值总是返回true
我目前正在学习Java。我正在制作一个程序,获取给定URL的html代码,然后将代码保存为*.html。在代码末尾,我试图打印一条消息,说明文件是否已保存 我的问题是,确定文件是否保存的布尔值总是返回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
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@jlordosaved
在OP的代码中始终为false
,因为boolean saved=false代码>从未更改(循环中保存的新本地变量始终为真)@Bohemian:OPs代码中没有循环。保存的布尔值=false在他的writeToFile()
方法的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");
}