Optimal lock file method

Take a look at the enlightening presentation File Locking Tricks and Traps: This short talk presents several common pitfalls of file locking and a few useful tricks for using file locking more effectively. Edit: To address your questions more precisely: Is there a better synchronization method than the lock file? As @Hasturkun already mentioned and … Read more

Test if a file is an image file

This works pretty well for me. Hope I could help import javax.activation.MimetypesFileTypeMap; import java.io.File; class Untitled { public static void main(String[] args) { String filepath = “/the/file/path/image.jpg”; File f = new File(filepath); String mimetype= new MimetypesFileTypeMap().getContentType(f); String type = mimetype.split(“https://stackoverflow.com/”)[0]; if(type.equals(“image”)) System.out.println(“It’s an image”); else System.out.println(“It’s NOT an image”); } }

c# open file, path starting with %userprofile%

Use Environment.ExpandEnvironmentVariables on the path before using it. var pathWithEnv = @”%USERPROFILE%\AppData\Local\MyProg\settings.file”; var filePath = Environment.ExpandEnvironmentVariables(pathWithEnv); using(ostream = new FileStream(filePath, FileMode.Open)) { //… }

How to generate and prompt to save a file from content in the client browser? [duplicate]

This “FileSaver” library may help. If you want it to be reasonably cross-browser, you’ll also need this to implement the W3C Blob API in places it’s not already implemented. Both respect namespaces, and are completely framework agnostic, so don’t worry about naming issues. Once you’ve got those included, and as long as you’re only saving … Read more

Sort directory files by creation datetime in Windows filesystem

On windows the CTime is the creation time, use the following: <?php $files = array(); foreach (new DirectoryIterator(‘/path’) as $fileInfo) { $files[$fileInfo->getFileName()] = $fileInfo->getCTime(); } arsort($files); After this $files will contain an array of your filenames as the keys and the ctime as the values. I chose this backwards representation due to the possibility of … Read more

Write and read multiple objects to file

You can’t append to an existing file created with an ObjectOutputStream, at least not without effort. There is a trick somewhere about extending ObjectOutputStream and overriding the writeStreamHeader() method so as not to write the stream header the second time, but I’m not in favour of it. You should really rewrite the whole file, maybe … Read more

iPhone Documents directory and UIFileSharingEnabled, hiding certain documents

The answer given by FrenchKiss Dev is not correct. The user will still be able to see the “.data” directory in iTunes and save that locally with all the files inside it. Instead, store private documents in Library/Preferences According to Apple: In addition to the directories documented previously, the entire /Library directory has always been … Read more