I have large amount of files in a ZIP file (lets say 1000 images, some db files, binary files, ...). Inside it, I have some xml file I need to find and parse it. Information from it is shown to the screen. Problem is, when I am iterating through zip entry using:
InputStream inputStream = new FileInputStream(zipPath);
in = new ZipInputStream(inputStream);
for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
...some code here...
}
So when I am using Log.d, I see, it is iterating file by file, in case of large amount of files, it could take several minutes. Is there any better way, how to locate one specific file among others in ZIP file? "Brute force" approach I am using now is time consuming.
Thanks for any ideas
Waypoint
You can probably do this:
BufferedReader in = new BufferedReader(new InputStreamReader(zipfile.getInputStream(entry)));
When extracting specific files, you should be using a ZipFile. In particular, ZipFile.getEntry().
This should be no problem since you are using a File. However, if you only have an InputStream (or you prefer to use ZipInputStream for some reason) then, if you control how the zip file is built, you should put the XML file as the first entry.
Related
I want to obtain only the available languages for my android application, i.e. those for which I have a folder named values- (e.g. values-en, values-fr) in res folder.
I do not want to store the language codes and I think to list all the sub folders of "res" of the form values-* and take the language code from their name. (eventually check if the code is in the array returned by Locale.getAvailableLocales() to be sure that it is correct). This idea is stated here How to get the available languages (Not all of them, just the languages available on my app).
I have tried using
getResources().getAssets().list("res"); getResources().getAssets().list("/res");
getResources().getAssets().list("/res/");
but none of them worked.
Do you have any idea how I can list the sub folders of "res" folder?
Thank you in advance.
I don't know of a direct way to do this, but I have done a similar thing with the assets folder which should work fine. I use IntelliJ IDEA and ANT on Windows for my building, but you should be able to adapt this for Eclipse or other IDE and *nix/OSX.
Before compiling, I use an ANT build facet to run a dir command to list all files in the assets folder and pipe the output to a text file:
dir /assets /s /b > /res/raw/filelist.txt
I then read filelist.txt into a hashmap to give me very easy and fast way to find any file (I have hundreds of files in dozens of assets subfolders and AssetManager is too brain dead to deal with that).
public class AssetsFileManager {
private static Map<String, String> files = new HashMap<String, String>();
static{
BufferedReader in = new BufferedReader(new InputStreamReader(this.getResources.openRawResource(R.raw.filelist)))
String line = "";
while ((line = in.readLine()) != null) {
map.put(line.substring(line.lastIndexOf("\\")+1), line);
}
in.close();
}
}
Note the escaped backslash, \\
This gives me a map keyed on the filename and the full path as the value.
You could easily adapt this approach to get your folder list. E.g. you might use dir /ad /b /s
I'm not sure that this will work, but you can get all possible languages with Locale.getAvaliableLocales() and try to get some resource (e.g. app_name) for each locale in list. Here is one possible way.
I am really lost here, i am trying to find my way around xml parsing, reading and writing.
I have this app where at one point i can input data such as a Date and a time for instance - click save, and once it saved it will write into an existing XML file, for later reading, and add it at the end in a format like this:
<Units>
<item>
<date>27-5-12</date>
<time>15:30</time>
</item>
<item>
... and so on ...
</Units>
i managed to read an xml file, but i am really having trouble in opening a premade - existing file for reading or writing.
currently i tried this code:
InputStream raw = this.getAssets().open("mydata.xml");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
which returns file not found exception.
could anyone direct me on what i should look for?
Thanks.
As written, your source XML file is located in your APK's assets directory - everything in your APK is read-only, so you won't be able to write to that file. (Also, you should probably put that XML data into the res/xml directory instead of the assets directory, unless you have a compelling reason to do otherwise.)
If the XML file isn't very long/complex, you could read the assets file into a structure, then add your new data to that structure, and write a new XML file into your app's data directory with the updated data. This approach has the advantage that you can have multiple source files feeding into one main file per app.
A more flexible and open-ended option would be to set up a database table. When the app is first installed, you load/update the table with data from the assets file. As your app keeps adding timestamped data, you just add new rows to the table. This approach also has the advantage that you can easily update the source data or the database structure with each app update - it's harder to compare old vs new data if it's stored internally in XML format.
I didn't see the assets folder in my project, i placed my xml file there and it works now :)
Pretty short question:
I have put a .csv file in my assets folder. My problem is that I'm unsure how to read it and store its contents in an array-like form that I can pass to a function that then loops through the contents.
So, basically I want something like (pseudo code):
VariableArray var[] = new Variable (get the file contents);
performFunction(var[]);
How I can accomplish this?
You can use this to get an InputStream to your file in assets.
InputStream is = getAssets().open(fileName);
Then you just need to make a reader and loop through the file until the end of it.
I suggest you to use the JavaCSV library to deal with CSV formats. There are a lot of things it can accomplish in very few lines of code.
I am new to Android development. I have an XML file with data that the app will read. Where should I keep this XML file? Should it be stored within the "value" folder?
I'd say that depends. What do you save in your XML-File? There also is a res/xml-folder, where XML-Files can be kept. But Android does nearly anything with XML-Files, so you might want to read my little Tutorial about where to put which recourses.
Also, there is a difference between the assets and the res-directory's:
res
No subdirectorys are allowed under
the specific resource-folders.
The R-class indexes all resources
and provides simple access.
There are some simple methods which
help reading files stored in the
res-directory
assets
Subdirectorys are allowed (as much as
you like).
No indexing by the R-class
Reading resources stored in assets
is done using the AssetManager.
You can put it in the res/raw folder. Then you will access it using:
getResources().openRawResource(resourceName)
I had a similar requirement and after lot of research , I found 2 solution to place a custom XML :
You can place custom XML in
res/raw/
res/xml/
To access these location you will use following code :
a. if XML is placed in res/raw then :
getResources().openRawResource(R.raw.custom-xml) :
This gives you easy methods for reading xml :
with below code I am reading XML in memory placed in raw folder :
BufferedReader br = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.custom-xml)));
StringBuilder str = new StringBuilder();
String line;
while ( (line = br.readLine()) != null){
str.append(line);
}
2nd Option :
getResources().getxml(R.xml.custom-xml);
with this you could read the xml using eventbased parser.
I am looking for a way to store configuration setting on an external file on the sd card. The app must look into this external file and then retrieve some kind of settings. At the moment i am trying to get it to look into a file and get a name. I know you can store things in shared preferences but they are internally accessed.
Anybody know an external way? Thanks
The idea is to have a simple text file or xml file on the sd card. So when a configuration needs changing it is done thru that file?
EDIT
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"/Config.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line="";
int c;
while ((c = br.read()) != -1) {
line+=(char)c;
if(String.valueOf(c) == ";" & line =="name;"){
String name ="";
}
}
}
I have attempted to read in a config.txt file and to separate each value with a ;
But i cant seem to understand exactly how i am going to attach what comes after name; to the variable String name.
the config.txt file has the following:
name;fred
somethingelse;test
The program should know when it has got to name and then set the name variable to fred??
Hi
I had same kind of preferences reading in Blackberry with (Comma separated or) semicolon separated values. When same development came to android the developers (I was not on android at that time.) This is what they have done.
We had created a text file like this
"name";"value";"data_type"
For example
"application_runs";"1";"int"
Or
"trial_period_key";"01ab23cd";"string"
The data type is hardcoded so we can detect the datatypes. For variable names we also had hardcoded list of preferences. And values can be parsed accordingly. They have written public readable shared preference based on that txt files, to use default values, they have copied a "default.txt" in assets folder along with a copy in SD card.
The drawback of this procedure is
You have to be careful about file name and the data written in that file (this is the reason why we had put a default values file in assets so the app doesn't crash)
The text file on SD Card must be readable, you have to program it along with parsing.
Hope it helps.