How-to articles, tricks, and solutions about JAVA
To get the file name from a String containing the absolute file path in Java, you can use the following methods:
To get the last character of a string in Java, you can use the charAt() method of the String class, which returns the character at a specific index in the string.
To import the javax.servlet or jakarta.servlet API in an Eclipse project, follow these steps:
To initialize a byte array in Java, you can use the array initializer syntax, like this:
To invoke a Java method when given the method name as a string, you can use reflection. Reflection is a feature of the Java language that allows you to inspect and manipulate classes, fields, and methods at runtime.
There are several ways to join two lists in Java. Here are a few options:
To load a file from the resource folder in a Java application, you can use the ClassLoader and getResourceAsStream method.
To make a delay in Java, you can use the Thread.sleep method which will pause the current thread for a specified number of milliseconds. Here's an example:
To make the return type of a method generic in Java, you can use a type parameter. A type parameter is a placeholder for a specific type that will be specified when the method is called.
To measure time elapsed in Java, you can use the System.currentTimeMillis() method to get the current time in milliseconds, then subtract the start time from the end time to get the elapsed time.
To parse command line arguments in Java, you can use the main() method of your application's entry point class.
To print a Java object in a more readable format, you can override the toString() method in your class.
To programmatically determine the operating system in Java, you can use the System.getProperty method and pass it the "os.name" property.
There are several ways to read an InputStream and convert it to a String in Java. One way is to use the BufferedReader and InputStreamReader classes to read the InputStream line by line, and use a StringBuilder to construct the final String:
To remove repeated elements from an ArrayList in Java, you can use the removeAll method and pass it a Collection containing the elements to be removed.
To replace a character in a string in Java, you can use the String.replace(char oldChar, char newChar) method.
A ClassNotFoundException occurs when the Java virtual machine (JVM) is unable to find a class that has been referenced in your code. This can happen for a number of reasons, including:
The java.net.BindException: Address already in use: JVM_Bind error occurs when you try to bind a socket to a local address and port, but the address and port are already in use by another process. This can happen when you try to start a server on a port
To reverse an int array in Java, you can use a loop to swap the elements at the beginning and end of the array, and then move the pointers inward until they meet in the middle of the array.
To save a String to a text file in Java, you can use the write method of the FileWriter class. Here's an example of how you can do this:
To set an environment variable from Java, you can use the System.setProperty() method.
To set the proxy to be used by the Java Virtual Machine (JVM), you can use the -Dhttp.proxyHost, -Dhttp.proxyPort, and -Dhttp.nonProxyHosts options when starting the JVM.
To specify the JDK version that Gradle should use, you can include the org.gradle.java.home property in the gradle.properties file in the root project directory.
To tell Maven to use the latest version of a dependency, you can use the [RELEASE] or [LATEST] version range in the <version> element of the dependency in your pom.xml file.
To test a class that has private methods, fields, or inner classes, you can do the following: