Powered by Blogger.
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

What is Java?

Java is a computer programming language that is used to build softwares. It is also an application, development, and deployment environment. Java is object oriented programming language to help us visualize the program in real-life terms. The syntax of Java is similar to C and C++ based language. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture.

Brief History of Java

In 1990, Sun Microsystems began a research project named Green to develop the C and C++ based language. James Gosling, a network software designer was assigned to this project. Gosling’s solution to the problems of C++ was a new language called Oak after an oak tree outside his window at Sun. In May 1995, Sun Microsystems formally announced Java (Oak was renamed Java). Finally, in 1996, Java arrived in the market as a programming language.

With the advent of Java 2 (released initially as J2SE 1.2 in December 1998), new versions had multiple configurations built for different types of platforms. For example, J2EE targeted enterprise applications and the greatly stripped-down version J2ME for mobile applications (Mobile Java). J2SE designated the Standard Edition. In 2006, for marketing purposes, Sun renamed new J2 versions as Java EE, Java ME, and Java SE, respectively.

On November 13, 2006, Sun released much of Java as free and open source software, (FOSS), under the terms of the GNU General Public License (GPL). On May 8, 2007, Sun finished the process, making all of Java’s core code available under free software/open-source distribution terms.


Table: Major Release Versions of Java.

Version
Date
JDK 1.0
January 23, 1996
JDK 1.1
February 19, 1997
J2SE 1.2
December 8, 1998
J2SE 1.3
May 8, 2000
J2SE 1.4
February 6, 2002
J2SE 5.0
September 30, 2004
Java SE 6
December 11, 2006
Java SE 7
July 28, 2011

Java: How to Read a Java String from the contents of a file

I have been using this idiom for some time now. And it seems to be the most wide-spread, at least in the sites I've visited.

Does anyone have a better/different way to Read a Java String from the contents of a file?

private String readFile( String file ) throws IOException {
    BufferedReader reader = new BufferedReader( new FileReader (file));
    String line = null;
    StringBuilder stringBuilder = new StringBuilder();
    String ls = System.getProperty("line.separator");

    while( ( line = reader.readLine() ) != null ) {
        stringBuilder.append( line );
        stringBuilder.append( ls );
    }

    return stringBuilder.toString();
}

Read String from a File Example:-

 import java.io.*;

 public class ReadFile{
   public static void main(String[]args){
      File file = new File(”/home/Parvez/MyJavaBook”, ”MyText.txt”);
      try{
         BufferedReader in = new BufferedReader(new FileReader(file));
         String str = in.readLine();
         while(str != null){
         System.out.println(”Read: ” +str);
         str = in.readLine();
         }
         in.close();
      }catch(FileNotFoundException e1){
         System.out.println(”File not found”);
      }catch(IOException e2){
         System.out.println(”Input/output problem”);
      }
   }
 }

Generation of Source Code:--

/*
Read File in String Using Java Buffered Input Stream Example. This example shows how to read a file content into a Sting object using available and read methods of Java Buffered Input Stream.
*/
    
    import java.io.*;
    
    public class ReadFileInToString {
    
            public static void main(String[] args) {
                  
                    //create file object
                    File file = new File("C://FileIO//ReadFile.txt");
                    BufferedInputStream bin = null;
                  
                    try
                    {
                            //create FileInputStream object
                            FileInputStream fin = new FileInputStream(file);
                          
                            //create object of BufferedInputStream
                            bin = new BufferedInputStream(fin);
                          
                            //create a byte array
                            byte[] contents = new byte[1024];
                          
                            int bytesRead=0;
                            String strFileContents;
                          
                            while( (bytesRead = bin.read(contents)) != -1){
                                  
                                    strFileContents = new String(contents, 0, bytesRead);
                                    System.out.print(strFileContents);
                            }
                          
                    }
                    catch(FileNotFoundException e)
                    {
                            System.out.println("File not found" + e);
                    }
                    catch(IOException ioe)
                    {
                            System.out.println("Exception while reading the file " + ioe);
                    }
                    finally
                    {
                            //close the BufferedInputStream using close method
                            try{
                                    if(bin != null)
                                            bin.close();
                            }catch(IOException ioe)
                            {
                                    System.out.println("Error while closing the stream :"
    + ioe);
                            }
                          
                    }
            }
          
    }
    
    
/*
Output would be This file is for demonstration of how to read a file into String using Java Buffered Input Stream.
*/

Java : Definition of Overloading Constructors and Program Solution in Java

Definition of Overloading Constructors:-

We can declare several constructors with different arguments in a class, which is overloading constructors.

Constructor Overloading in Java:-

Here, you will learn more about Constructor and how constructors are overloaded in Java. This section provides you a brief introduction about the Constructor that are overloaded in the given program with complete code absolutely in running state i.e. provided for best illustration about the constructor overloading in Java.

Constructors are used to assign initial values to instance variables of the class. A default constructor with no arguments will be called automatically by the Java Virtual Machine (JVM). Constructor is always called by new operator. Constructor are declared just like as we declare methods, except that the constructor don't have any return type. Constructor can be overloaded provided they should have different arguments because JVM differentiates constructors on the basis of arguments passed in the constructor.

Whenever we assign the name of the method same as  class name. Remember this method should not have any return type. This is called as constructor overloading.

We have made one program on a constructor overloading, after going through it the concept of constructor overloading will get more clear. In the example below we have made three overloaded constructors each having different arguments  types so that the JVM can differentiates between the various constructors.

Overloading Constructor Program:-

 public class Light{
    int a, b, c;

    public Light(){
       a=2;
       b=4;
       c=6;
    }

    public Light(int a){
       this.a=a;
       b=4;
       c=6;
    }

    public Light(int a, int b){
       this.a=a;
       this.b=b;
       c=6;
    }

    public Light(int a, int b, int c){
       this.a=a;
       this.b=b;
       this.c=c;
    }
  }