在java IO类中需要帮助

时间:2012-04-15 07:09:32

标签: java java-io

我需要IO java代码的帮助,我试图将java控制台输出[从URL类]保存到本地机器中的文件,我能够获得输出需要帮助将该文件保存到本地机器< / p>

得到输出我做了这个编码

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

2 个答案:

答案 0 :(得分: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());
        }

    }
}

答案 1 :(得分: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();
    }
}

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