Programming Articles

Page 2436 of 2547

How to read certain number of elements from a file in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Sep-2019 853 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

How to read data from one file and print to another file in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 10-Sep-2019 3K+ 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 programs, 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

Can we use readUTF() to read a string from a .txt file in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 10-Sep-2019 510 Views

The readUTF() method of the java.io.DataOutputStream reads data that is in modified UTF-8 encoding, into a String and returns it.ExampleThe following Java program reads a UTF-8 text from a .txt file using the readUTF() method.import java.io.DataInputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; public class UTF8Example {    public static void main(String args[]) {       StringBuffer buffer = new StringBuffer();       try {          //Instantiating the FileInputStream class          FileInputStream fileIn = new FileInputStream("D:\test.txt");          //Instantiating the DataInputStream class          DataInputStream inputStream = new DataInputStream(fileIn); ...

Read More

Reading UTF8 data from a file using Java

Maruthi Krishna
Maruthi Krishna
Updated on 10-Sep-2019 4K+ Views

In general, data is stored in a computer in the form of bits (1 or, 0). There are various coding schemes available specifying the set of bytes represented by each character.Unicode (UTF) − Stands for Unicode Translation Format. It is developed by The Unicode Consortium. if you want to create documents that use characters from multiple character sets, you will be able to do so using the single Unicode character encodings. It provides 3 types of encodings.UTF-8 − It comes in 8-bit units (bytes), a character in UTF8 can be from 1 to 4 bytes long, making UTF8 variable width.UTF-16 ...

Read More

How to check if an URL is valid or not using Java?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Sep-2019 3K+ Views

The URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource(file or, directory or a reference) in the worldwide web.This class provides various constructors one of them accepts a String parameter and constructs an object of the URL class. While passing URL to this method if you used an unknown protocol or haven’t specified any protocol this method throws a MalformedURLException.Similarly, the toURI() method of this class returns an URI object of the current URL. If the current URL is not properly formatted or, syntactically incorrect according to RFC 2396 this method throws ...

Read More

Restrictions while declaring a generic (type) in Java

Maruthi Krishna
Maruthi Krishna
Updated on 06-Sep-2019 1K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Restrictions on genericsYou cannot use generics in certain ways and in certain scenarios as listed below −You cannot use primitive datatypes with generics.class Student{    T age;    Student(T age){       this.age = age; ...

Read More

Print leaf nodes in binary tree from left to right using one stack in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 22-Aug-2019 310 Views

Program should print the leaf nodes of a binary tree from left to right but the challenge involves is using of only one stackThrough push() function insert nodes of a binary tree and through pop() operation display the leaf nodes.Leaf nodes are the end nodes whose left and right pointer is NULL which means that particular node is not a parent node.ExampleInput : 12 21 32 41 59 33 70 Output : 41 59 33 70Stack is a data structure which is a LIFO structure in which top pointer will point to the last elements inserted so the leaf nodes ...

Read More

Area of Largest rectangle that can be inscribed in an Ellipse?

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 396 Views

Here we will see the area of largest rectangle that can be inscribed in an ellipse. The rectangle in ellipse will be like below −The a and b are the half of major and minor axis of the ellipse. The upper right corner of the rectangle is (x, y). So the area isNow, after making this equation as f(x) and maximizing the area, we will get the area asExample#include #include using namespace std; float area(float a, float b) { if (a < 0 || b < 0 ) //if the valuse are negative it is ...

Read More

"static const" vs "#define" vs "enum" ?

sudhir sharma
sudhir sharma
Updated on 20-Aug-2019 5K+ Views

"static const"“static const” is basically a combination of static(a storage specifier) and const(a type qualifier).The static determines the lifetime and visibility/accessibility of the variable. This means if a variable is declared as a static variable, it will remain in the memory the whole time when the program is running, while the normal or auto variables are destroyed when the function (where the variable was defined) is over.The const is a type qualifier. A type qualifier is used to express additional info about a value through type system. When a variable is initialized using the const type qualifier, it will not ...

Read More

"delete this" in C++?

sudhir sharma
sudhir sharma
Updated on 20-Aug-2019 2K+ Views

Delete is an operator that is used to Deallocate storage space of Variable.This pointer is a kind of pointer that can be accessed but only inside nonstatic member function and it points to the address of the object which has called the member function.This pointer holds the address of the current object, in simple words, you can say that this pointer points to the current object of the classWhenever we call a member function through its object, compiler secretly passes the address of calling that object as the first parameter in member function as this pointer.Generally, delete operator should not ...

Read More
Showing 24351–24360 of 25,466 articles
Advertisements