Found 7442 Articles for Java

Write program to reverse a String without using reverse() method in Java?

Maruthi Krishna
Updated on 02-Aug-2019 12:27:11

5K+ Views

You can reverse a String in several ways, without using the reverse() function.Using recursion − Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. You can reverse a string using recursive function as shown in the following program.Exampleimport java.util.Scanner; public class StringReverse {    public static String reverseString(String str){       if(str.isEmpty()){          return str;       }else{          return reverseString(str.substring(1))+str.charAt(0);       } ... Read More

What are the different ways to iterate over an array in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:21:33

19K+ Views

In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0.You can access the elements of an array using name and position as −System.out.println(myArray[3]); //Which is 1457Creating an array in JavaIn Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7]; myArray[0] = 1254; myArray[1] = 1458; myArray[2] = ... Read More

What are the different ways of copying an array into another array in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:26:07

810 Views

In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0. You can access the elements of an array using name and position as −System.out.println(myArray[3]); //Which is 1457 Creating an array in Java:In Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7]; myArray[0] = 1254; myArray[1] = ... Read More

How to delete folder and sub folders using Java?

Maruthi Krishna
Updated on 02-Aug-2019 12:13:29

3K+ Views

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.The delete() method of the File class deletes the file/directory represented by the current File object.This ListFiles() method of the File class returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.Therefore, to delete a folder along with its sub directories and files, you need to define a recursive method.ExampleFollowing Java program deletes the specified directory recursively −import java.io.File; ... Read More

How to get file last modified time in Java?

Maruthi Krishna
Updated on 02-Aug-2019 12:01:30

3K+ Views

He class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.ExampleFollowing Java program gets the last modified time of a directory −import java.io.File; import java.util.Date; public class GettingLastmodifiedTime {    public static void main(String args[]) {       String filePath = "D://ExampleDirectory//";       //Creating the File ... Read More

Different ways to traverse an Array in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:28:53

16K+ Views

In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0.You can access the elements of an array using name and position as −System.out.println(myArray[3]); //Which is 1457Creating an array in JavaIn Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7]; myArray[0] = 1254; myArray[1] = 1458; myArray[2] ... Read More

How to list out the hidden files in a Directory using Java program?

Maruthi Krishna
Updated on 02-Aug-2019 11:47:25

577 Views

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.The isHidden() method of the File class verifies weather the (abstract path of) file/directory represented by the current File object, is hidden.The ListFiles() method of the File class returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.Therefore, to list all the hidden files in a directory get all the file objects using the ListFiles() method, verify weather each ... Read More

What are the best practices to keep in mind while using packages in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:11:58

868 Views

You can create the .class files of all the Java classes and interfaces related to each other in one folder automatically by declaring them under same package. A package is nothing but a directory storing classes and interfaces of a particular concept.Creating a packageYou can create a package and add required classes/interfaces in it just by declaring the package on the top of the Class/Interface files using the keyword package as −Package package_name;ExampleFollowing Java program, demonstrates the declaration of a package in Java.package com.tutorialspoint.mypackage; public class Sample{    public void demo(){       System.out.println("This is a method of the ... Read More

Can I import same package twice? Will JVM load the package twice at runtime?

Maruthi Krishna
Updated on 23-Nov-2023 10:04:58

2K+ Views

In Java classes and interfaces related to each other are grouped under a package. Package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in java.io package. Creating a Package You can group required classes and interfaces under one package just by declaring the package at the top of the Class/Interface (file) using the keyword package as − Example public class Sample{ public void demo(){ System.out.println("This is a method of the sample class"); } ... Read More

How to write a program to copy characters from one file to another in Java?

Maruthi Krishna
Updated on 02-Aug-2019 11:24:50

3K+ Views

To copy the contents of one file to other character by characterExampleimport java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyFiles {    public static void main(String[] args) throws IOException {       //Creating a File object to hold the source file       File source = new File("D:\ExampleDirectory\SampleFile.txt");       //Creating a File object to hold the destination file       File destination = new File("D:\ExampleDirectory\outputFile.txt");       //Creating an FileInputStream object       FileInputStream inputStream = new FileInputStream(source);       //Creating an FileOutputStream object       FileOutputStream outputStream = ... Read More

Advertisements