【问题标题】:Need help in java IO class [closed]在java IO类中需要帮助[关闭]
【发布时间】:2012-04-15 07:09:32
【问题描述】:

我需要有关 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());
        }
    }
}

【问题讨论】:

  • 你的代码真的是这样的吗?你根本没有任何缩进吗?您的代码很难阅读。
  • 他们把 Shift 键放在键盘上是有原因的。这样你就不必一直大喊大叫了。当你大喊大叫时,它会让你的问题更难阅读,很烦人,而且不会让你更快地得到答案。 (此外,您的问题可能对您很重要,但其他人的问题对他们也很重要。乞求紧急或尽快帮助只会使您的问题变得混乱。只要不紧急请求,有人会尽快帮助您。 )
  • 写作部分,举个例子:stackoverflow.com/a/22074145/3315914

标签: java java-io


【解决方案1】:

其实差不多就这些了,你只需要知道每一行代码的作用就知道如何组合起来了。

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

    }
}

【讨论】:

  • 我在想怎么对你说谢谢[真相]。你是我的救星。
  • @Satyam:通过将答案标记为已接受的答案来表示感谢。有关详细信息,请参阅faq
【解决方案2】:

您正在将文本输出到控制台,为什么不将其写入文件?

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

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

【讨论】:

  • 非常感谢您的时间和精力
猜你喜欢
  • 1970-01-01
  • 2011-02-24
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 2021-11-12
  • 2014-09-22
  • 2014-08-31
相关资源
最近更新 更多