【问题标题】:I want know how I can write my InputStream content to an XML file (android)我想知道如何将 InputStream 内容写入 XML 文件(android)
【发布时间】:2011-09-09 23:55:16
【问题描述】:

嘿,我有以下代码:

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

class OpenStreamTest {
public static void main(String args[]) {
    try {
        URL yahoo = new URL("http://www.yahoo.com/");
        DataInputStream dis;
        String inputLine;

        dis = new DataInputStream(yahoo.openStream());
        while ((inputLine = dis.readLine()) != null) {
            System.out.println(inputLine);
        }
        dis.close();
    } catch (MalformedURLException me) {
        System.out.println("MalformedURLException: " + me);
    } catch (IOException ioe) {
        System.out.println("IOException: " + ioe);
    }
}
}

如何将我从中获得的源代码保存到 XML 文件中?请帮忙

【问题讨论】:

    标签: java android xml input


    【解决方案1】:

    这里是一个例子,“iso”是你的 InputSrteam

    try {
        final File file = new File("/sdcard/filename.xml");
        final OutputStream output = new FileOutputStream(file);
    
        try {  
            try {
                final byte[] buffer = new byte[1024];
                int read;
    
                while ((read = iso.read(buffer)) != -1)
                    output.write(buffer, 0, read);
    
                output.flush();
            }
            finally {
                output.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            iso.close();
            System.out.println("saved");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 记得添加SD卡存储使用权限!
    【解决方案2】:

    创建连接:

    DefaultHttpClient  httpclient = new DefaultHttpClient();
    HttpGet httppost = new HttpGet("http://www.google.com");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity ht = response.getEntity();
    BufferedHttpEntity buf = new BufferedHttpEntity(ht);
    InputStream is = buf.getContent();
    

    将输入流放入缓冲区并读取:

    BufferedReader r = new BufferedReader(new InputStreamReader(is2));
    total = new StringBuilder();
    String line;
    while ((line = r.readLine()) != null) {
        total.append(line);
    }
    

    然后放到文件中:

    File file = new File("/sdcard", "report.xml");
    if(!file.exists()){
        file.createNewFile();
    }
    
    StringBuilder temp = null;
    while ((inputLine = dis.readLine()) != null) {
         temp.append(inputLine);
    }
    
    FileWriter fw = new FileWriter(file);
    fw.write(temp.toString());
    fw.flush();
    

    希望对你有帮助

    【讨论】:

    • Nahhh 不正确,因为 Android java 有点不同。
    • 不,我在第一行得到一个错误(我做 android 2.1)你能 c/p 你拥有的整个代码吗?您能否在 MSN 或 Skype 中添加我以获得进一步的帮助? :(
    • <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 添加到您的AndroidManifest.xml
    • 它在 dls thingy 上有一个错误(我把它从 dis 重命名为 dls) StringBuilder temp = null; while ((inputLine = dls.readLine()) != null) { temp.append(inputLine); } FileWriter fw = new FileWriter(wlsource); fw.write(temp.toString()); fw.flush(); } } } }
    猜你喜欢
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多