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