How to order the files in src folder - java

Hi I'm trying to put Lesson10 file in the bottom and I can't.

Folders are sorted in lexical order which means Lesson10 comes before Lesson2. You can solve this by adding a zero before the lower numbered lessons: Lesson02, Lesson03, etc. As a note, if you get to lesson 100 you would need to add an additional zero before every prior lesson.

Related

Sorting text lines from hard drive files by partly loading to memory | Java

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.

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.

MarkLogic: Move document from one directory to another on some condition

I'm new to MarkLogic and trying to implement following scenario with its Java API:
For each user I'll have two directories, something like:
1.1. user1/xmls/recent/
1.2. user1/xmls/archived/
When user is doing something with his xml - it's put to the "recent" directory;
When user is doing something with his next xml and "recent" directory is full (e.g. has some amount of documents, let's say 20) - the oldest document is moved to the "archived" directory;
User can request all documents from the "recent" directory and should get no more than 20 records;
User can remove something from the "recent" directory manually; In this case, if it had 20 documents, after deleting one it must have 19;
User can do something with his xmls simultaneously and "recent" directory should never become bigger than 20 entries.
Questions are:
In order to properly handle simultaneous adding of xmls to the "recent" directory, should I block whole "recent" directory when adding new entry (to actually add it, check if there are more than 20 records after adding, select the oldest 21st one and move it to the "archived" directory and do all these steps atomically)? How can I do it?
Any suggestions on how to implement this via Java API?
Is it possible to change document's URI (e.g. replace "recent" with "archived" in my case)?
Should I consider using MarkLogic's collections here?
I'm open to any suggestions and comments (as I said I'm new to MarkLogic and maybe my thoughts on how to handle described scenario are completely wrong).
You can achieve atomicity of a sequence of transactions using Multi-Statement Transactions (MST)
It is possible to MST from the Java API: http://docs.marklogic.com/guide/java/transactions#id_79848
It's not possible to change a URI. However, it is possible to use an MST to delete the old document and reinsert a new one using the new URI in one an atomic step. This would have the same effect.
Possibly, and judging from your use case, unless you must have the recent/archived information as part of the URI, it may be simpler to store this information in collections. However, you should read the documentation and evaluate for yourself: http://docs.marklogic.com/guide/search-dev/collections#chapter
Personally I would skip all the hassle with separate directories as well as collections. You would endlessly have to move files around, or changes their properties. It would be much easier to not calculate anything up front, and simply use lastModified property, or something alike, to determine most recent items at run-time.
HTH!

Android how to name image to save in /camera?

I built a camera app. My problem is that I want to continue the logical naming the default camera app uses.
EG: The camera app produces files such as:
IMG_20130104_033852
IMG_20130104_033853
If these are the only contents of my /camera folder, I should theoretically name my photo IMG_20130104_033854. Should I look for the image with the "biggest" name and add a number or is there a better solution for this problem?
I would suggest building the name up until the point you get to the last sequence of numbers, and then you could look for the biggest number and add one to it if you feel like it.
So for instance, based off of today's date, you could build the IMG_20130121_ part. You could do File [] fList = f.listFiles();, where f is pointing to the /camera/ directory. From there, you could check the files by doing fileName.startsWith("IMG_20130121_") in an IF statement.
From there, do a substring to grab the string-numbers after the last underscore (_), convert to Integer, and loop through until you finish comparing all the Integers.
I did this for folder names. A standalone cd burning device creates folders that start with ezb_, followed by a number. I can paste my looping code tomorrow if this info wasn't enough.
As a sidenote, if you were to start your own naming algorithm after IMG_20130121_, like adding an s or something, you'd still have to either keep track or look for the biggest number. You might as well just implement something like what I tried to explain. Let me know if I need to clarify!

how to delete a particular line from random access file

I am using a random access file in which i want to delete a line which satisfies some condition like if i have records
MCA 30
MBA 20
BCA 10
now my requirement is if i enter MBA then second line is to be deleted.
Generally, removing an item from the middle of the file means rewriting all entries after the entry so as to use the space the item occupied.
Some things instead mark items that are deleted with some invalid value so as to spot that the slot is unused. Typically they don't even reuse deleted slots, as this is rather more management than you might imagine, and is basically implementing a heap-like architecture in a file. They need a separate 'compacting' step to remove this dead space later. Microsoft Jet (as in Access) worked like this.
There's a very cool optimisation that's applicable in some cases:
If the rows are unordered, and equal length, you can overwrite the entry you want to 'delete' with the last entry, and truncate the file.
If the rows are unordered but not fixed length, you can use a more complicated variant of this approach where you move some entry from near the end that is the same length as the item being removed, so as only to need to shuffle up as few entries as possible.
If it is a simple text file than you'll have to copy everything except the MBA line(s) to a new file. Random Access files don't really support delete or insert.
Optimization: move everything after the MBA line up (in the same file)
Alternative: use something more structured like a database.
It sounds to me like you're looking for a 'grep' like implementation. There's a Java implementation of GNU grep, you can find the documentation here,
and download from a link in this page.

Categories

Resources