Found 7442 Articles for Java

How to get file URI reference in Java?

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

5K+ 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.In general, an URI (Uniform Resource Identifier) represents a resource. The URI class of Java represents this format You can get the URI format of a file by invoking the toURI() method.ExampleFollowing Java example, prints the URI format of the file named samplefile.txtimport java.io.File; import java.io.IOException; import java.net.URI; public class GetURI {    public static void main(String args[]) throws IOException {       //Instantiating the File class       String ... Read More

How to create and store property file dynamically in Java?

Maruthi Krishna
Updated on 02-Aug-2019 11:18:19

3K+ Views

The .propertiesis an extension in java which is used to store configurable application. It is represented by the Properties class in Java, you can store a properties file and read from it using the methods of this class. This class inherits the HashTable class.Creating a .properties fileTo create a properties file −Instantiate the Properties class.Populate the created Properties object using the put() method.Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.ExampleThe Following Java program creates a properties file in the path D:/ExampleDirectory/import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class CreatingPropertiesFile {    public static ... Read More

How to compress and un-compress the data of a file in Java?

Maruthi Krishna
Updated on 02-Aug-2019 11:11:37

1K+ Views

Java provides a two classes namely, DeflaterOutputStream and InflaterInputStream for compressing and uncompressing data.Compressing a Single fileTo compress a single file −Create a FileInputStream object, by passing the path of the file to be compressed in String format, as a parameter to its constructor.Create a FileOutputStream object, by passing the path of the output file, in String format, as a parameter to its constructor.Create a DeflaterOutputStream object, by passing the above created FileOutputStream object, as a parameter to its constructor.Then, read the contents of the input file and write using the write() method of the DeflaterOutputStream class.Exampleimport java.io.FileOutputStream; import java.io.IOException; ... Read More

How to filter the files by file extensions and show the file names in Java?

Maruthi Krishna
Updated on 02-Aug-2019 11:06:45

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.To get the list of all the existing files in a directory this class provides five different methods to get the details of all files in a particular folder −String[] list()File[] listFiles()String[] list(FilenameFilter filter)File[] listFiles(FilenameFilter filter)File[] listFiles(FileFilter filter)Among these, the String[] list(FilenameFilter filter) method returns a String array containing the names of all the files and directories in the path represented by the current (File) object. But the retuned array contains the filenames ... Read More

How to get list of all files/folders from a folder in Java?

Maruthi Krishna
Updated on 12-Sep-2023 03:22:52

43K+ 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.To get the list of all the existing files in a directory this class provides the files class provides list() (returns names) and ListFiles() (returns File objects) with different variants.The List() methodThis method returns a String array which contains the names of all the files and directories in the path represented by the current (File) object.Using this method, you can just print the names of the files and directories.ExampleThe following Java program ... Read More

What is the importance of a StringWriter in Java?

raja
Updated on 22-Nov-2023 12:03:35

181 Views

The StringWriter class is s subclass of Writer class and it writes the String to an output stream. To write a string, this character stream collects the string into a string buffer and then constructed a string. The buffer of StringWriter automatically grows according to data. The important methods of StringWriter class are write(), append(), getBuffer(), flush() and close(). Syntax public class StringWriter extends Writer Example import java.io.*; public class StringWriterTest { public static void main(String args[]) { String str = "Welcome to Tutorials Point"; try { ... Read More

How to make a singleton enum in Java?

Vivek Verma
Updated on 23-Jun-2025 12:13:12

3K+ Views

In Java, an enum is a special class used to represent a group of constants (that cannot be changed, like a final variable), such as Days: SUNDAY, MONDAY, TUESDAY, etc. Following is the syntax to create an Enum class in Java: enum ClassName { VALUE1, VALUE2, VALUE3, // ... VALUEN; } Where, enum is a reserved keyword in Java used to define an enum class, ClassName is the name of the enum, and VALUE1, VALUE2, VALUE3, ..., VALUEN are the ... Read More

Can you use a switch statement around an enum in Java?

Maruthi Krishna
Updated on 02-Jul-2020 11:48:07

1K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }You can also define an enumeration with custom values to the constants declared. But you need to have an instance variable, a constructor and getter method to return values.Using enumeration with switchA switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being ... Read More

How do you print the content of an array in Java?

Maruthi Krishna
Updated on 02-Jul-2020 11:49:24

504 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

Difference between import and package in Java?

Maruthi Krishna
Updated on 02-Jul-2020 11:50:24

3K+ 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 packageYou 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 −package com.tutorialspoint.mypackage; public class Sample{    public void demo(){       System.out.println("This is a method of the sample class");    }    public static void main(String ... Read More

Advertisements