Wednesday, May 15, 2013

file programming in java



Reading and Writing text files

When reading and writing text files:
  • it's often a good idea to use buffering (default size is 8K)
  • it's often possible to use references to abstract base classes, instead of references to specific concrete classes
  • there's always a need to pay attention to exceptions (in particular, IOException and FileNotFoundException)
The close method:
  • always needs to be called, or else resources will leak
  • is called automatically, if you use try-with-resources (JDK 7+)
  • will automatically flush the stream, if necessary
  • calling close on a "wrapper" stream will automatically call close on its underlying stream
  • closing a stream a second time has no consequence
  • When called on a Scanner, the close operation only works if the item passed to its constructor implements Closeable. Warning: if you pass a File to a Scanner, you will not be able to close it! Try using a FileReader instead.
Commonly used items:
  • Scanner - allows reading files in a compact way
  • BufferedReader - readLine
  • BufferedWriter - write + newLine
The FileReader and FileWriter classes are a bit tricky, since they implicitly use the system's default character encoding. If this default is not appropriate (for example, when reading an XML file which specifies its own encoding), the recommended alternatives are, for example :
FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader
in = new InputStreamReader(fis, "UTF-8");
FileOutputStream fos = new FileOutputStream("test.txt");
OutputStreamWriter
out = new OutputStreamWriter(fos, "UTF-8");
Scanner scanner = new Scanner (file, "UTF-8");
In JDK 7, a new package called java.nio was added. This new package fixes the defects of the older API. As usual, the more modern API is a significant improvement, and you're strongly encouraged to use it.
The examples below are separated into two sections. The first section uses JDK 7, and the second uses an older JDK.
JDK 7
When dealing dealing with files, the most important classes in JDK 7 are:
  • Paths and Path - file locations/names, but not their content.
  • Files - operations on file content.
Also of note are:
  • StandardCharsets and Charset (an older class), for encodings of text files.
  • the File.toPath method, which lets older code interact nicely with the newer java.nio API.
Example 1


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class ReadWriteTextFileJDK7 {
 
  public static void main(String... aArgs) throws IOException{
    ReadWriteTextFileJDK7 text = new ReadWriteTextFileJDK7();
   
    //treat as a small file
    List<String> lines = text.readSmallTextFile(FILE_NAME);
    log(lines);
    lines.add("This is a line added in code.");
    text.writeSmallTextFile(lines, FILE_NAME);
   
    //treat as a large file - use some buffering
    text.readLargerTextFile(FILE_NAME);
    lines = Arrays.asList("Down to the Waterline", "Water of Love");
    text.writeLargerTextFile(OUTPUT_FILE_NAME, lines);  
  }

  final static String FILE_NAME = "C:\\Temp\\input.txt";
  final static String OUTPUT_FILE_NAME = "C:\\Temp\\output.txt";
  final static Charset ENCODING = StandardCharsets.UTF_8;
 
  //For smaller files
 
  List<String> readSmallTextFile(String aFileName) throws IOException {
    Path path = Paths.get(aFileName);
    return Files.readAllLines(path, ENCODING);
  }
 
  void writeSmallTextFile(List<String> aLines, String aFileName) throws IOException {
    Path path = Paths.get(aFileName);
    Files.write(path, aLines, ENCODING);
  }

  //For larger files
 
  void readLargerTextFile(String aFileName) throws IOException {
    Path path = Paths.get(aFileName);
    try (Scanner scanner =  new Scanner(path, ENCODING.name())){
      while (scanner.hasNextLine()){
        //process each line in some way
        log(scanner.nextLine());
      }     
    }
  }
 
  void readLargerTextFileAlternate(String aFileName) throws IOException {
    Path path = Paths.get(aFileName);
    try (BufferedReader reader = Files.newBufferedReader(path, ENCODING)){
      String line = null;
      while ((line = reader.readLine()) != null) {
        //process each line in some way
        log(line);
      }     
    }
  }
 
  void writeLargerTextFile(String aFileName, List<String> aLines) throws IOException {
    Path path = Paths.get(aFileName);
    try (BufferedWriter writer = Files.newBufferedWriter(path, ENCODING)){
      for(String line : aLines){
        writer.write(line);
        writer.newLine();
      }
    }
  }

  private static void log(Object aMsg){
    System.out.println(String.valueOf(aMsg));
  }
 
}

Earlier Versions of the JDK (< JDK 7)
Example 2
Here is a fairly compact example (for JDK 1.5) of reading and writing a text file, using an explicit encoding. If you remove all references to encoding from this class, it will still work -- the system's default encoding will simply be used instead.

import java.io.*;
import java.util.Scanner;

/**
 Read and write a file using an explicit encoding.
 Removing the encoding from this code will simply cause the
 system's default encoding to be used instead. 
*/
public final class ReadWriteTextFileWithEncoding {

  /** Requires two arguments - the file name, and the encoding to use.  */
  public static void main(String... aArgs) throws IOException {
    String fileName = aArgs[0];
    String encoding = aArgs[1];
    ReadWriteTextFileWithEncoding test = new ReadWriteTextFileWithEncoding(
      fileName, encoding
    );
    test.write();
    test.read();
  }
 
  /** Constructor. */
  ReadWriteTextFileWithEncoding(String aFileName, String aEncoding){
    fEncoding = aEncoding;
    fFileName = aFileName;
  }
 
  /** Write fixed content to the given file. */
  void write() throws IOException  {
    log("Writing to file named " + fFileName + ". Encoding: " + fEncoding);
    Writer out = new OutputStreamWriter(new FileOutputStream(fFileName), fEncoding);
    try {
      out.write(FIXED_TEXT);
    }
    finally {
      out.close();
    }
  }
 
  /** Read the contents of the given file. */
  void read() throws IOException {
    log("Reading from file.");
    StringBuilder text = new StringBuilder();
    String NL = System.getProperty("line.separator");
    Scanner scanner = new Scanner(new FileInputStream(fFileName), fEncoding);
    try {
      while (scanner.hasNextLine()){
        text.append(scanner.nextLine() + NL);
      }
    }
    finally{
      scanner.close();
    }
    log("Text read in: " + text);
  }
 
  // PRIVATE
  private final String fFileName;
  private final String fEncoding;
  private final String FIXED_TEXT = "But soft! what code in yonder program breaks?";
 
  private void log(String aMessage){
    System.out.println(aMessage);
  }
}



Example 3
This example uses FileReader and FileWriter, which implicitly use the system's default encoding. To make this example compatible with JDK 1.4, just change StringBuilder to StringBuffer:

import java.io.*;

public class ReadWriteTextFile {

  /**
  * Fetch the entire contents of a text file, and return it in a String.
  * This style of implementation does not throw Exceptions to the caller.
  *
  * @param aFile is a file which already exists and can be read.
  */
  static public String getContents(File aFile) {
    //...checks on aFile are elided
    StringBuilder contents = new StringBuilder();
   
    try {
      //use buffering, reading one line at a time
      //FileReader always assumes default encoding is OK!
      BufferedReader input =  new BufferedReader(new FileReader(aFile));
      try {
        String line = null; //not declared within while loop
        /*
        * readLine is a bit quirky :
        * it returns the content of a line MINUS the newline.
        * it returns null only for the END of the stream.
        * it returns an empty String if two newlines appear in a row.
        */
        while (( line = input.readLine()) != null){
          contents.append(line);
          contents.append(System.getProperty("line.separator"));
        }
      }
      finally {
        input.close();
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }
   
    return contents.toString();
  }

  /**
  * Change the contents of text file in its entirety, overwriting any
  * existing text.
  *
  * This style of implementation throws all exceptions to the caller.
  *
  * @param aFile is an existing file which can be written to.
  * @throws IllegalArgumentException if param does not comply.
  * @throws FileNotFoundException if the file does not exist.
  * @throws IOException if problem encountered during write.
  */
  static public void setContents(File aFile, String aContents)
                                 throws FileNotFoundException, IOException {
    if (aFile == null) {
      throw new IllegalArgumentException("File should not be null.");
    }
    if (!aFile.exists()) {
      throw new FileNotFoundException ("File does not exist: " + aFile);
    }
    if (!aFile.isFile()) {
      throw new IllegalArgumentException("Should not be a directory: " + aFile);
    }
    if (!aFile.canWrite()) {
      throw new IllegalArgumentException("File cannot be written: " + aFile);
    }

    //use buffering
    Writer output = new BufferedWriter(new FileWriter(aFile));
    try {
      //FileWriter always assumes default encoding is OK!
      output.write( aContents );
    }
    finally {
      output.close();
    }
  }

  /** Simple test harness.   */
  public static void main (String... aArguments) throws IOException {
    File testFile = new File("C:\\Temp\\blah.txt");
    System.out.println("Original file contents: " + getContents(testFile));
    setContents(testFile, "The content of this file has been overwritten...");
    System.out.println("New file contents: " + getContents(testFile));
  }
}
This example demonstrates using Scanner to read a file containing lines of structured data. Each line is then parsed using a second Scanner and a simple delimiter character, used to separate each line into a name-value pair. The Scanner class is used only for reading, not for writing.

import java.io.*;
import java.util.Scanner;

public class ReadWithScanner {

  public static void main(String... aArgs) throws FileNotFoundException {
    ReadWithScanner parser = new ReadWithScanner("C:\\Temp\\test.txt");
    parser.processLineByLine();
    log("Done.");
  }
 
  /**
   Constructor.
   @param aFileName full name of an existing, readable file.
  */
  public ReadWithScanner(String aFileName){
    fFile = new File(aFileName); 
  }
 
  /** Template method that calls {@link #processLine(String)}.  */
  public final void processLineByLine() throws FileNotFoundException {
    //Note that FileReader is used, not File, since File is not Closeable
    Scanner scanner = new Scanner(new FileReader(fFile));
    try {
      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
        processLine( scanner.nextLine() );
      }
    }
    finally {
      //ensure the underlying stream is always closed
      //this only has any effect if the item passed to the Scanner
      //constructor implements Closeable (which it does in this case).
      scanner.close();
    }
  }
 
  /**
   Overridable method for processing lines in different ways.
   
   <P>This simple default implementation expects simple name-value pairs, separated by an
   '=' sign. Examples of valid input :
   <tt>height = 167cm</tt>
   <tt>mass =  65kg</tt>
   <tt>disposition =  "grumpy"</tt>
   <tt>this is the name = this is the value</tt>
  */
  protected void processLine(String aLine){
    //use a second Scanner to parse the content of each line
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter("=");
    if ( scanner.hasNext() ){
      String name = scanner.next();
      String value = scanner.next();
      log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()) );
    }
    else {
      log("Empty or invalid line. Unable to process.");
    }
    //no need to call scanner.close(), since the source is a String
  }
 
  // PRIVATE
  private final File fFile;
 
  private static void log(Object aObject){
    System.out.println(String.valueOf(aObject));
  }
 
  private String quote(String aText){
    String QUOTE = "'";
    return QUOTE + aText + QUOTE;
  }
}


Example run of this class :
Name is : 'height', and Value is : '167cm'
Name is : 'mass', and Value is : '65kg'
Name is : 'disposition', and Value is : '"grumpy"'
Name is : 'this is the name', and Value is : 'this is the value'
Done.

Wednesday, April 17, 2013

Add Numbers inside an Array : JAVA OOP



Write Java Source code: How to Add Numbers inside an Array using For Loop




Sample Output:

Enter the size of the input you want to enter: 5
Enter 5 numbers: 2 5 3 7 25
The sum of the numbers is:42

Source Code:





//java class


public class ArraySum
{
    public int sumOfArray(int[] array)
    {
        int sum = 0;
        for(int i=0; i<array.length; i++)
        {
            sum = sum + array[i];
        }

        return sum;
    }
}

//main class


import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the size of the input you want to enter: ");
        int size = input.nextInt();
        int[] numArr = new int[size];
         
        System.out.print("Enter "+ size +" numbers: ");
        for(int i=0; i<numArr.length; i++)
        {
          numArr[i]=input.nextInt();
          
        }

        ArraySum access = new ArraySum();
        System.out.print("The sum of the numbers is:" + access.sumOfArray(numArr));       

    }
}

Tuesday, April 16, 2013

Reverse String : Java OOP

Write Java Source code: Reverse String in Java Programming

Sample Output:
Enter a word: arif
The reverse word is: fira


Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Java source code on how to print a string backward using recursion
//java class
public class StringBackward
{
    public static void reverseString(String word, int size)
    {
       if(size==0)
       {
           return;
       }
       else
       {
          System.out.print(word.charAt(size-1));
          reverseString(word, size-1);
       }
    }
}
//main class
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
      Scanner input = new Scanner(System.in);
       String word;
       System.out.print("Enter a word: ");
       word = input.next();
        StringBackward access = new  StringBackward();
        System.out.print("The reverse word is: ");
        access.reverseString(word, word.length());
        System.out.println();
    }
}

Greatest Common Divisor or GCD : JAVA OOP

Write Java Source Code on Printing the Greatest Common Divisor or GCD in Recursion

Sample Output:
Enter the base number: 100
Enter the power: 45
The GCD of 2 numbers is: 5

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//java class
public class GCD
{
    public static int gcd(int num1, int num2)
    {
        if(num2 == 0)
        {
            return num1;
        }
        else
        {
            return gcd(num2, num1%num2);
        }
    }
}
//main class
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
       Scanner input = new Scanner(System.in);
      System.out.print("Enter the base number: ");
      int num1 = input.nextInt();
      System.out.print("Enter the power: ");
      int num2 = input.nextInt();
      GCD access = new GCD();
      System.out.print("The GCD of 2 numbers is: " + access.gcd(num1, num2));
    }
}

Binary Search in Recursion : JAVA OOP

Write Java Source Code: Binary Search in Recursion

Sample Output 1:
Enter the size of the array: 5
Enter 5 numbers: 2 1 3 5 4
The sorted numbers are:
1 2 3 4 5
Enter the number you want to search: 3
The search number is on the index 2
Source Code:

//Java Class
public class binarySearch //Example of Java Class
{
    //biSearch is an example of a method or a function
    public int binSearch(int[] arr, int fIndex, int lIndex,int search)
    {
int middle = (fIndex + (lIndex - fIndex) / 2);
        if(fIndex<lIndex ){
            if (search == arr[middle]){
                return middle;
            }
            else if(search < arr[middle]){
                if(search == arr[0])
                    return 0;
                return binSearch(arr, fIndex, middle, search);
            }
            else if(search > arr[middle]){
                if(search == arr[middle+1])
                    return middle + 1;
                return binSearch(arr, middle+1, lIndex, search);
            }
        }
       return -1;
    }
//this is also a class method
 public void sort(int[] arr)
{
       for(int i=0; i<arr.length; i++)
        {
            for(int j=i+1; j<arr.length; j++ )
            {
                if(arr[i] > arr[j])
                {
                    int temp = arr[j];
                    arr[j]=arr[i];
                    arr[i]= temp;
                }
            }
        }
       for(int i=0; i<arr.length; i++)
       {
           System.out.print(arr[i] + " ");
       }
}
}
//main class
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
         Scanner input = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];
        System.out.print("Enter "+ n +" numbers: ");
        int middle;
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }
        binarySearch access = new binarySearch(); //this is how to instantiate an object to access a class
        System.out.println("The sorted numbers are: ");
        access.sort(x);//this is how to access a method
        System.out.println();
         
        System.out.print("Enter the number you want to search: ");
        int value = input.nextInt();
        System.out.print("The search number is on the index ");
        System.out.print(access.binSearch(x, 0, x.length-1, value)); //how to access a class
    }
}

A Recursive Asterisk Diamond Shape : JAVA OOP

Write Java Source Code: A Recursive Asterisk Diamond Shape

Sample Output 1:
Enter a number: 3
The shape for this is:
---*--
--*-*--
-*-*-*--
--*-*--
---*--
Sample Output 2:
Enter a number: 4
The shape for this is:
----*--
---*-*--
--*-*-*--
-*-*-*-*--
--*-*-*--
---*-*--
----*--

Sample Output 3:
Enter a number: 5
The shape for this is:
-----*--
----*-*--
---*-*-*--
--*-*-*-*--
-*-*-*-*-*--
--*-*-*-*--
---*-*-*--
----*-*--
-----*--

Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//java class
public class Diamond
{
    public String Diamond_Asterisk(int num) //method1
    {
        if(num>0)
        {
            return "*-" + Diamond_Asterisk(num-1);
        }
        else
        {
            return "-";
        }
    }
     public String Diamond_Asterisk2(int num)//method2
    {
        if(num>0)
        {
            return "-*-" + Diamond_Asterisk(num-1);//access method1
        }
        else
        {
            return "-";
        }
    }
    public String Space(int num) //method3
    {
        if(num>0)
        {
            return "-" + Space(num-1);
        }
        else
        {
            return "-";
        }
    }
    public void DiamondResult(int num)//method4
    {
        for(int i=1; i<num; i++)
        {
            System.out.print(Space(num-i));//access method3
             System.out.println(Diamond_Asterisk(i));//access method1
             
        }
         for(int i=0; i<num; i++)
        {
            System.out.println(Diamond_Asterisk2(num-i));//access method2
            System.out.print(Space(i));//access method3
        }
      
    }
     
}
//main class
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = input.nextInt();
        Diamond access = new Diamond();
        System.out.println("The shape for this is: ");
        access.DiamondResult(num);
         
    }
}

How to sort numbers in Bubble Sort : JAVA OOP

Write a Java Source Code: How to sort numbers in Bubble Sort

Sample Output:
Enter the size of the array: 10
Enter 10 numbers: 100 35 45 3 7 2 1 500 200 15
The Sorted Numbers: 1 2 3 7 15 35 45 100 200 500

Java Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//java class
public class BubbleSort
{
 public void bubbleSort(int[] arr){
     for(int i=0; i<arr.length; i++){
        for(int j=1; j<arr.length; j++){
            if(arr[j]< arr[j-1] ){
                int temp = arr[j];
                arr[j] = arr[j-1];
                arr[j-1] = temp;           
            }
        }
     }
     for(int i=0; i<arr.length; i++)
     {
         System.out.print(arr[i] + " ");
     }
}
}
//main class
import java.util.Scanner;
public class Main
{
    
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];
        System.out.print("Enter "+ n +" numbers: ");
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }
         
        BubbleSort access = new  BubbleSort();
    System.out.print("The Sorted numbers: ");
        access.bubbleSort(x);
    }
}