Skip to main content

Posts

Showing posts from March, 2015

HOW TO INTERFACE WITH FILES IN JAVA

Simple example to use string in different ways. Syntax : String variable_name = "Hello World!" Example : import java.io.*;   public class CopyFile {    public static void main(String args[]) throws IOException    {       FileInputStream in = null;       FileOutputStream out = null;         try {          in = new FileInputStream("input.txt");          out = new FileOutputStream("output.txt");                   int c;          while ((c = in.read()) != -1) {             out.write(c);          }       }finally {          if (in != null) {             in.close();          }          if (out != null) {             out.close();          }       }    } } Description : Step 1: Starting of class Step 2: Start of main function. Step 3: The input or output type of exceptions that occur in main function are thrown away. Step 4 : There are many classes for byte stream but FileInputStream and FileOutputStream are mo

HOW TO CONCATENATE (join) TWO STRINGS

Simple example to use string in different ways. Syntax : string1 .concat( string2 ) Example : public class StringConcatDemo { public static void main(String args[]) {    String s = "Strings are immutable";    s = s.concat(" all the time");    System.out.println(s);    } } Description : Step 1: Starting of class Step 2: Start of main function. Step 3: Define a string "s" Step 4: Concatenating data of string 's' with new data "all the time". Step 5: Display content of variable "s" to console. Output: Strings are immutable all the time

HOW TO FIND LENGTH OF A STRING IN JAVA

Simple example to use string in different ways. Syntax : Some_string_variable_name .length() Example : public class StringDemo {      public static void main(String args[]) {       String palindrome = "Dot saw I was Tod";       int len = palindrome.length();       System.out.println( "String Length is : " + len );    } }   Description : Step 1: Starting of class Step 2: Start of main function. Step 3: Define a string "palindrome" Step 4: Defining and initializing a variable "len". Here the length(number of characters) of the string "palindrome" is copied to variable "len" which is of integer type. Step 5: Display content of variable "len" to console. Output: String Length is : 17

HOW TO CREATE A STRING IN JAVA

Simple example to use string in different ways. Syntax : String variable_name = "Hello World!" Example : public class String_Demo{      public static void main(String args[]){       char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};       String helloString = new String(helloArray);        System.out.println( helloString );    } } Description : Step 1: Starting of class Step 2: Start of main function. Step 3: Define a character array Step 4: Defining and initializing "helloString". Here value of the variable "Hello array" is copied to the String variable "helloString". Step 5: Display content of variable "helloString" to console.