Object Oriented Programming Articles

Page 228 of 589

Name validation using Java Regular Expressions

Fendadis John
Fendadis John
Updated on 11-Mar-2026 1K+ Views

The name can be validated using the java.util.regex.Pattern.matches() method. This method matches the regular expression for the name and the given input name and returns true if they match and false otherwise.A program that demonstrates this is given as follows:Examplepublic class Demo {    public static void main(String args[]) {       String name = "John Harry Smith";       String regexName = "\p{Upper}(\p{Lower}+\s?)";       String patternName = "(" + regexName + "){2, 3}";       System.out.println("The name is: " + name);       System.out.println("Is the above name valid? " + name.matches(patternName));    } ...

Read More

Create a directory in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 911 Views

A directory can be created with the required abstract path name using the method java.io.File.mkdir(). This method requires no parameters and it returns true on the success of the directory creation or false otherwise.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("c:\demo1");          file.createNewFile();          boolean flag = file.mkdir();          System.out.print("Directory created? " + flag);       } catch(Exception e) {     ...

Read More

Display File class constants in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 170 Views

The java.io.File class has display constants File.separatorChar and File.pathSeparatorChar mainly. The File.separatorChar is ‘/’ and the File.pathSeparatorChar is ‘:’ for Unix.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          System.out.println("File.pathSeparatorChar = " + File.pathSeparatorChar);          System.out.println("File.separatorChar = " + File.separatorChar);       } catch(Exception e) {          e.printStackTrace();       }    } }The output of the above program is as follows −OutputFile.pathSeparatorChar = : File.separatorChar = /Now let us ...

Read More

List the file system roots in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 424 Views

The method java.io.File.listRoots() is used to list the file system roots in Java. This method requires no parameters. It returns the available file system roots in the form of an array of file objects and if the file system roots cannot be determined, it returns null.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       File[] roots = File.listRoots();       try {          for(File r : roots) {             System.out.println(r);          } ...

Read More

Get file extension name in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 742 Views

The file extension is the suffix that is attached to the computer file and it denotes the format of the file. A program that demonstrates getting the file extension name is given as follows −Exampleimport java.io.File; public class Demo {    private static String fileExtension(File file) {       String name = file.getName();       if(name.lastIndexOf(".") != -1 && name.lastIndexOf(".") != 0)          return name.substring(name.lastIndexOf(".") + 1);       else          return "";    }    public static void main(String[] args) {       File file = new File("demo1.txt");   ...

Read More

File Path with double slash in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

The double slash in the file path is required as to create the ’\’ character , another ‘\’ needs to be added to escape it. A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       File file = new File("C:\jdk11.0.2\demo1.txt");       System.out.println(file);    } }The output of the above program is as follows −OutputC:\jdk11.0.2\demo1.txtNow let us understand the above program.The file path of the file is provided with double slash and then it is printed. A code snippet that demonstrates this is given ...

Read More

Java Program to strip a filename of its extension after the last dot

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 4K+ Views

The method removeExtension() is used to strip a filename of its extension after the last dot. This method requires a single parameter i.e. the file name and it returns the file name without its extension.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static String removeExtension(String fname) {       int pos = fname.lastIndexOf('.');       if(pos > -1)          return fname.substring(0, pos);       else          return fname;    }    public static void main(String[] args) {       System.out.println(removeExtension("c:\JavaProgram\demo1.txt"));   ...

Read More

Delete file or directory on termination in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 332 Views

A file or directory can be deleted on termination of the program i.e. after the virtual machine terminates using the method java.io.File.deleteOnExit(). This method requires no parameters and it does not return a value.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          System.out.println("File: " + file);          file.deleteOnExit();       } catch(Exception e) {          e.printStackTrace();   ...

Read More

Get the Current Working Directory in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 8K+ Views

The method java.lang.System.getProperty() is used to obtain the system property. This system property is specified by the key which is the parameter for the method. To obtain the current working directory, the key used is user.dir.A program that demonstrates this is given as follows −Examplepublic class Demo {    public static void main(String[] argv) throws Exception {       String currentDirectory = System.getProperty("user.dir");       System.out.println("The current working directory is " + currentDirectory);    } }The output of the above program is as follows −OutputThe current working directory is c:\JavaProgramNow let us understand the above program.The current working ...

Read More

AbstractSequentialList in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 199 Views

The Java Collection Framework contains the AbstractSequentialList class. This class is used to create and implement an unmodifiable list. Also this class implements the Collection interface and the AbstractCollection class.A program that demonstrates this is given as follows −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       AbstractSequentialList list = new LinkedList();       list.add("John");       list.add("Martha");       list.add("Sally");       list.add("Susan");       list.add("Harry");       System.out.println("The elements are: " + list);    } }The output of the above program is as follows −OutputThe ...

Read More
Showing 2271–2280 of 5,881 articles
« Prev 1 226 227 228 229 230 589 Next »
Advertisements