I have a Java process that opens a file using a FileReader. How can I prevent another (Java) process from opening this file, or at least notify that second process that the file is already opened? Does this automatically make the second process get an exception if the file is open (which solves my problem) or…
Tag: java
What’s the nearest substitute for a function pointer in Java?
I have a method that’s about ten lines of code. I want to create more methods that do exactly the same thing, except for a small calculation that’s going to change one line of code. This is a perfect application for passing in a function pointer to replace that one line, but Java doesn’t hav…
Java: Why aren’t NullPointerExceptions called NullReferenceExceptions?
Was this an oversight? Or is it to do with the JVM? Answer Java does indeed have pointers–pointers on which you cannot perform pointer arithmetic. From the venerable JLS: There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspo…
Generate a current datestamp in Java
What is the best way to generate a current datestamp in Java? YYYY-MM-DD:hh-mm-ss Answer Using the standard JDK, you will want to use java.text.SimpleDateFormat However, if you have the option to use the Apache Commons Lang package, you can use org.apache.commons.lang.time.FastDateFormat FastDateFormat has th…
Serialize Java objects into Java code
Does somebody know a Java library which serializes a Java object hierarchy into Java code which generates this object hierarchy? Like Object/XML serialization, only that the output format is not binary/XML but Java code. Answer I am not aware of any libraries that will do this out of the box but you should be…
How does the Java ‘for each’ loop work?
Consider: What would the equivalent for loop look like without using the for each syntax? Answer Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for ( : ) idiom, since the actual iterator is merely inferred. As was noted by Denis Bueno, this…
Why doesn’t Java offer operator overloading?
Coming from C++ to Java, the obvious unanswered question is why didn’t Java include operator overloading? Isn’t Complex a, b, c; a = b + c; much simpler than Complex a, b, c; a = b.add(c);? Is there a known reason for this, valid arguments for not allowing operator overloading? Is the reason arbit…
Ant is not able to delete some files on windows
I have an ant build that makes directories, calls javac and all the regular stuff. The issue I am having is that when I try to do a clean (delete all the stuff that was generated) the delete task reports that is was unable to delete some files. When I try to delete them manually it works just fine. The
Quick and easy way to test OSGi bundles
Currently, I am working on a new version control system as part of a final year project at University. The idea is to make it highly adaptable and pluggable. We’re using the OSGi framework (Equinox implementation) to manage our plug ins. My problem is that I can’t find a simple & easy to use m…
Can I add maven repositories in the command line?
I’m aware I can add maven repositories for fetching dependencies in ~/.m2/settings.xml. But is it possible to add a repository using command line, something like: The reason I want to do this is because I’m using a continuous integration tool where I have full control over the command line options…