How to get the INI file in the Asset directory in Android

Source: Internet
Author: User
Tags getmessage

1. get the input stream of the resource

Suppose the resource is in the assets directory:

context.getassets (). Open ("Sample.txt");


public void Deepfile (Context ctxdealfile, String path) {
try {
String str[] = ctxdealfile.getassets (). List (path);
if (str.length > 0) {//If it is a directory
File File = new file ("/data/" + path);
File.mkdirs ();
for (String string:str) {
Path = path + "/" + string;
System.out.println ("zhoulc:\t" + path);
Textview.settext (Textview.gettext () + "\ T" +path+ "\ t");
Deepfile (ctxdealfile, path);
Path = path.substring (0, Path.lastindexof ('/'));
}
} else {//if the file
InputStream is = Ctxdealfile.getassets (). open (path);
FileOutputStream fos = new FileOutputStream (New File ("/data/")
+ path));
byte[] buffer = new byte[1024];
int count = 0;
while (true) {
count++;
int len = is.read (buffer);
if (len = =-1) {
Break
}
Fos.write (buffer, 0, Len);
}
Is.close ();
Fos.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

2. Specific examples

private void Getbooturl (context context) throws IOException {
LOG.D (TAG, "Getbooturl () Start");
InputStream is = null;
BufferedReader in = null;
try {
is = Context.getassets (). Open ("Ini/boot.ini");
in = new BufferedReader (new InputStreamReader (IS));
} catch (IOException E1) {
E1.printstacktrace ();
LOG.D (TAG, E1.getmessage ());
}


StringBuffer sb = new StringBuffer ();
String temp = null;//Stores the contents of each row read


int line = 1;
string[] Sbstrs = new string[] {};//splits a String with "=" as a delimiter


try {
while (temp = in.readline ()) = null) {
if (Temp.contains ("boot")) {
Sb.append (Temp.substring ((Temp.indexof ("T") + 2) + "");//intercept the substring after the "=" delimiter, i.e. save only the boot and Bootip
line++;
Read Next line
while (temp = in.readline ()) = null) {
if (Temp.contains ("Bootip")) {
Sb.append (Temp.substring ((Temp.indexof ("P") + 2)
+ ""), or//from the "=" after the delimiter to intercept the string save, that is, only save the boot and Bootip
Break
} else {
line++;
}
}
Break
}
}
In.close ();
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
Booturl = "";
LOG.D (TAG, E.getmessage ());
} finally {
if (in = null) {
In.close ();
}
}
Sbstrs = Sb.tostring (). Split ("");
Booturl = Sbstrs[0];
Bootipurl = sbstrs[1];
LOG.D (TAG, "Getbooturl () end");
}

How to get the INI file in the Asset directory in Android

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.