Programming Articles - Page 2855 of 3366

AbstractSequentialList in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

146 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 −Example Live Demoimport 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 ... Read More

Java Program to read the next byte of data from the input stream

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

448 Views

The method java.io.InputStream.read() is used to read the next byte of data from the input stream. This method requires no parameters and it returns the next data byte in the form of int. If the stream end is reached, it returns -1.A program that demonstrates this is given as follows −Exampleimport java.io.InputStream; public class Demo {    public static void main(String[] args) throws Exception {       InputStream input = null;       int i;       char c;       try {          input = new FileInputStream("C://JavaProgram//data.txt");          System.out.println("The ... Read More

Java Program to mark the current position in this input stream

Alshifa Hasnain
Updated on 30-Dec-2024 19:14:42

534 Views

In this article, we will learn to mark and reset the current position in a file input stream in Java. Marking the current position in an input stream is a crucial functionality provided by Java’s InputStream class. It enables developers to reset the stream to a previously marked position, facilitating efficient data handling, especially in scenarios like re-reading or backtracking within a file. Key Concepts Following are the key concepts we need to know to mark the current position in this input stream − InputStream Class: The InputStream class is an abstract class that represents ... Read More

Java program to close this input stream and release any system resources associated with the stream

karthikeya Boyini
Updated on 11-Nov-2024 19:15:09

242 Views

This Java article discusses the InputStream.close() function to close the input stream and free up system resources. The method java.io.InputStream.close() is used to close this input stream and release any system resources associated with the stream. This method requires no parameters and returns no value. Also, the IOException is thrown when an I/O error occurs. Problem StatementGiven an input stream, write a Java program to close this input stream and release any system resources associated with it. Ensure that the program handles any exceptions that may occur if the stream is already closed or an I/O error is ... Read More

Create temporary file in specified directory in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

3K+ Views

A temporary file in the specified directory can be created using the method java.io.File.createTempFile(). This method requires three parameters i.e. the prefix to define the file name, the suffix to define the file extension and the directory in which the temporary file is to be created. It also returns the abstract path name for the temporary file created.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {      public static void main(String[] args) throws Exception {       File directory = new File("C:/JavaProgram");       File file = File.createTempFile("temp", ".java", directory);   ... Read More

Create temporary file with specified extension suffix in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

A temporary file with the specified extension suffix can be created using the method java.io.File.createTempFile(). This method requires two parameters i.e. the prefix to define the file name and the suffix to define the file extension. It also returns the abstract path name for the temporary file created.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) throws Exception {       File file = File.createTempFile("temp", ".java");       System.out.println(file.getAbsolutePath());       file.deleteOnExit();      } }The output of the above program is as follows ... Read More

Create a temporary file in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

7K+ Views

A temporary file can be created using the method java.io.File.createTempFile(). This method requires two parameters i.e. the prefix to define the file name and the suffix to define the file extension. It also returns the abstract path name for the temporary file created.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) throws Exception {       File file = File.createTempFile("temp", null);       System.out.println(file.getAbsolutePath());       file.deleteOnExit();    } }The output of the above program is as follows −OutputC:\Users\amit_\AppData\Local\Temp\temp6072597842246154962.tmpNow let us understand the above ... Read More

Get the Current Working Directory in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

7K+ 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 −Example Live Demopublic 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 ... Read More

Check if a directory is not empty in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

3K+ Views

The method java.io.File.list() is used to obtain the list of the files and directories in the specified directory defined by its path name. This list of files is stored in a string array. If the length of this string array is greater than 0, then the specified directory is not empty. Otherwise, it is empty.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File directory = new File("C:\JavaProgram");       if (directory.isDirectory()) {          String[] files = directory.list();   ... Read More

Rename file or directory in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

432 Views

The method java.io.File.renameTo() is used to rename a file or directory. This method requires a single parameter i.e. the name that the file or directory is renamed to and it returns true on the success of the renaming or false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file1 = new File("demo1.txt");          File file2 = new File("demo2.txt");          file1.createNewFile();          file2.createNewFile();         ... Read More

Advertisements