Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 2446 of 3366
691 Views
While defining a method, In general, we will specify the arguments it accepts along with the type as −myMethod(int a, String b){ }Suppose if you need to accept more than one variable of the same type you need to specify the variables one after the other as −myMethod(int a, int b, int c){ }You can also pass a variable number of arguments of a particular type, to a method. These are known as variable arguments or, varargs. They are represented by three dots (…)Syntaxpublic myMethod(int ... a) { // method body }Once you use variable arguments as a parameter ... Read More
502 Views
In most scenarios, if you try to write content to a file, using the classes of the java.io package, the file will be overwritten i.e. data existing in the file is erased and the new data is added to it.But, in certain scenarios like logging exceptions into a file (without using logger frameworks) you need to append data (message) in the next line of the file.You can do this using the Files class of the java.nio package. This class provides a method named write() which acceptsAn object of the class Path, representing a file.A byte array holding the data to ... Read More
248 Views
The getClass() method is from Object class and it returns an instance of the Class class. When we declare a new instance of an object, it will be referring to a class. There can only be one class per JVM but multiple object referring to it. So when we get the class of two objects, they might be referring to the same class. Syntax public final Class getClass() Example class User { private int id; private String name; public User(int id, String name) { this.id = id; this.name ... Read More
17K+ Views
A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to write the contents of a .csv file using a Java program.Maven dependency com.opencsv opencsv 4.4 org.apache.commons commons-lang3 3.9 The CSVWriter class of the com.opencsv package represents a simple csv writer. While instantiating this class you need to pass a Writer object representing the file, to which you want to write the data, as a parameter to its constructor.It provides methods named writeAll() and writeNext() to write data to a .csv file.Using the ... Read More
4K+ Views
A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to read the contents of a .csv file using a Java program.Maven dependency com.opencsv opencsv 4.4 org.apache.commons commons-lang3 3.9 The CSVReader class of the com.opencsv package represents a simple CSV reader. While instantiating this class you need to pass a Reader object representing the file to be read as a parameter to its constructor. It provides methods named readAll() and readNext() to read the contents of a .csv fileUsing the readNext() methodThe readNext() ... Read More
1K+ 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 mkdir() method of this class creates a directory with the path represented by the current object.Therefore, to create a directory −Instantiate the File class by passing the path of the directory you need to create, as a parameter (String).Invoke the mkdir() method using the above-created file object.ExampleThe following Java example reads the path and name of the directory to be created, from the user, and creates it. Live Demoimport java.io.File; import java.util.Scanner; ... Read More
841 Views
Using the File classThe class named File of the java.io package represents a file or directory (pathnames) in the system. This class provides various methods to perform various operations on files/directories.This class provides various methods to manipulate files, The rename() method of the File class accepts a String representing a destination file and, renames the abstract file path of the current file to the given one.This method actually moves the file from the source path to the destination path.Example Live Demoimport java.io.File; public class MovingFile { public static void main(String args[]) { //Creating a source file object ... Read More
12K+ Views
The class named File of the java.io package represents a file or directory (pathnames) 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)The ListFiles() methodThis method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The following Java program prints the name, path and, size ... Read More
798 Views
To read a fixed number of elements from a file you can either read a required number of data elements from the file and process them or, read the entire file into a collection or an array and process it for every n element.ExampleFollowing Java program, reads the contents of a file 10 words at a time and prints them in a separate line.import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadingData { public static void main(String args[]) throws FileNotFoundException { //Creating an object of the File to read data File file = ... Read More
23K+ Views
A JFrame is a subclass of Frame class and the components added to a frame are referred to as its contents, these are managed by the contentPane. We can add the components to a JFrame to use its contentPane instead. A JFrame is like a Window with border, title, and buttons. We can implement most of the java swing applications using JFrame.By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.Syntaxpublic void setLocationRelativeTo(Component c)Exampleimport javax.swing.*; import java.awt.*; public class JFrameCenterPositionTest extends JFrame { public JFrameCenterPositionTest() { ... Read More