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!
Related
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.
I'm trying to get the current user directory, where all of the users on the computer are saved, so I can save some details to this folder so it will apply to the whole computer instead of just the user area of the current user.
For example
System.getProperty("user.home")
returns C:\Users\CURRENT_USER
How can I get the "C:\Users"
Probably using substring or something using the index of the last slash? But help would be appreciated, can't seem to get it
You could find the index of the system property "user.name" in the home directory that you already have. You would use lastIndexOf to get the position, and substring to effectively remove the user name. Then you can remove the resultant trailing backslash.
I need to create a program that sets an int to 01 at the beginning of each day. Every time the file is run, the int increments up until the next day. This int will be inserted into a file name, e.g. FileName(insertdatehere)01.txt, FileName(insertdatehere02.txt, FileName(insertdatehere)03.txt, etc...
I was wondering if this is possible:
-Checking if the file already exists, and if it does, then the int value will increment. This will work since the file name has the date on it, so that each day, a new file name will be created anyways.
Am I going in the right direction, or should I completely rethink this question?
Sorry if this isn't clear, if you need me to clarify, I will.
Your ideas seem correct and doing it in this way would probably work well.
Something to watch out for would be if two of the same process exist and both try to create a file assuming it doesn't exist.
As long as you consider this case, and your process runs reliably throughout the day (and you don't fall into timezone traps), you should be good to go.
Did you try using java.util.Date class for setting time stamp , date etc.
You can set date whenever the file is opened in some other file or you can set the same value at some specific place in the same file.
Then whenever you open the file again you can compare and check the earlier date which is already set.
This would surely help you.
Firstly try it yourself and then if you remain unable to do the same post issues whichever you face.
Your Idea is good .
But for the case when there does not exists any file and two processes are trying to create it simultaniously with same name then the problem with occur .
Above problem you can solve by using Synchronization in Java, so that code block(which contains logic for check file if it exists and creates new file) cannot be accessed simultaneously .
My Code:
I'm currently developing a game and throughout several different parts of the code I'm using some resources (Images/Sounds/Animations etc.). To avoid loading the same resource twice I wrote a ResourceManager, that returns the wanted resource if I pass a string to it.
Here's an example:
Image myImage = imageManager.getImage("princess");
This way I can reference a resource without knowing the name of the file or position of it, when I want to use it.
The trick here is that I have to load the images before I can get them like so:
imageManager.loadImage("res/princessImage.png", "princess");
This creates the ImageObject from the given file, and stores it into a HashMap with the given key.
My Problem:
I really don't want to hardcode the paths to these resources, because I'd have to change the sourcecode every time I decide to move or rename any of the resource-files.
A possible solution (?):
I thought about creating another HashMap that reads some kind of configFile and maps the in-code-resource-names to the resource-paths in a HashMap. The file would look somewhat like this:
princess: res/princess.png
hero: res/hero.png
sword: res/items/sword.png
This way I could use resource-names like "princess", "hero" or "sword" safely and don't worry about their position on the hard drive while I'm coding. Whenever I move or rename a resource-file I just update the path/name in this configFile and everything would be fine.
On the other hand I think it's pretty ugly to have one giant file that maps every in-code-resource-name to a path. This could result in one giant String to String HashMap which I'd have to store in the ResourceManager aswell. Things could get pretty confusing/unclear.
Does anyone have a better solution for me?
I'd really appreciate your help,
Thanks :)
Using a config or resource file as you described is a fine approach. Instead of populating a HashMap, though, consider using ResourceBundle or PropertyResourceBundle. It is designed to hold/access such things. http://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html
The Problem:
I have numerous files that contain Apache web server log entries. Those entries are not in date time order and are scattered across the files. I am trying to use Pig to read a day's worth of files, group and order the log entries by date time, then write them to files named for the day and hour of the entries it contains.
Setup:
Once I have imported my files, I am using Regex to get the date field, then I am truncating it to hour. This produces a set that has the record in one field, and the date truncated to hour in another. From here I am grouping on the date-hour field.
First Attempt:
My first thought was to use the STORE command while iterating through my groups using a FOREACH and quickly found out that is not cool with Pig.
Second Attempt:
My second try was to use the MultiStorage() method in the piggybank which worked great until I looked at the file. The problem is that MulitStorage wants to write all fields to the file, including the field I used to group on. What I really want is just the original record written to the file.
The Question:
So...am I using Pig for something it is not intended for, or is there a better way for me to approach this problem using Pig? Now that I have this question out there, I will work on a simple code example to further explain my problem. Once I have it, I will post it here. Thanks in advance.
Out of the box, Pig doesn't have a lot of functionality. It does the basic stuff, but more times than not I find myself having to write custom UDFs or load/store funcs to get form 95% of the way there to 100% of the way there. I usually find it worth it since just writing a small store function is a lot less Java than a whole MapReduce program.
Your second attempt is really close to what I would do. You should either copy/paste the source code for MultiStorage or use inheritance as a starting point. Then, modify the putNext method to strip out the group value, but still write to that file. Unfortunately, Tuple doesn't have a remove or delete method, so you'll have to rewrite the entire tuple. Or, if all you have is the original string, just pull that out and output that wrapped in a Tuple.
Some general documentation on writing Load/Store functions in case you need a bit more help: http://pig.apache.org/docs/r0.10.0/udf.html#load-store-functions