2010-11-03 16 views
95

बातें सरल हैं, लेकिन माना जाता के रूप में काम नहीं करते।एंड्रॉयड पाठ कच्चे संसाधन फ़ाइल को पढ़ने

मैं एक पाठ फ़ाइल एक कच्चे संसाधन के रूप में जोड़ा है। पाठ फ़ाइल पाठ की तरह:

ख) यदि लागू कानून सॉफ़्टवेयर के संबंध में किसी भी वारंटी होते हैं, जिसे जैसे वारंटी लिमिटेड में अवधि नब्बे के लिए (डिलीवरी की तिथि से 90) दिन हैं ।

(ग) कोई मौखिक या लिखित जानकारी या सलाह आभासी ओरिएंटियरिंग द्वारा दिए गए इसके डीलरों, वितरकों, एजेंट या कर्मचारियों को एक वारंटी या किसी भी तरह से बनाएगी किसी भी वारंटी यहाँ उपलब्ध कराए गए क्षेत्र में वृद्धि ।

(घ) (यूएसए केवल) कुछ राज्य न करें निहित वारंटियों के बहिष्कार की अनुमति, अतः उपरोक्त बहिष्कार आप पर लागू नहीं हो सकता है। इस वारंटी GIVES आप विशिष्ट कानूनी अधिकारों और आप कर सकते हैं भी अन्य कानूनी अधिकार है कि भिन्न राज्य से दूसरे राज्य होगा।

मेरी स्क्रीन पर मैं इस तरह एक लेआउट है:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center" 
        android:layout_weight="1.0" 
        android:layout_below="@+id/logoLayout" 
        android:background="@drawable/list_background"> 

      <ScrollView android:layout_width="fill_parent" 
         android:layout_height="fill_parent"> 

        <TextView android:id="@+id/txtRawResource" 
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent" 
           android:padding="3dip"/> 
      </ScrollView> 

    </LinearLayout> 

कच्चे संसाधन को पढ़ने के लिए कोड है:

TextView txtRawResource= (TextView)findViewById(R.id.txtRawResource); 

txtDisclaimer.setText(Utils.readRawTextFile(ctx, R.raw.rawtextsample); 

public static String readRawTextFile(Context ctx, int resId) 
{ 
    InputStream inputStream = ctx.getResources().openRawResource(resId); 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

    int i; 
    try { 
     i = inputStream.read(); 
     while (i != -1) 
     { 
      byteArrayOutputStream.write(i); 
      i = inputStream.read(); 
     } 
     inputStream.close(); 
    } catch (IOException e) { 
     return null; 
    } 
    return byteArrayOutputStream.toString(); 
} 

पाठ का पता चला मिलता है लेकिन प्रत्येक पंक्ति के बाद मैं एक मिल अजीब चरित्र [] मुझे लगता है कि चरित्र को कैसे निकालूं? मुझे लगता है कि यह नई लाइन है।

श्रमजीवी समाधान

public static String readRawTextFile(Context ctx, int resId) 
{ 
    InputStream inputStream = ctx.getResources().openRawResource(resId); 

    InputStreamReader inputreader = new InputStreamReader(inputStream); 
    BufferedReader buffreader = new BufferedReader(inputreader); 
    String line; 
    StringBuilder text = new StringBuilder(); 

    try { 
     while ((line = buffreader.readLine()) != null) { 
      text.append(line); 
      text.append('\n'); 
     } 
    } catch (IOException e) { 
     return null; 
    } 
    return text.toString(); 
} 

उत्तर

54

क्या आप बाइट आधारित InputStream के बजाय एक अक्षर आधारित BufferedReader का उपयोग करता है, तो?

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 
String line = reader.readLine(); 
while (line != null) { ... } 

भूलें कि readLine() नई लाइनों को छोड़ देता है!

140

आप इस का उपयोग कर सकते हैं:

try { 
     Resources res = getResources(); 
     InputStream in_s = res.openRawResource(R.raw.help); 

     byte[] b = new byte[in_s.available()]; 
     in_s.read(b); 
     txtHelp.setText(new String(b)); 
    } catch (Exception e) { 
     // e.printStackTrace(); 
     txtHelp.setText("Error: can't show help."); 
    } 
+4

मुझे यकीन है कि Inputstream.available(), यहाँ सही विकल्प है बल्कि == -1 एक ByteArrayOutputStream तक एन एन पढ़ नहीं हूँ। – ThomasRS

+12

यह बड़े संसाधनों के लिए काम नहीं कर सकता है। यह इनपुटस्ट्रीम रीड बफर के आकार पर निर्भर करता है और केवल संसाधन का एक हिस्सा लौटा सकता है। – d4n3

+4

@ d4n3 सही है, इनपुट धारा उपलब्ध विधि राज्यों के प्रलेखन: "बाइट्स कि या पढ़ा जा सकता है और अधिक निवेश के लिए ब्लॉक किए बिना छोड़ दिया की अनुमानित संख्या देता है नोट है कि इस विधि में इस तरह के एक कमजोर गारंटी नहीं है कि ऐसा नहीं है प्रदान करता है। अभ्यास में बहुत उपयोगी " – ozba

2

यह एक और तरीका है जो निश्चित रूप से काम करेंगे, लेकिन मैं प्राप्त नहीं कर सकते यह एक ही गतिविधि में कई textviews में देखने के लिए एकाधिक पाठ फ़ाइलों को पढ़ने में, किसी को भी मदद कर सकते हैं?

TextView helloTxt = (TextView)findViewById(R.id.yourTextView); 
    helloTxt.setText(readTxt()); 
} 

private String readTxt(){ 

InputStream inputStream = getResources().openRawResource(R.raw.yourTextFile); 
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

int i; 
try { 
i = inputStream.read(); 
while (i != -1) 
    { 
    byteArrayOutputStream.write(i); 
    i = inputStream.read(); 
    } 
    inputStream.close(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 

return byteArrayOutputStream.toString(); 
} 
1

आप की तरह

TextView tv ; 
findViewById(R.id.idOfTextView); 
tv.setText(readNewTxt()); 
private String readNewTxt(){ 
InputStream inputStream = getResources().openRawResource(R.raw.yourNewTextFile); 
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

int i; 
try { 
i = inputStream.read(); 
while (i != -1) 
    { 
    byteArrayOutputStream.write(i); 
    i = inputStream.read(); 
    } 
    inputStream.close(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
e.printStackTrace(); 
} 

return byteArrayOutputStream.toString(); 
} 
24

समान तरीके से ऐसा कर सकते हैं @borislemke आप अपाचे "कॉमन्स-कब" से IOUtils का उपयोग करते हैं तो यह और भी आसान है:

InputStream is = getResources().openRawResource(R.raw.yourNewTextFile); 
String s = IOUtils.toString(is); 
IOUtils.closeQuietly(is); // don't forget to close your streams 

निर्भरता : http://mvnrepository.com/artifact/commons-io/commons-io

Maven:

<dependency> 
    <groupId>commons-io</groupId> 
    <artifactId>commons-io</artifactId> 
    <version>2.4</version> 
</dependency> 

Gradle:

'commons-io:commons-io:2.4' 
+0

IOUtils का उपयोग करने के लिए मुझे क्या आयात करना चाहिए? –

+1

अपाचे कॉमन्स-कब पुस्तकालय (http://commons.apache.org/proper/commons-io/)। या यदि आप मेवेन (http://mvnrepository.com/artifact/commons-io/commons-io) का उपयोग करते हैं। – tbraun

+6

Gradle के लिए: संकलन "कॉमन्स-कब: कॉमन्स-कब: 2.1" – JustinMorris

3

बल्कि यह इस तरह से कार्य करें:,

// reads resources regardless of their size 
public byte[] getResource(int id, Context context) throws IOException { 
    Resources resources = context.getResources(); 
    InputStream is = resources.openRawResource(id); 

    ByteArrayOutputStream bout = new ByteArrayOutputStream(); 

    byte[] readBuffer = new byte[4 * 1024]; 

    try { 
     int read; 
     do { 
      read = is.read(readBuffer, 0, readBuffer.length); 
      if(read == -1) { 
       break; 
      } 
      bout.write(readBuffer, 0, read); 
     } while(true); 

     return bout.toByteArray(); 
    } finally { 
     is.close(); 
    } 
} 

    // reads a string resource 
public String getStringResource(int id, Charset encoding) throws IOException { 
    return new String(getResource(id, getContext()), encoding); 
} 

    // reads an UTF-8 string resource 
public String getStringResource(int id) throws IOException { 
    return new String(getResource(id, getContext()), Charset.forName("UTF-8")); 
} 

एक गतिविधि से जोड़ने

public byte[] getResource(int id) throws IOException { 
     return getResource(id, this); 
} 

या एक परीक्षण का मामला से जोड़ने,

public byte[] getResource(int id) throws IOException { 
     return getResource(id, getContext()); 
} 

और अपने त्रुटि हैंडलिंग देखना - पकड़ने और अपवाद की अनदेखी जब अपने संसाधनों मौजूद होना चाहिए या कुछ और है (बहुत?) गलत नहीं है।

+0

क्या आपको 'openRawResource()' द्वारा खोला गया स्ट्रीम बंद करने की आवश्यकता है? –

+0

मुझे नहीं पता, लेकिन यह निश्चित रूप से मानक है। उदाहरण अपडेट कर रहा है। – ThomasRS

1

1.First एक निर्देशिका फ़ोल्डर बना सकते हैं और नाम यह रेस फ़ोल्डर 2.Create आप पहले और यह किसी भी नाम eg.articles.txt देना बनाई कच्चे निर्देशिका फ़ोल्डर के अंदर एक .txt फ़ाइल .... अंदर कच्चे 3.copy और पाठ आप .txt फ़ाइल के अंदर चाहते हैं आप "articles.txt" बनाया 4.dont अपने main.xml MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gettingtoknowthe_os); 

    TextView helloTxt = (TextView)findViewById(R.id.gettingtoknowos); 
    helloTxt.setText(readTxt()); 

    ActionBar actionBar = getSupportActionBar(); 
    actionBar.hide();//to exclude the ActionBar 
} 

private String readTxt() { 

    //getting the .txt file 
    InputStream inputStream = getResources().openRawResource(R.raw.articles); 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

    try { 
     int i = inputStream.read(); 
     while (i != -1) { 
      byteArrayOutputStream.write(i); 
      i = inputStream.read(); 
     } 
     inputStream.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return byteArrayOutputStream.toString(); 
} 

आशा है कि यह काम किया में एक TextView शामिल करने के लिए भूल जाते हैं पेस्ट!

0

यहाँ weekens की और Vovodroid के समाधान के मिश्रण चला जाता है।

यह weekens के समाधान की तुलना में अधिक पूर्ण Vovodroid के समाधान की तुलना में अधिक सही और है।

try { 
     InputStream inputStream = res.openRawResource(resId); 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 
      try { 
       StringBuilder result = new StringBuilder(); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        result.append(line); 
       } 
       return result.toString(); 
      } finally { 
       reader.close(); 
      } 
     } finally { 
      inputStream.close(); 
     } 
    } catch (IOException e) { 
     // process exception 
    } 
0
InputStream is=getResources().openRawResource(R.raw.name); 
BufferedReader reader=new BufferedReader(new InputStreamReader(is)); 
StringBuffer data=new StringBuffer(); 
String line=reader.readLine(); 
while(line!=null) 
{ 
data.append(line+"\n"); 
} 
tvDetails.seTtext(data.toString()); 
संबंधित मुद्दे