Creating New Directories in Java
Learn to create a new single directory or nested directory along with parent directories in a specified path using Java IO and NIO classes.
“Core Java” is Sun’s term, used to refer to Java SE, the standard edition and a set of related technologies, like the Java VM, CORBA, etc. This is mostly to differentiate from others like Java ME or Java EE. “Core Java” is Oracle’s definition and refers to subset of Java SE technologies.
The Core Java technologies and application programming interfaces (APIs) are the foundation of the Java Platform, Standard Edition (Java SE). They are used in all classes of Java programming, from desktop applications to Java EE applications.
Learn to create a new single directory or nested directory along with parent directories in a specified path using Java IO and NIO classes.
Learn to count all the lines in file with the efficient solutions like Stream of lines, List and LineNumberReader classes.
Learn to a few performance-proven methods to check if a given directory is empty or not using the Java NIO Files and DirectoryStream classes.
Learn to get the creation date and time of a file in Java using Java NIO Files class in different time units since epoch and to Instant.
Learn to create, detect and find targets of the symbolic links (also known as symlinks or soft links) using examples in Java.
Learn to make the file empty by deleting all the content in it. This makes the file size zero without deleting the file itself.
Learn to write the given byte[] into a file using different solutions. We will be using the Java NIO, Commons IO and Guava APIs that provide simple APIs for this usecase.
Learn to read all the lines from a file into ArrayList using Java IO and NIO APIs, Stream API, Commons IO and Guava classes.
This Java tutorial lists down the published tutorials on this blog related to Stream API, methods and its related concepts with examples.
Learn to read a specific line from a text file in Java. We will learn to write the solution for small files as well as large files as well.
Learn to read all lines from a large file (size in GB) in Java and avoid any performance pitfalls such as very high usage of memory or even OutOfMemoryError if the File is large enough.
Learn to delete a specified file or directory in Java. Note that different methods behave differently for deleting non-empty directories.
Learn to rename a file or directory at a specified path or move to a new directory in Java. We will learn to use the classes from Standard IO, New IO, Guava and Commons IO.
Learn the difference between path, absolute and canonical paths. Also, learn to get the path of a file in Java using standard IO and New IO classes.
Learn to get the size of a file or a directory in Java using IO classes File, Files and Common IO’s FileUtils class.
After the legacy Java finalization has been deprecated (JEP-421) in Java 18, learn the basics of Cleaner API and how to use it.
Learn to create and operate on the streams of primitive types (IntStream, LongStream and DoubleStream) in Java with examples.
The legacy Java API had been a big pain point for Java developers for quite a long time. With the release of Java 8 release (JSR-310), package java.time introduced the new immutable classes that solved the issue with the original classes. Following articles have been published to help you start with some very common …
Learn to add or subtract hours, minutes or seconds from a given date and time in Java using various date-time classes and their plus methods.
Learn to get the year, month and day from a given date in Java using the new LocalDate classes as well as legacy Date and Calendar classes.
Learn to set the default time zone used by the JVM using an environment variable, JVM argument and TimeZone class.
Learn to find the day of the week for a given date using the legacy Date and Calendar classes as well as the new Java 8 Date API. 1. Overview There may be a requirement to show the day of the week (Monday to Sunday) in the UI, and then …
Learn to get the start and the end of a date using Java date API, e.g., LocalDateTime and ZonedDateTime. We may require to get this information in many cases. For example, we have to filter the events that occurred at different times in a single day based on event timestamps. …
For any application supporting multiple locales and timezones, it is often the requirement to show the date and timestamp adjusted to the user’s local timezone offset.
Learn about ZonedDateTime and OffsetDateTime classes in Java, and what are the main differences between both classes in depth. 1. Understanding Zone Id and Zone Offsets Before jumping to the internals of the classes, let’s make sure we understand the difference between the zone identifier and zone offsets. Most programming …
Learn to convert a given duration in milliseconds to hours, minutes and seconds; and format to to HH:mm:ss and any other custom pattern.
Starting with JDK 8, we have a comprehensive date-time API containing classes such as LocalDate, LocalTime, LocalDateTime, ZonedDateTime, OffsetDateTime, and OffsetTime. These classes allow easy formatting of the date-time output via DateTimeFormatter.ofPattern(). The API also allows further customize the output based on specified Locale information to support localization and internationalization features. In …
Java supports creating and modifying the date and time using primarily two packages java.time and java.util. The package java.time was part of Java 8 release (JSR-310) that introduced the new immutable classes solving the shortcomings of the legacy java.util.Date and java.util.Calendar classes. 1. Legacy Date Time API (Prior to Java …
Arrays in Java, conceptually, are no different than in other programming languages. The java.util.Arrays class provides many useful static methods to work with arrays and perform common operations on them. 1. Array Basics 2. Array Operations 3. Searching and Sorting 4. Conversions 5. Advance Topics Happy Learning !! Sourcecode on …
Learn why we get the ArrayStoreException while working with arrays in Java and how to identify the root cause and fix this error. 1. Root Cause of ArrayStoreException Java arrays are covariant and support the subtyping rules of Java, an array of type T[] may contain elements of type T …
Learn to find the sum and average of the numbers stored in an array. We will be using the Java Stream API and simple for loop to find these values. Note that the numbers in Java are represented with 8 primitives i.e. short, char, byte, boolean, int, float, long and …
Learn to convert a list to an array in Java and also convert a given array to a list. We will learn the conversions using the core Java APIs. 1. Converting List to Array We can use the following two approaches to convert a given list to an array. 1.1. …
Learn to convert a stream to an array and vice versa in Java. We will learn to convert for the primitives as well as the Object types. Note that Java Stream API provides the following specialized classes for the stream of primitives. These classes support many useful sequential and parallel …
Learn to remove the array items in Java by the index positions as well as by the item values. Note that theoretically, we can remove an array item in two ways: Create a new array and copy all items from the original array, except the index or item to be …
Learn to convert an array of primitives (int, long or double) to an array of objects (Integer, Double or Long), and vice versa. For example, we will convert int[] to Integer[] and then convert back the Integer[] to int[]. Note that all the techniques follow the same logic for the …
Learn to split an array in Java using different ways. We will learn to split the array into equal parts, at the specified index and of equal lengths. 1. Arrays.copyOfRange() API The copyOfRange() creates a new array of the same type as the original array, and contains the items of …
Learn to concatenate two primitive arrays or objects arrays to create a new array consisting of the items from both arrays. We will learn to merge array items using simple for-loop, stream API and other utility classes. Note that no matter which technique we use for merging the arrays, it …
Learn to compare two arrays using different techniques in Java. We will learn the array comparison from using simple for loops to inbuilt Java APIs. 1. How is the Arrays Comparison Done? In Java or any other programming language, the basics behind comparing two arrays are the same. Two arrays …
Learn to check if a given array is already sorted for a defined sorting order i.e. ascending, descending or the custom order. 1. Overview An array is considered sorted if every item in the array is greater or lesser than its predecessor based on the sorting order of the array. …
Learn to find the top N items in a given array in Java. Note that we should be very clear about the meaning of top N items. The suggested solution may need minor changes based on our interpretation and requirement. For example, in this tutorial, top N items mean the …
Learn to find the smallest and the largest item in an array in Java. We will discuss different approaches from simple iterations to the Stream APIs. In the given examples, we are taking an array of int values. We can apply all the given solutions to an array of objects …
Arrays are fixed-size data structures and array sizes can not be changed once they have been initialized. However, in cases where array size needs to be changed, we have to follow one of the given approaches in this tutorial. 1. Using java.util.Arrays.copyOf() The copyOf(originalArray, newLength) method takes an array and …
Learn how to reverse or invert an array in Java. A reversed array is of equal size to the original array and contains the same items but in the reverse order. 1. Collections.reverse() API The easiest way to reverse the array is to use the existing APIs built for this …
Learn to declare and initialize arrays using different techniques and their differences. Apart from directly accessing the arrays, we will also be using the java.util.Arrays and Stream API that provides several useful methods to work with arrays in Java. Note that an array is a contiguous block of memory so …
Java is one of the most popular programming languages for developing all kinds of desktop applications, web applications and mobile apps. The following articles will help you quickly become familiar with Java language and move towards more complex concepts such as API and cloud development. 1. Java Language Basics Start …
Learn to sort a Java Set, List and Map of primitive types and custom objects using Comparator, Comparable and new lambda expressions. We will learn to sort in ascending and descending order as well. 1. Sorting a List of Objects To sort a list of objects, we have two popular …
Flogger, developed, used and recommended by Google, is a fluent style logging API for Java. Apart from fluency, flogger offers many more other features than existing logging frameworks that we will learn in this tutorial. 1. Flogger Dependencies Flogger, similar to SLF4J, acts as an abstraction and uses the underlying …
Logback XMLLayout outputs events into XML format that is log4j.dtd compliant (instead of an W3C XML Schema). The generated XML logs can be viewed using the opensource Log viewer tools such as Apache Chainsaw and vigilog. 1. XMLLayout Sample Configuration The given configuration logs into the rolling files using the …
Learn to sort a Java List or Stream with Comparator‘s nullsFirst() and nullsLast() methods. The Stream may contain null values, or the custom objects may have null field values. Failing to handle the null values during comparison will cause NullPointerException in runtime. 1. Introduction In this tutorial, we will learn …
Java 17 was released on September 14, 2021. Java 17 is an LTS (Long Term Support) release, like Java 11 and Java 8. Oracle will support it for bug fixes, patches and performance enhancements for the next few years. Spring 6 and Spring boot 3 will have first-class support for …
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.