| Image of Advent calendar using the actual windows of the German city of Hünfeld's town hall in Advent © Wikimedia |
Mite Mitreski
Shade number 51
Java Advent Calendar
On 14.12.2012 I took part of initiative called Java Advent Calendar with a post about
Functional Java collections. The idea for the Calendar is very simple, one technical article will be published per day between the 1st and 24th of December. This initiative is done for other languages like Perl or PHP and even some frameworks like Perl Dancer.
You can directly sign up on
the RSS feed
or follow
via e-mail.
How can you help? Sharing on the social networks is highly appreciated.
You can also visit the original hello world post or read on Wikipedia about Advent Calendars.
Temporary files and directories in Java 7 and before
Sometimes we want to create a temporary file, whether to safe some data that gets written by some other application or just to temporary store stuff. Well usually applications have their own temporary folder where they do this and it gets somehow configured. But why not use the underlying OS specific file like "/tmp/" in Linux so there must be some system property that has this info and there is. The key is "java.io.tmpdir" resulting in "/tmp" in my case or by code :
Deletion will only happen if JVM is closed under normal condition and not for example using kill -9 (SIGKILL) under Linix. One other interesting option is to do the deletion with a shutdown hook using "Runtime.getRuntime().addShutdownHook(new Thread() {...}" that registers an a new virtual-machine shutdown hook. This shutdown hook is simply an initialized thread but it is not started. The thread will only be executed when the virtual machine begins its shutdown sequence during normal shutdown.
Related Links
String tempDir = System.getProperty("java.io.tmpdir");
We can use tempDir folder as temporary place to store files, but there is a lot nicer ways to work with files like this even in JDK6 not just in JDK7 :
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class TempFile {
public static void main(String[] args) {
try {
// create a temp file
File tempFile = File.createTempFile("old-file", ".tmp");
tempFile.deleteOnExit();
System.out.println("Temp file : " + tempFile.getAbsolutePath());
// nio style
final Path path = Files.createTempFile("nio-temp", ".tmp");
System.out.println("Temp file : " + path);
//call the same delete on exit
path.toFile().deleteOnExit();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
Files.delete(path);
System.out.println("deleted file at "+path);
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
The JDK 7 way is the use the Files class and use Path 's that can easily enable us way to create and work files and their attributes and permissions.
Calling tempFile.deleteOnExit() or path.toFile().deleteOnExit() will enable the files to be deleted automatically after the virtual machine terminates. Files are deleted in the reverse order that they are registered and besides that we need to be careful on what the path is.Deletion will only happen if JVM is closed under normal condition and not for example using kill -9 (SIGKILL) under Linix. One other interesting option is to do the deletion with a shutdown hook using "Runtime.getRuntime().addShutdownHook(new Thread() {...}" that registers an a new virtual-machine shutdown hook. This shutdown hook is simply an initialized thread but it is not started. The thread will only be executed when the virtual machine begins its shutdown sequence during normal shutdown.
Related Links
Input this - input that, HTML5 new input types
We are used to input types like "submit", "radio" or "hidden" but these days there are more components that are so common that are part of almost every web framework out there no matter if it is in Java, Ruby, Python or any other language. I'll give a short overview of what is being added in HTML5. The current state of them is that well... they sort of work, basically not all of the features are supported in all the modern browsers or IE.
Opera seams to support most of them and they have some good initial look there.
Why would you need input types for this, well first thing is validation that otherwise you would need to do with JavaScript on the client side.
You could also style with CSS using input type specific selectors like input[type=radio]. Best thing about the input types by far is that they are semantically correct so mobile phones open input type specific view on the keyboard.
Isn't this what you always wanted? ... at least when it comes down to html input.
TL;DR version
Related Links
Demos - http://mitemitreski.com/demo/html5/input.html
W3 input element spec - http://www.w3.org/TR/2011/WD-html5-20110525/the-input-element.html
Support test - http://www.quirksmode.org/html5/tests/inputs_dates.html
Mozzila developers article - https://developer.mozilla.org/en-US/docs/HTML/Element/Input
Web Platform page - http://docs.webplatform.org/wiki/html/elements/input/type/search
Opera seams to support most of them and they have some good initial look there.
Date selection related.
Many times you need to create or integrate some control that will be a date-time picker, I've done this way to often and I'm sure that you also have. But why should we do something like this, this should be part of html and we should just style it with CSS. HTML5 makes this very simple with the date input types.
<input type="datetime" />
<input type="datetime-local" />
<input type="date" />
<input type="week" />
<input type="time" />
Live example:Emails, Phone and URL
![]() |
| Email validation example on Opera 12 |
![]() |
| Input type email on iOS |
You could also style with CSS using input type specific selectors like input[type=radio]. Best thing about the input types by far is that they are semantically correct so mobile phones open input type specific view on the keyboard.
<form>
<input name="email" type="email" pattern="[^ @]*@[^ @]*" value="" />
<input id="url" type="url" />
<input id="phone" type="tel" />
<input type="submit" />
</form>
Live example:![]() |
| Input type phone on iOS |
Color
Input type that results in color picker.
<input type="color" />
Isn't this what you always wanted? ... at least when it comes down to html input.
TL;DR version
NEW HTML5 input types, awesome!!! see demo
Related Links
Demos - http://mitemitreski.com/demo/html5/input.html
W3 input element spec - http://www.w3.org/TR/2011/WD-html5-20110525/the-input-element.html
Support test - http://www.quirksmode.org/html5/tests/inputs_dates.html
Mozzila developers article - https://developer.mozilla.org/en-US/docs/HTML/Element/Input
Web Platform page - http://docs.webplatform.org/wiki/html/elements/input/type/search
Subscribe to:
Posts (Atom)


