Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 50 of 50

How many ways can we read data from the keyboard in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 5K+ Views

The java.io package provides various classes to read write data from various sources and destinations.You can read data from user (keyboard) using various classes such as, Scanner, BufferedReader, InputStreamReader, Console etc.Using Scanner classFrom Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions. By default, whitespace is considered as the delimiter (to break the data into tokens).To read data from keyboard you need to use standard input as source (System.in). For each datatype a nextXXX() is ...

Read More

How to set file permissions in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 8K+ Views

In general, whenever you create a file you can restrict/permit certain users from reading/writing/executing a file.In Java files (their abstract paths) are represented by the Files class of the java.io package. This class provides various methods to perform various operations on files such as read, write, delete, rename etc.In addition, this class also provides the following methods −setExecutble() − This method is sued to set the execute permissions to the file represented by the current (File) object.setWritable() − This method is used to set the write permissions to the file represented by the current (File) object.setReadable() − This method is ...

Read More

Is it possible to change directory by using File object in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 2K+ Views

The File classThe 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.This class provides various methods to manipulate files, The renameTo() 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 source path to the destination path.Exampleimport java.io.File; public class MovingFile {    public static void main(String args[]) {       //Creating a source file object       ...

Read More

How to read contents of a file using Scanner class?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 15K+ Views

From Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions. By default, whitespace is considered as the delimiter (to break the data into tokens).To read various datatypes from the source using the nextXXX() methods provided by this class.Reading the contents of a file −To read the contents of a file, Scanner class provides various constructors.Sr.NoConstructors and Description1Scanner(File source)Used to read data from the file represented by the given File object.2Scanner(InputStream source)Used to read data from ...

Read More

How to convert InputStream object to a String in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 11K+ Views

Java provides I/O Streams to read and write data where, a Stream represents an input source or an output destination which could be a file, i/o devise, other program etc.There are two types of streams available −InputStream − This is used to read (sequential) data from a source.OutputStream − This is used to write data to a destination.FileInputStreamThis class reads the data from a specific file (byte by byte). It is usually used to read the contents of a file with raw bytes, such as images.Converting an InputStream object to StringYou can convert an InputStream Object int to a String ...

Read More

What is a Stream and what are the types of Streams and classes in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 31K+ Views

Java provides I/O Streams to read and write data where, a Stream represents an input source or an output destination which could be a file, i/o devise, other program etc.In general, a Stream will be an input stream or, an output stream.InputStream − This is used to read data from a source.OutputStream − This is used to write data to a destination.Based on the data they handle there are two types of streams −Byte Streams − These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, ...

Read More

What is the use of in flush() and close() methods of BufferedWriter class in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 2K+ Views

The BufferedWriter class of Java is used to write stream of characters to the specified destination (character-output stream). It initially stores all the characters in a buffer and pushes the contents of the buffer to the destination, making the writing of characters, arrays and Strings efficient.You can specify the required size of the buffer at the time of instantiating this class.The flush() methodWhile you are trying to write data to a Stream using the BufferedWriter object, after invoking the write() method the data will be buffered initially, nothing will be printed.The flush() method is used to push the contents of ...

Read More

Why does Java strictly specify the range and behavior of its primitive types?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 405 Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double.Java strictly specifies range and behaviors of all the primitive datatypes. Making the users choose the required datatypes based on the application thus reducing the unused occupancy of memory.For example, if you need to store an integer constant of single digit using integer would be a waste of memory instead, you can use byte type since 8 bits would be necessary to store it.ExampleFollowing Java example lists out the ranges of the primitive datatypes.public class ...

Read More

List out the default values of numeric and non-numeric primitive data types in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 581 Views

When you create instance variables in Java you need to initialize them, else the compiler will initialize on your behalf with default values which are −byte: 0short: 0int: 0long: 0float: 0.0double: 0.0boolean: falsestring: nullExampleIn the following Java program prints the default values of the numeric and non-numeric primitive variables in java.public class DefaultValues {    byte byteVariable;    short shortVariable;    int intVariable;    long longVaraible;    float floatVariable;    double doubleVariable;    boolean boolVariable;    String stringVariable;    public static void main(String args[]){       DefaultValues obj = new DefaultValues();       System.out.println("Default values of numeric variables ...

Read More

How to call the constructor of a superclass from a constructor in java?

Maruthi Krishna
Maruthi Krishna
Updated on 01-Aug-2019 6K+ Views

Whenever you inherit/extend a class, a copy of superclass’s members is created in the subclass object and thus, using the subclass object you can access the members of both classes.ExampleIn the following example we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).Now, you create an object of the subclass and call the method demo().class SuperClass{    public void demo() {       System.out.println("demo method");    } } public class SubClass extends SuperClass {    public static void main(String args[]) {       SubClass obj = new ...

Read More
Showing 491–500 of 500 articles
« Prev 1 46 47 48 49 50 Next »
Advertisements