Found 9321 Articles for Object Oriented Programming

Can we define a class inside a Java interface?

V Jyothi
Updated on 20-Feb-2020 05:46:54

6K+ Views

Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.Exampleinterface Library {    void issueBook(Book b);    void retrieveBook(Book b);    public class Book {       int bookId;       String bookName;       int issueDate;       int returnDate;    } } public class Sample implements Library {    public void issueBook(Book b) {       System.out.println("Book Issued");    }    public void retrieveBook(Book b) { ... Read More

What is the use of marker interfaces in Java?

Johar Ali
Updated on 30-Jul-2019 22:30:21

401 Views

An interface with no methods in it is referred to as a tagging interface. There are two basic design purposes of tagging interfaces -Creates a common parent As with the EventListener interface, which is extended by dozens of other interfaces in the Java API, you can use a tagging interface to create a common parent among a group of interfaces. For example, when an interface extends EventListener, the JVM knows that this particular interface is going to be used in an event delegation scenario.Adds a data type to a classThis situation is where the term, tagging comes from. A class that ... Read More

How to truncate a file in Java?

Swarali Sree
Updated on 20-Feb-2020 09:55:21

1K+ Views

The flush() method of the FileWriter class flushes the contents of the file. You can use this method to truncate a file.Exampleimport java.io.File; import java.io.FileWriter; public class FileTruncate {    public static void main(String args[]) throws Exception {       File file = new File("myData");       FileWriter fw = new FileWriter(file, false);       fw.flush();       System.out.println("File truncated");    } }OutputFile truncated

How to open a plain text file in Java?

Samual Sam
Updated on 20-Feb-2020 09:54:30

239 Views

You can access a plain text using the File class.ExampleLive Demoimport java.io.File; public class ReadFile {    public static void main(String[] args) {       File f = null;       String str = "data.txt";       try {          f = new File(str);          boolean bool = f.canExecute();          String a = f.getAbsolutePath();          System.out.print(a);          System.out.println(" is executable: "+ bool);       } catch (Exception e) {          e.printStackTrace();       }    } }Output C:\Users\data is executable: true

How to create a pdf file in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

2K+ Views

You can create a PDF file using the PDF Box library. You can set the environment for pdf box by following Pdf Box Environment Tutorial. Example import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; public class CreatingEmptyPdf { public static void main(String args[]) throws IOException { PDDocument document = new PDDocument(); document.addPage(new PDPage()); document.save("C:/pdfBox/BlankPdf.pdf"); System.out.println("PDF created"); document.close(); } } Output PDF created

What are file operations in Java?

Monica Mona
Updated on 25-Feb-2020 10:23:05

169 Views

File class provides various methods to perform respective file operations.canRead(): This method tests whether the application can read the file denoted by this abstract pathname. It returns true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise.canWrite(): This method tests whether the application can modify the file denoted by this abstract pathname. It returns true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.createNewFile(): This method atomically creates a ... Read More

How to perform sort using Java?

karthikeya Boyini
Updated on 20-Feb-2020 09:52:19

74 Views

You can sort an array using sort() method of the Arrays class.ExampleLive Demoimport java.util.Arrays; public class MainClass {    public static void main(String args[]) throws Exception {       int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };       Arrays.sort(array);       printArray("Sorted array", array);       int index = Arrays.binarySearch(array, 1);       System.out.println("Didn't find 1 @ " + index);       int newIndex = -index - 1;       array = insertElement(array, 1, newIndex);       printArray("With 1 added", array);    }   ... Read More

How to calculate the size of folder using Java?

Sharon Christine
Updated on 20-Feb-2020 09:51:17

1K+ Views

You can get the size of a directory with the help of FileUtils.sizeofDirectory(File Name) method of FileUtils class.Exampleimport java.io.File; import org.apache.commons.io.FileUtils; public class Main {    public static void main(String[] args) {       long size = FileUtils.sizeOfDirectory(new File("C:/Windows"));       System.out.println("Size: " + size + " bytes");    } }OutputSize: 2048 bytes

How to find the file using Java?

Swarali Sree
Updated on 30-Jul-2019 22:30:20

102 Views

Following example shows the way to look for a particular file in a directory by creating a File filter. Following example displays all the files having file names beginning with 'b'.ExampleLive Demoimport java.io.*; public class Main {    public static void main(String[] args) {       File dir = new File("C:");             FilenameFilter filter = new FilenameFilter() {          public boolean accept (File dir, String name) {             return name.startsWith("b");          }       };       String[] children = ... Read More

How to create a Directory recursively using Java?

Samual Sam
Updated on 20-Feb-2020 09:49:22

191 Views

The java.io.File.mkdirs() creates the directory named by this abstract pathname, together with necessary and non-existent parent directories.ExampleLive Demoimport java.io.File; public class Main {    public static void main(String[] args) {       String directories = "D:\a\b\c\d ";       File file = new File(directories);       boolean result = file.mkdirs();       System.out.println("Status = " + result);    } }OutputStatus = true

Advertisements