Skip to content
Advertisement

Regular expression to match URLs in Java

I use RegexBuddy while working with regular expressions. From its library I copied the regular expression to match URLs. I tested successfully within RegexBuddy. However, when I copied it as Java String flavor and pasted it into Java code, it does not work. The following class prints false: Does anyone know what I am doing wrong? Answer Try the following

Equivalent of PHP’s print_r in Java?

last time I asked how to populate a data structure here. Now I would like to know if there’s something in Java, like the print_r I use in PHP, to represent what I have populated in the Maps and lists without having to do my own algorithm. Any ideas? Answer Calling toString on the collection should return a string containing

Exception thrown inside catch block – will it be caught again?

This may seem like a programming 101 question and I had thought I knew the answer but now find myself needing to double check. In this piece of code below, will the exception thrown in the first catch block then be caught by the general Exception catch block below? I always thought the answer would be no, but now I

How to pretty print XML from Java?

I have a Java String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nicely formatted XML. How do I do this? Note: My input is a String. My output is a String. (Basic) mock result: Answer Now it’s 2012 and Java can do more than it used to with

How can I lock a file using java (if possible)

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 do I have to

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, correspondingly, two kinds of data values that can be stored in

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 the benefit of being thread safe, so you can use a single instance throughout your application. It

Advertisement