2012-04-15 66 views
-1

我需要幫助的IO Java代碼,我試圖保存java控制檯輸出[從URL類]到本地計算機中的文件,我能夠得到輸出需要幫助保存文件到本地機器需要幫助在Java IO類

得到的輸出我做這個編碼

import java.net.*; 
import java.io.*; 

public class URLConnectionReader { 
    public static void main(String[] args) throws Exception { 
     URL oracle = new URL("http://www.yahoo.com/"); 
     URLConnection yc = oracle.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
     yc.getInputStream())); 
     String inputLine; 
     while ((inputLine = in.readLine()) != null) 
      System.out.println(inputLine); 
     in.close(); 
    } 
} 

注:我有一個其他的代碼,我在其中可以輸出保存到一個文件,但無法共同既涉及這兩個,我已經嘗試延長,但沒有進展,這裏是該類

import java.io.*; 
class FileWrite { 
    public static void main(String args[]) { 
     try{ 
      // Create file 
      FileWriter fstream = new FileWriter("out.txt"); 
      BufferedWriter out = new BufferedWriter(fstream); 
      out.write("Hello Java"); 
      //Close the output stream 
      out.close(); 
     }catch (Exception e){//Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
     } 
    } 
} 
+4

這就是你的代碼真的看起來像什麼?你有沒有縮進?你的代碼很難閱讀。 – 2012-04-15 07:10:21

+4

他們把Shift鍵放在鍵盤上是有原因的。這是所以你不必一直都在。當你注意時,它會讓你的問題更難閱讀,這很煩人,而且它不會讓你更快得到答案。 (另外,你的問題可能對你很重要,但其他人對他們提出的問題也是如此,懇求緊急或儘快的幫助會讓你的問題混亂,只要問問緊急求助,有人會盡快幫助你。 ) – 2012-04-15 07:13:07

+0

對於寫作部分,這裏是一個例子:http://stackoverflow.com/a/22074145/3315914 – rpax 2014-02-28 18:40:14

回答

2

它實際上幾乎都在那裏,你只需要知道每行代碼如何知道如何把它放在一起。

import java.net.*; 
import java.io.*; 

public class WriteFromURL { 
    public static void main(String[] args) throws Exception { 
     try{ 

      // Block from connection reader 
      URL oracle = new URL("http://www.yahoo.com/"); 
      URLConnection yc = oracle.openConnection(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(
      yc.getInputStream())); 

      // Block from writer 
      // Create file 
      FileWriter fstream = new FileWriter("out.txt"); 
      BufferedWriter out = new BufferedWriter(fstream); 

      // Block from reader w/ small change 
      String inputLine; 
      while ((inputLine = in.readLine()) != null){ 
       // Write what you read 
       out.write(inputLine); 
       out.newLine(); 
      } 
      in.close(); 

      // Block from writer 
      //Close the output stream 
      out.close(); 
     }catch (Exception e){//Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
     } 

    } 
} 
+0

我在想如何對你說[thutheality] thx。你是我的救星。 – 2012-04-15 08:19:36

+0

@Satyam:將答案標記爲已接受的答案表示感謝。有關更多信息,請參閱[faq](http://stackoverflow.com/faq#howtoask)。 – 2012-04-15 08:44:42

1

您正在輸出文本到控制檯,爲什麼不把它寫入文件?

public class URLConnectionReader { 
    public static void main(String[] args) throws Exception { 
     URL yahoo = new URL("http://www.yahoo.com/"); 
     URLConnection yc = yahoo.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); 

     FileWriter fstream = new FileWriter("out.txt"); 
     BufferedWriter out = new BufferedWriter(fstream); 

     String inputLine; 
     while ((inputLine = in.readLine()) != null){ 
      out.write(inputLine); 
      System.out.println(inputLine); 
     } 
     out.close(); 
     in.close(); 
    } 
} 

現在兩個代碼段連在一起做你想做的事情。我沒有運行代碼。檢查它的正確性並添加錯誤處理代碼。

+0

非常感謝您的時間和精力 – 2012-04-15 08:20:04