Combine txt files under 2 folders with subfolders - java

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

Related

Depth-first directory stream in Java 8

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.

Combing two text files together and zipping them

I AM NOT LOOKING FOR A CODE but and idea on how to approach the problem.
I have multiple text files with the following format
NAME_EMAIL_CONTROL_DATE.txt
NAME_EMAIL_CONTROL2_DATE.txt
I want to zip both the files given the DATE.
I am not sure how I can approach the problem.
If date is being stored at a specific constant spot on all the files (beginning of file, end of file) you can use a FileInputStream to read those specific bits into a buffer and check if the two contain the same data, which you could then continue to use said FileInputStream to read the contents of both into buffers and use a FileOutputStream to create your new combination file.
Assuming that what you mean is that the file NAMES all have dates in them, at the end of their filename 'stems'...
Write a function to make a list of all your files -- given a directory containing the files, use listFiles() to get a list of all of them and compare the date portion to whatever you want, ending up with a list.
Then for each such file, use the zip file creation facility in java to add each file.
If all of these are in one directory, the command line zip command to do this will be fairly trivial, the hardest part will be the regular expression for the filename.

Fast search of proper XML file in folder

If I have 100 short XML files in folder and I'd like to know which of them contains text aaabbbccc (for later accurate parsing). Is this a good idea to read them as Strings one after other and try to use contains function to determine what files not contains this text?
As I know the contains function is very fast.

Java JTree directory structure from file paths

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.

File Operations in Java

I'm working on a small application in Java that takes a directory structure and renames the files according to a certain format, after parsing the original name.
What is the best Java class / methodology to use in order to facilitate these file operations?
Edit: the question is only regarding the file operations part, I got the "getting the formatted name" down :)
Edit 2: Also, how do I list files recursively?
Use java.io.File
Listing all files in a directory
http://www.javaprogrammingforums.com/java-code-snippets-tutorials/3-how-list-all-files-directory.html
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
// Do something with "listOfFiles[i]"
}
UPDATE
To list the files recursively, your best approach is fairly easy:
Create a queue of directories. Initially add the first directory to the queue
Pop the first directory element off the queue.
List all files in that directory, same as above
Iterate over all the files in that directory
If a file is a directory (use isDirectory() method), add it to the back of the queue.
Else, process this next file as needed (e.g. print)
Stop when the queue is empty.
An example (I think a bit different from my approach above) is http://www.javapractices.com/topic/TopicAction.do?Id=68
Renaming a file
http://www.roseindia.net/java/example/java/io/RenameFileOrDir.shtml
boolean Rename = oldfile.renameTo(newfile);
Finding a new name to rename to
I'm not sure what you want the formatting rules to be - when I implemented the same utility in Perl for my own use I used Regular Expressions. For Java, that'd be java.util.regex
This Sun Totorial could be a good start. If I where you I would basically retrieve all the files in the directory and then loop through them, as shown here. You might have to use regular expressions as well, a basic tutorial can be found here
You can always use the standard java.io.File class, but it's primitive and not very useful on its own.
For complex file-I/O operations, I recommend using Apache Commons IO, which provides a rich class library for (among other things) file operations. See classes like FileUtils and FilesystemUtils
There's the class File, that does all you need:
listFiles()
renameTo()

Categories

Resources