Java parsing JSON file method (ii)

Source: Internet
Author: User

Assets folder resource Access Assets folder files are kept in the original file format, you need to use Assetmanager as a byte stream to read the file. 1. First Call in activity getassets ()To get the Assetmanager reference. 2. Re-use the Assetmanager Open (String fileName, int accessmode)The InputStream method specifies the read file and the access mode to get the input stream. 3. Then you read the file with the InputStream of the open file, and remember to InputStream when the read is complete. Close ()。 4. Call Assetmanager. Close ()Close the Assetmanager.

It is important to note that files from resources and assets can only be read and cannot be written.
The following is read from the raw file:
Code

<div "=" "> Public String getfromraw () {
try {
InputStreamReader Inputreader = new InputStreamReader (Getresources (). Openrawresource (R.raw.test1));
BufferedReader bufreader = new BufferedReader (Inputreader);
String line= "";
String result= "";
while (line = Bufreader.readline ()) = null)
Result + = line;
return Result;
} catch (Exception e) {
E.printstacktrace ();
}
}
The following is read directly from assets
Code
public string Getfromassets (string fileName) {
try {
InputStreamReader Inputreader = new InputStreamReader (Getresources (). Getassets (). Open (FileName));
BufferedReader bufreader = new BufferedReader (Inputreader);
String line= "";
String result= "";
while (line = Bufreader.readline ()) = null)
Result + = line;
return Result;
} catch (Exception e) {
E.printstacktrace ();
}
}
Of course, if you want to get the memory stream, you can return to the memory stream directly!

<div "=" > Next, we create a new project file named Assetsdemo.

Then set up a layout file, such as the next, very simple, without me to introduce more, we see it.

Then, I found a paragraph of text from the Internet, stored in the assets file directory, named Health.txt This is the file we are going to read today.

this. txt file, we can directly double-click to view. as shown below.

<div "=" "> Next, is the play of today, Android reads the core code of the file. Just put the code directly. <div "=" ">package com.assets.cn;
Import Java.io.InputStream;
Import Org.apache.http.util.EncodingUtils;
Import android.app.Activity;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Assetsdemoactivity extends Activity {
public static final String ENCODING = "UTF-8";
TextView TV1;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

TV1 = (TextView) Findviewbyid (R.ID.TV1);
Tv1.settextcolor (Color.Black);
Tv1.settextsize (25.0f);
Tv1.settext (Getfromassets ("Health.txt"));
}

Get the file from the Assets folder and read the data
public string Getfromassets (string fileName) {
String result = "";
try {
InputStream in = Getresources (). Getassets (). open (FileName);
Gets the number of bytes in the file
int lenght = in.available ();
Create a byte array
byte[] buffer = new Byte[lenght];
Read the data in a file into a byte array
In.read (buffer);
result = encodingutils.getstring (buffer, ENCODING);
} catch (Exception e) {
E.printstacktrace ();
}
return result;
}
}<div "=" "><div" = "" >

Java parsing JSON file method (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.