Reader & Writer Streams in Advanced Java Programming

import java.io.BufferedReader;
import java.io.FileReader;

public class Main {
  public static void main(String[] args) throws Exception{
    String srcFile = "test.txt";
    BufferedReader br = new BufferedReader(new FileReader(srcFile));
    String text = null;

    while ((text = br.readLine()) != null) {
      System.out.println(text);
    }
    br.close();
  }
}




Buffered Input & Output Streams in Advanced Java Programming

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

class BufferedInputStreamDemo
{
public static void main(String args[]) throws IOException
{
FileInputStream fin = new FileInputStream("text.txt");

BufferedInputStream bin = new BufferedInputStream(fin);
System.out.println("Number of remaining bytes:" + bin.available());

boolean b=bin.markSupported();
if (b)
bin.mark(bin.available());

bin.skip(4);
System.out.println("FileContents :");

// read characters from FileInputStream and
// write them
int ch;
while ((ch=bin.read()) != -1)
System.out.print((char)ch);

// illustrating reset() method
bin.reset();
while ((ch=bin.read()) != -1)
System.out.print((char)ch);

// close the file
fin.close();
}
}






File Input & Output Stream in Advanced Java Programming

import java.io.*;
public class ReadConsole {

   public static void main(String args[]) throws IOException {
      InputStreamReader cin = null;

      try {
         cin = new InputStreamReader(System.in);
         System.out.println("Enter characters, 'q' to quit.");
         char c;
         do {
            c = (char) cin.read();
            System.out.print(c);
         } while(c != 'q');
      }finally {
         if (cin != null) {
            cin.close();
         }
      }
   }
}




File Class in Advanced Java Programming

Home  »  Java  »  Advance Java »  Java File Handling »  File Class

The file class is used to deal with fiels and files system in Java. The File class helps determine the properties of a class such as the premissions to access the file, date and time when the file was created and directory path for the file. The File class hides the details how information is retrieved from and stored in files. Files can be used as source as well as destination to store the data for a program or the processed information obtained from the output of a program. Various constructors that can be used to create objects of the File class are:

  1. File(String directoryPath): The string parameter directory path determines the path name of the file.
  2. File(String diectoryPath, String filename): The string paramter directory path determines the path name of the file and the string parameter filename determines the name of the file.
  3. File(File dieObj, String filename): The file parameter dirObj determines the File object that specifies the directory and the string parameter filename determines the name of the file.
  4. file(URI uriObj): The URI parameter determines the URI object that describes a file.

Various examples of creating the File class objects based on the constructor are:

File file1=new File("/");

File file2=new File("/","autoexec.bat");

File file3=new File("file1","autoexec.bat");



The following program code shows how to obtain various properties of File Object.



The above example uses various methods such as getname to access the name of a file. The getAbsolutePath method determines the absolute path for the file. The exists function determines whether file exists or not. If the exists the function return true value and if the file does not exist the function returns false value and the above code will be stored in the file called FileClassTest.java, which on compilation will crate a class file called FileClassTest.class. Now compiple this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac FileClassTest.java
C:\>jdk1.4\bin>java FileClassTest



"Output of FileClassTest.class"


Download Complete Program

  -  



File Handling


Input/Output Streams I/O Exceptions
File Class File Input and Output Stream
Data Input and Output Stream Buffered Input and Output Streams
Reader and Writer Streams Random Access File



Polygon in Advanced Java Graphics Programming

After this you can comiple your java applet program as shown below:


c:\jdk1.4\bin\javac Polygon.java
c:\jdk1.4\bin\appletviewer Polygon.java


"Output of Polygon.class"




Download Complete Program

  -   Read more »




Arcs in Advance Java Graphics Programming

After this you can comiple your java applet program as shown below:


c:\jdk1.4\bin\javac Arcs.java
c:\jdk1.4\bin\appletviewer Arcs.java


"Output of Arcs.class"




Download Complete Program

  -   Read more »




Circles & Ellipses in Advance Java Graphics Programming



After this you can comiple your java applet program as shown below:


c:\jdk1.4\bin\javac Ellipses.java
c:\jdk1.4\bin\appletviewer Ellipses.java


"Output of Ellipses.class"




Download Complete Program

  -   Read more »




Rectangle in Advance Java Graphics Programming



After this you can comiple your java applet program as shown below:


c:\jdk1.4\bin\javac Rectanlge.java
c:\jdk1.4\bin\appletviewer Rectanlge.java


"Output of Rectangle.class"




Download Complete Program

  -   Read more »




Lines in Advance Java Graphics Programming


After this you can comiple your java applet program as shown below:


"Output of Lines.class"



Download Complete Program

  -   Read more »




Displaying The Image Object On The Java Applet

In applets, we can display numerical values by first converting them into strings and then using the drawstring() method of Graphics class we can do this easily by calling the valueOf() Method of String class. Let us consider an example:



This applet runs using the following HTML file:


After this you can comiple your java applet program as shown below:



"Output of DisplayImageInApplet.class"




Download Complete Program

  -     -   HTML File   -   Image





Getting Input from the User in Java Applet

Applets work in graphical environment. Therefore applets treat input as text strings. We must first create an area of the screen in which user can type and edit input items. We can do this by using the TextField class of the applet package. The values of the fields can be given even editer after the creation of input fields. Next step is to retrieve the items from the fields for display of calculations.

For any kinds of computation on the input field, we must convert it to the right form and the resuls again converted back to strings for display.

Let us consider an example


This applet runs using the following HTML file:



Note :- after comipling this applet program will not calculate the two variables a and b because for this we need event programming...


"Output of GettingInputfromtheUser.class"


Download Complete Program

  -     -   HTML File





Display Numerical Values in Applet

 In applets, we can display numerical values by first converting them into strings and then using the drawstring() method of Graphics class we can do this easily by calling the valueOf() Method of String class. Let us consider an example:





This applet runs using the following HTML file:






First Applet Program in Java



After creating FirstJavaApplet.java file now you need create FirstJavaApplet.html file as shown below:


After this you can comiple your java applet program as shown below:


"Running Applet"


"Output of FirstJavaApplet.class"



Download Complete Program

  -     -   HTML File
Read more »





Creating Package in Java

The listing below shows a package named package1 containing a single class. Class Ademo.

Ademo.java

This source file should be named as class Ademo.Java and stored in the subdirectory package1. After compiling this file, a class Ademo.class file will created should be stored in the same subdirectory.

How to compile or make the package use under commands:




after compiling java create a folder named package1 under bin dir....




Now consider the other program as follows:

Test1.java



Output



Download Complete Program

Ademo Test1
  -  
  -  
Read more »




Implementing Interface in Java



Output of "ImplementingInterface.java"


Download Complete Program

  -   Read more »





Extending Interface in Java



Output of "ExtendingInterface.java"



Download Complete Program

  -   Read more »





Java Vectors



Output of "VectorClass.java"


Download Complete Program

  -   Read more »





StringBuffer Class in Java



Output of "Str_Buffer.java"


Download Complete Program

  -   Read more »




Java String and String Methods



Output of "Str_StrMethod.java"


Download Complete Program

  -   Read more »





Two Dimensional Array in Java





Download Complete Program

  -   Read more »