Java Programming Tips


How to Use Java to Get a List of Running Processes in Linux

In linux, the ps command can be used to get a list of running processes. Type the following command in a linux console. ps -e -o command The above command prints all running processes including the processes owned by other users (as specified by the -e option). The -o command flag instructs ps to show […]

How to Check Even or Odd in Java

The following sample Java program shows how the modulus operator in Java can be used to check whether a given number is even or odd. The following program reads a number from the console using the Scanner class and System.in input stream, import java.util.Scanner; // Java program to check whether a number is even or […]

ASCII Art Generator Library in Java

ASCII art is a method of creating pictures using the normal printable characters used in computers (ASCII character set). ASCII art can be composed using normal text editors. However this process is very cumbersome. There are a number of software packages available that can convert images or text to ASCII art. If you are looking […]

How to Swap Two Numbers in Java

Swapping two number is easy when you use a temporary variable. The following Java programs swaps two numbers using a temporary variable of the same type. // Java program to swap numbers using temporary variable public class SwapNumbersWithTemp { public static void main(String[] args) { int a = 10; int b = 20; System.out.println("a="+a+",b="+b); int […]

How to List Files in a Directory Using Files.list

Java 8 introduced a number of utility classes intended to improve programmer productivity. The java.nio package was enhanced with a number of new methods. This article explores the list method in java.nio.file.Files class. This new method returns all the entries (files and directories) in the specified directory. However it is not recursive (for recursive use […]