How-to articles, tricks, and solutions about JAVA

How do I check if a file exists in Java?

To check if a file exists in Java, you can use the exists method of the File class from the java.io package. This method returns true if the file exists, and false if it doesn't. Here's an example of how you can use this method:

How do I concatenate two strings in Java?

To concatenate two strings in Java, you can use the + operator.

How do I connect to a SQL Server 2008 database using JDBC?

To connect to a SQL Server database using JDBC, you will need to use the JDBC driver for SQL Server.

How do I convert 2018-04-10T04:00:00.000Z string to DateTime?

To convert a string in the format "2018-04-10T04:00:00.000Z" to a DateTime object in Java, you can use the org.joda.time.DateTime class and the DateTimeFormatter class.

How do I convert a Map to List in Java?

You can use the entrySet() method of the Map interface to get a Set view of the mappings contained in the map, and then create a List from this set using the new

How do I convert a String to an InputStream in Java?

To convert a String to an InputStream in Java, you can use the ByteArrayInputStream class, which allows you to create an InputStream from a byte array.

How do I convert a String to an int in Java?

To convert a String to an int in Java, you can use the parseInt() method of the Integer class. Here's an example:

How do I convert from int to Long in Java?

To convert an int to a long in Java, you can simply use the typecast operator (long) and place it in front of the value you want to convert.

How do I convert from int to String in Java?

To convert an int to a String in Java, you can use the Integer.toString() method. For example

How do I copy an object in Java?

There are several ways to copy an object in Java. Here are a few options:

How do I create a Java string from the contents of a file?

To create a Java string from the contents of a file, you can use the following code:

How do I declare and initialize an array in Java?

There are several ways to declare and initialize an array in Java. Here are a few examples: Declare and initialize an array of integers with size 5

How do I determine whether an array contains a particular value in Java?

To check if an array contains a particular value in Java, you can use the contains() method of the List interface, which is implemented by the ArrayList class.

How do I do a HTTP GET in Java?

To send an HTTP GET request in Java, you can use the java.net.URL and java.net.HttpURLConnection classes.

How do I efficiently iterate over each entry in a Java Map?

There are several ways to iterate over the entries in a Map in Java. Here are some options:

How do I find out my MySQL URL, host, port and username?

To find out your MySQL connection details, you will need to look in your MySQL configuration files. The specific location of these files will depend on how you installed MySQL, but common locations include /etc/my.cnf,

How do I find out what keystore my JVM is using?

To find out which keystore the Java Virtual Machine (JVM) is using, you can use the javax.net.ssl.KeyManagerFactory class and the getDefaultAlgorithm method.

How do I find where JDK is installed on my windows machine?

To find where the JDK is installed on your Windows machine, you can follow these steps:<br>

How do I fix a NoSuchMethodError?

A NoSuchMethodError is thrown when a program tries to call a method that does not exist in the class or interface.

How do I generate random integers within a specific range in Java?

To generate a random integer within a specific range in Java, you can use the nextInt method of the Random class.

How do I get a class instance of generic type T?

To get a class instance of a generic type T, you can use the new operator and specify the type argument when creating an instance of the class. Here's an example:

How do I get a Date without time in Java?

To get a java.util.Date object with the time set to 00:00:00 (midnight), you can use the toInstant() method to convert a LocalDate object to an Instant, and then use the atZone() method to convert the Instant to a ZonedDateTime object, and finally use the

How do I get a platform-dependent new line character?

To get a platform-dependent new line character in Java, you can use the System.lineSeparator() method.

How do I get the file extension of a file in Java?

To get the file extension of a file in Java, you can use the File class and its getName() method to get the file name, and then use the substring() method of the String class to extract the extension from the file name.