2013-03-26 126 views
-1

我目前正在学习Java。我正在制作一个程序来获取给定URL的html代码,然后将代码保存为* .html。在代码的最后,我试图打印一条消息,确定该文件是否已保存。布尔总是返回true

我的问题是,确定文件是否已保存的布尔总是返回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()方法。

任何帮助,将不胜感激:)

+6

内部'saved'不应该在它前面有'boolean'。你把外面的'保存'超出了范围,这是暂时的阴影。 'saved = true',而不是'boolean saved = true'。 – pickypg 2013-03-26 19:35:35

+0

在'try'里面的'saved'之前删除'boolean'' – Maroun 2013-03-26 19:35:41

+2

从'boolean saved = true;'中删除'boolean''但是你不使用任何IDE?因为这应该表明你编译错误 – Tako 2013-03-26 19:37:22

回答

3
try { 
    writer.write(content); 
    saved = true; // no need to initialize again 
} finally { 
    writer.close(); 
} 
+0

我并不反对这个错误,但是这并不能解释它为什么总是返回*真*:他的代码总是返回false。我怀疑这个标题是不正确的......:/ – Bohemian 2013-03-26 19:45:19

+0

@Bohemian:为什么它会成为'假'(只要没有'IOException')? – jlordo 2013-03-26 19:47:44

+0

@jlordo OP的代码,而不是代码回答 – Bohemian 2013-03-26 23:05:28

0

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

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"); 
} 
+2

顶部'saved'不是全局变量。 – pickypg 2013-03-26 19:38:35

+0

@pickypg第一行只是告诉用户先要知道什么。我知道它不是全球性的,但是在方法的背景下,它可以被视为方法级别的全局,并且被视为从级别级别的本地。 – 2013-03-26 19:41:41

+0

本地到'class'将是一个字段/成员,对于实例方法来说更类似于全局。 – pickypg 2013-03-26 20:17:37