2014-05-02 66 views

回答

1

是當然的,你只需要像做

public static void main(String[] args) { 

    URL url; 

    try { 
     // get URL content 
     url = new URL("http://www.mkyong.com"); 
     URLConnection conn = url.openConnection(); 

     // open the stream and put it into BufferedReader 
     BufferedReader br = new BufferedReader(
          new InputStreamReader(conn.getInputStream())); 

     String inputLine; 

     //save to this filename 
     String fileName = "https://stackoverflow.com/users/mkyong/test.html"; 
     File file = new File(fileName); 

     if (!file.exists()) { 
      file.createNewFile(); 
     } 

     //use FileWriter to write file 
     FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
     BufferedWriter bw = new BufferedWriter(fw); 

     while ((inputLine = br.readLine()) != null) { 
      bw.write(inputLine); 
     } 

     bw.close(); 
     br.close(); 

     System.out.println("Done"); 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

出處:mkyong

1

你可能想看看番石榴的CharStreams類。

CharStreams.toString(new InputStreamReader(..)) 

將使您免於撰寫大量樣板代碼。

Here is doc

相關問題