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.
Related
My task is to sort file which is too large to fit in memory. File contains text lines.
What I did:
read from original file by parts (of allowed size).
sorted each part
saved each sorted part to tempfiles
As I understand next thing i should do is:
read first lines of each file
sort them between each other (use local variable to temporarily store it, but I am not sure if it will be below restricted size)
write first line (as result of sorting) to final file
now I need to remove line I just wrote from temporary file
now i need to repeat steps 1-4 until all lines are sorted and "transferred" from temp files to final file
I am most unsure about step 4 - is there a class than can look for a value and then erase line with this value (at that point I won't even know from which file that line came)? I think that this is not a proper way to reach my goal at all. But I need to remove lines which are already sorted. And I can't operate with files' data in memory.
do you need to do this in Java (assuming by the tag)? As memory wise it isn't going to be efficient way. The simplest option in my opinion would be using sort and just sort the file directly on the OS level.
This article will give you a guide on how to use sort: https://www.geeksforgeeks.org/sort-command-linuxunix-examples/
Sort is available on Windows as well as unix/linux and can handle huge files.
I'd like to traverse a directory structure in Java 8 using the Stream API in depth-first order. The reason for this is that I want to sort the contents in the files according to a timestamp present in each file on a per directory basis. Essentially I'm looking for something similar to Files#walk but for directories. How can I achieve this?
Using StreamEx is a breeze:
File root = new File("someFilePath");
StreamEx.ofTree(root, x -> StreamEx.of(x.listFiles(File::isDirectory)))
.map(File::getAbsolutePath) // or whathever you need to do with the folder
.forEach(System.out::println); // the same as previous line
It’s hard to understand your problem description, most notably, what you actually want to do when you say you want to “sort the contents in the files according to a timestamp present in each file on a per directory basis”.
And when you say you want “something similar to Files#walk but for directories”, it implies that you have a problem with using Files.walk with directories, but it’s not clear, what problem you have.
I.e., you can simply list all subdirectories of the current directory like
Files.walk(Paths.get("")).filter(Files::isDirectory).forEach(System.out::println);
So if that’s not matching your goal, you have to spent more time elaborating your goal.
I have two directories with identical folder tree structure and file names. These are the data of current week and last week. I want to combine (append the new data to the old ones) each pair of them (name to name) into and save it under another directory with the original file tree structure.
How do I do it in Java? Please help.
What is the best way to read and write a file?
I think I would use a recursive approach, iterating over the current week's structure. For each file found, locate the corresponding files in last week's structure and append it to the current week's file. If the structures are not guaranteed to be identical, you might also need to recurse over last week's files to pick up anything that does not exist in this week's directory tree.
As for the actual Java part of the question, i.e. the implementation of those operations, the following may be helpful:
java.io.File
java.io.FileReader
java.io.FileWriter
Apache Commons IO
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.
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