الثلاثاء، 25 فبراير 2020

Java 8 Files walk() Examples

1. Overview


In this tutorial, We'll be learning the Files API walk() method in java 8. walk() method is part of the Files class and java.nio.file package.

This method is used to walk through any given directory and retrieves Stream<Path> as the return value. This method traverses through all its subdirectories as well.


Java 8 Files walk() Examples



API Description:


Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. The file tree is traversed depth-first, the elements in the stream are Path objects that are obtained as if by resolving the relative path against start.

Note: This method must be used within a try-with-resources statement.

In this article, We'll see its syntax and example programs on how to list all the files in the directory, list directories and specific file patterns such as .csv or file name contains 'Match' word.

الجمعة، 21 فبراير 2020

Java 8 – TemporalAdjusters examples

1. Overview


In this tutorial, We'll learn about the new Java 8 TemporalAdjusters class and its methods along with examples.

This is part of the new java 8 date and time api and in java.time.temporal package and class name TemporalAdjusters. This class now has predefined temporal objects to change the data as we want.

TemporalAdjusters class is a collection of TemporalAdjuster implementations.

First We will see the example programs on TemporalAdjusters class and next take a look at the Temporal interface.

At last, We'll define custom TemporalAdjuster implementation.

Java 8 – TemporalAdjusters examples


الخميس، 13 فبراير 2020

Files Compressing and Decompressing in Java (Zipping and Unzipping)

1. Introduction


In this tutorial, We will be learning how to compress a file in java and how to decompress the same compressed file using java java.util.zip package.

Java is built with zipping(archiving) files utilities in java.util.zip which you can find all the zipping and unzipping related utilities.

In this article, we will see how to compress a file, multiple files, and files in folder or directory. At last, we will how to unzip an archived file. All the examples shown are can be done in java 8 or higher versions.

Files Compressing and Decompressing in Java (Zipping and Unzipping)


الاثنين، 10 فبراير 2020

Java Program to Check Whether a Number is Positive or Negative

1. Introduction


In this programming tutorial, We will learn how to check the given number is a positive or negative value. This can be done using if else condition and with '<', '>' operators.

If-else Statement in java examples.

Java Program to Check Whether a Number is Positive or Negative


الأربعاء، 5 فبراير 2020

Java Program to Check Whether a Number is Palindrome or Not

1. Introduction


In this tutorial, we will learn how to check whether a given number is a palindrome or not.

Definition: A palindrome is a word, number, phrase, or other sequences of characters that reads the same backward as forward, such as madam, racecar.
Palindrome ref

But in out case input is a number such as 121, 345543 are examples of the palindrome.

To understand this problem, you must have knowledge on reversing a number in java.

You can achieve and implement in many ways using for loop and while loop.

A) Number Palindrome check using For loop
B) Number Palindrome check using while loop
C) Number Palindrome check with StringBuffer.reverse()

In approach A and B, We need to reverse the number using '%' and '/' operators and compares the given number with the reversed number. If both are the same then it is a palindrome, otherwise, it is not.


الاثنين، 3 فبراير 2020

Java Program to Find GCD of Two Numbers

1. Introduction


In this tutorial, We will learn how to calculate the GCD for a given two numbers. GCD means Greatest Common Divisor.

The GCD of two numbers is the largest positive integer number that divides both the numbers without leaving any remainder (remainder should be 0). In some of the cases, GCD is called the Highest Common Factor (HCF).

Let us take an example GCD of 60, 90 is 30.


Factors of a number 60 : 2 * 2 * 3 * 5
Factors of a number 90 : 2 * 3 * 3 * 5

Common unique factors are: 2, 3, 5

Multiplication of common factors = 2 * 3 * 5 = 30

This can be done in two approaches.

A) Running the loop till the minimum of two numbers
B) GCD finding using the subtraction method.

These two methods are implemented without using a recursive approach.


الأحد، 2 فبراير 2020

Java Program to Calculate Simple Interest

1. Introduction


In this tutorial, We will learn how to write a java program to find simple interest.

To calculate simple interest, you should know the formula for it.

Simple Interest(SI) = (P × N × R)/100

Here is the meaning for each team in it.

P is Principal amount
R is the rate per annum
N is time in years