So, basically, I'm trying to organize paths into an array (list or map) to replicate that directory structure of said paths.
So, for example:
res/icons/java.png
src/Main.java
src/somepackage/Stuff.java
lib/somelib.jar
And the idea is to store them into a data structure that's possible to iterate into some form of GUI display. Like a TreeView (JavaFX).
I was thinking of doing it by putting all of the paths into a regular String[] arrray and then looping through them and splitting each one by "/" and then checking if each part that is a folder already exists in a data structure. If it does, then just place the file at the end into it and then if not, then create an array where key = folder name and values = files.
tl;dr - is there a java array like structure that can be used to replicate a directory like structure?
Consider a recursive approach. In pseudocode:
Class Item
Boolean isFile
String title
List<Item> items
AddItem(item)
RemoveItem(item)
Iterate()
In this class, you can extend ArrayList<Item>. In that case you don't need to hold the list of items. You would also have lots of method to use directly.
If you need help to put in Java please say so.
Related
I want to save severals files using Stream to serialize objects, with one serializable object per file.
The reason why I want have one file per object is that I have a list of these objects, I am on Android and at the start of the applicaiton I want load all saved objects, this is the easy part.
But during the execution I want add new elements in this list, and delete elements. And I want update the folder with my files in the same time.
So I supposed I have to create one file per object. But may be there is another solution ?
The main problem is : how name each file ? To avoid overwrite a file, ...
I first thinked that I will have to name my files depending the name of a String given by the user, but I will have to check if the name file is valid,...
So may be the solution is to just name my files with an integer, and at the first load, I just continue the counter from the higher file ?
I suppose my problem is frequent, so what are your solutions to write during the execution a dynamic list of objects ?
Thanks you
I plan on reading several files when my app/game is created and using the information from them for the entirety of the app. I also have to write to the file at one point.
I have two files. One is a 2-column text file that I'll turn into a dictionary for fast searching. The other is a text file that has 11 columns. I'll make a dictionary out of two of the columns, and the other data I need kept as is so I can write to the columns to count the amount of times something happens in different circumstances for datamining.
Currently, I've turned the second file into a list of a list of strings, or List>. I can't figure out how to pass that around in intents. ".putStringArrayListExtra" only works for a list of strings.
Am I going about this the wrong way entirely? This is my first real Android app.
In order to store a data structure into an Intent, it has to be either serializable or parcelable. If your data structure is neither of them, you might create a class that would implement Serializable and manage it. A good example might be found here.
Once done, you then might use Intent.putSerializable(...) to store your data structure. See this:
Using putSerializable in Android
Additionally to this, if you could convert your structure into a JSON structure, you'd already have it done since it would be treated as a String. If not, the above solution should be easy to do.
I call a webservice and get the following data from it:
Name of the folder
Id of the folder
Id of the parent-folder (null if it is root)
I create ArrayLists (List<String>) for the names, the ids and the parent-ids. So the folder with the name on position "0" has the id and the parent-id on position "0" in these lists.
Now I need to recreate the same structure on my local file system. The user enters a root-directory ("C:\test" for example) that I need to use.
I guess that a recursive method would be the best thing to do, but I have no idea how to implement it.
Any ideas / hints?
I don't see how recursion helps you. I assume you get multiple sets of the data you present, implied by your explanation though you don't say so. You also don't say what order you get them in. I'd create a hashmap, using full path to each parent as a key, and an object representing the directory as a value. The directory object would contain pointers to all its child directories. I'd create that entire hashmap, then walk it top-down. If you don't get the data in the correct order to build it top-down, then you'll have to put them all in a list and search the list to create top-down order, or trust that you can build the list without the IDs and fill them in later
I've been trying to get my head around this so maybe some of you can help me. I have a list of files with their full paths (these are just strings the files are on another machine), e.g:
C:\a\b\c\file1.txt
C:\a\b\c\file2.txt
C:\a\d\file3.txt
C:\e\file4.txt
I want to create a Jtree to show the directory structure like this:
C:
a
b
c
file1.txt
file2.txt
d
file3.tct
e
file4.txt
I've been spliting the string on the seperator so I end up with a list of arrays like:
"C:","a","b","c","file1.txt"
"C:","a","b","c","file2.txt"
"C:","a","d","file3.txt"
"C:","e","file4.txt"
Now I want to add them an index at a time but if the value already exists at that level then to skip to the next index. i.e it would add the first array then on the second array it would go on level 0 of the tree there already exists a "C:" so move onto level 1 of the tree and index 1 of the array. The issues that I have is that Im not sure how to navigate the tree in such a way.
Any suggestions and or alternative implementations?
Let File do the work of parsing and maintaining paths. As you want to display the files in a JTree, you might as well create a corresponding TreeModel such as FileTreeModel, cited here. Because it implements TreeModel, it can "be set as a JTree's model and then you'd have a plain old standard JTree." You can use any File in any mounted file system as the root, for example:
TreeModel model = new FileTreeModel(new File(System.getProperty("user.dir")));
JTree tree = new JTree(model);
I'm not sure if FileTreeModel is the best way - it scans entire directories. From what you wrote, I guess you only want to display paths from your list. You can achieve it by using TreePathsTreeModel described here: How I Show Windows Registry in jTree?
You just have to to convert filepaths from strings into TreePath objects.
First, sort the Strings (before splitting them).
How to process the first line is obvious and I won't comment on it. In the second line, search the already built tree and check if the nodes already exist. After you find one that does not exist, follow the procedure done in the first line.
Hi I'm not particular good at Java so please bear with me. I'm trying to write a very simple android app now and I need help with some coding.
Thing is, I have a server that automatically generates .png files and saves them to a public directory in a numerical order. The update occurs daily and is non-exhaustive.
Is there anyway in which I can assign the dynamic values to an array within my app?
private String[] myRemoteImages = {
"http://hypotheticalurl1.png",
"http://hypotheticalurl2.png",
"http://hypotheticalurl3.png",
"http://hypotheticalurl4.png",
"http://hypotheticalurl5.png",
"http://hypotheticalurl6.png",
"http://hypotheticalurl7.png",
"http://hypotheticalurl8.png",
"http://hypotheticalurl9.png",
"http://hypotheticalurl10.png",
"http://hypotheticalurl11.png",
"http://hypotheticalurl12.png",
//...blah blah blah
// these are all dynamically created so I won't know what is the last number on the list
};
This array will eventually be used to get the images from my server using the app. It works so far but that's only with hardcoded URLs. I would like the URLs to be dynamic, as the number of images will change from day to day.
I'm doubting that regex will work well in Java but then again I'm no expert. Was thinking of perhaps writing a script on the server end that generates a list of existing values and somehow parsing that with the android app.
Can anyone point me in the right direction? Thanks in advance.
Clarification:
The array doesn't have to be dynamically sized while the app is running.
I need a way to read the list of existing images in a remote directory and pass that information to populate the array automatically at runtime.
Resolved
Guys, thanks for the help. Sorry if I wasn't clear enough.
I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.
After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.
thanks for the help. Sorry if I wasn't clear enough.
I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.
After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.
With an array you can :
change the elements of the array
but you can't :
add or remove elements. The number of elements if fixed in an array. Some workaround can be found like putting null values and discarding theem when using the values in the array. But that's more troublesome than really useful.
On the other hand, if you want a full dynamic "array" : use a list (java.util.List). An ArrayList would be interesting here, or even a Vector as you will probably need some multihtreading around this array. With a list you can add and remove elements, size can vary and elements can be replaced.
I'd use an ArrayList in this case. You don't have to know the number of elements you want to add then and it's very simple to append elements at the end.
private List<String> list = new ArrayList<String>();
Then simply add elements by
list.add("http://hypotheticalurl1.png");
Regards,
Patrick
instead of using Array of String
use ArrayList<String> It will gives you more flexibility on adding and removing item on runtime refer this link...http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html
hear you can find example on arraylist...http://www.java2s.com/Tutorial/Java/0140_Collections/0160_ArrayList.htm
hope that helps
According to your scenario you need to have the followings:
1- a Web Service which has a method to get you the list of the available image names.
2- You need a web service client for your android application, I suggest you to use KSOAP 2 because it is widely known and easy to implement.
(If you can't figure out how to use the ksoap in your program, I can provide you some example codes)
3- You need to use ArrayList(java.util) to hold your dynamically sized array.
Hey ytou can do it via
ArrayList stringList = new ArrayList();
stringList.add("Item");