从res / raw文件夹中读取文本文件

时间:2013-10-24 23:51:22

标签: java android r.java-file

我试图使用final Scanner input = new Scanner(new File(R.raw.xmlsource)).useDelimiter("[\\;]+");从res中的原始文件夹中读取一个文本文件但由于资源ID在R.java文件中为int而无法读取,我的代码位于

之下

编辑对我的代码进行了一些更改,仍然没有运行但是使用调试器我可以告诉它实际上是在读取文件,问题似乎是在String[] RssLinksArray = readLine.split("[\\;]+");代码终止了,我不能为我的生活找出原因。我也附加了logcat。

非常感谢任何帮助。

package com.simplerssreader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
import java.util.List;
import java.util.Scanner;

import org.xmlpull.v1.XmlPullParserException;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;

public class RssService extends IntentService 
{
public static final String ITEMS = "items";
public static final String RECEIVER = "receiver";

public RssService() 
{
    super("RssService");
}

@Override
protected void onHandleIntent(Intent intent) 
{   
    InputStream is = getResources().openRawResource(R.raw.xmlsource);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String readLine = null;

    try {
        while ((readLine = br.readLine()) != null) 
        {


        }
    } catch (IOException e) 
    {
        e.printStackTrace();
    }

    String[] RssLinksArray = readLine.split("[\\;]+");
    final String RSS_LINK = RssLinksArray[0];

    Log.d(Constants.TAG, "Service started");
    List<RssItem> rssItems = null;
    try 
    {
        XMLRssParser parser = new XMLRssParser();
        rssItems = parser.parse(getInputStream(RSS_LINK));
    } 
    catch (XmlPullParserException e) 
    {
        Log.w(e.getMessage(), e);
    } 
    catch (IOException e) 
    {
        Log.w(e.getMessage(), e);
    }
    Bundle bundle = new Bundle();
    bundle.putSerializable(ITEMS, (Serializable) rssItems);
    ResultReceiver receiver = intent.getParcelableExtra(RECEIVER);
    receiver.send(0, bundle);
}

public InputStream getInputStream(String link) 
{
    try 
    {
        URL url = new URL(link);
        return url.openConnection().getInputStream();
    } catch (IOException e) 
    {
        Log.w(Constants.TAG, "Exception while retrieving the input stream", e);
        return null;
    }
}
}

logcat的

10-24 23:07:49.908: D/dalvikvm(1189): GC_FOR_ALLOC freed 101K, 9% free 2778K/3040K, paused 68ms, total 72ms
10-24 23:07:49.938: I/dalvikvm-heap(1189): Grow heap (frag case) to 3.939MB for 1127536-byte allocation
10-24 23:07:50.089: D/dalvikvm(1189): GC_FOR_ALLOC freed 2K, 7% free 3877K/4144K, paused 149ms, total 149ms
10-24 23:07:50.461: W/dalvikvm(1189): threadid=11: thread exiting with uncaught exception (group=0x41465700)
10-24 23:07:50.504: E/AndroidRuntime(1189): FATAL EXCEPTION: IntentService[RssService]
10-24 23:07:50.504: E/AndroidRuntime(1189): java.lang.NullPointerException
10-24 23:07:50.504: E/AndroidRuntime(1189):     at com.simplerssreader.RssService.onHandleIntent(RssService.java:48)
10-24 23:07:50.504: E/AndroidRuntime(1189):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-24 23:07:50.504: E/AndroidRuntime(1189):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 23:07:50.504: E/AndroidRuntime(1189):     at android.os.Looper.loop(Looper.java:137)
10-24 23:07:50.504: E/AndroidRuntime(1189):     at android.os.HandlerThread.run(HandlerThread.java:61)
10-24 23:07:50.899: D/libEGL(1189): loaded /system/lib/egl/libEGL_emulation.so
10-24 23:07:50.940: D/(1189): HostConnection::get() New Host Connection established 0x2a1db628, tid 1189
10-24 23:07:51.078: D/libEGL(1189): loaded /system/lib/egl/libGLESv1_CM_emulation.so
10-24 23:07:51.279: D/libEGL(1189): loaded /system/lib/egl/libGLESv2_emulation.so
10-24 23:07:51.699: W/EGL_emulation(1189): eglSurfaceAttrib not implemented
10-24 23:07:51.729: D/OpenGLRenderer(1189): Enabling debug mode 0
10-24 23:07:51.769: I/Choreographer(1189): Skipped 85 frames!  The application may be doing too much work on its main thread.
10-24 23:07:56.358: I/Choreographer(1189): Skipped 265 frames!  The application may be doing too much work on its main thread.

1 个答案:

答案 0 :(得分:0)

InputStream inputStream = getResources().openRawResource(R.raw.xmlsource);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));