-1

我需要有关 IO java 代码的帮助,我正在尝试将 java 控制台输出 [来自 URL 类] 保存到本地机器中的文件中,我能够获得输出需要帮助将该文件保存到本地机器

为了得到输出我做了这个编码

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

2 回答 2

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());
        }

    }
}
于 2012-04-15T07:37:13.977 回答
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();
    }
}

现在将两个代码部分连接在一起以执行您想要的操作。我没有运行代码。检查它的正确性并添加错误处理代码。

于 2012-04-15T07:50:08.813 回答