English 中文(简体)
java IO类[封闭式]需要帮助
原标题:Need help in java IO class [closed]
  • 时间:2012-04-15 07:09:32
  •  标签:
  • java
  • java-io

我需要IIO java守则的帮助,我正试图将java console输出[从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());
        }
    }
}
问题回答

It s actually almost all there, you just need to know what each line of code does to know how to put it together.

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

    }
}

你们正在将案文引向青少年,为什么不将其写到档案中?

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

Now two code sections are joined together to do what you want. I didn t run the code. Check it s correctness and also add error handling code.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签