Reader & Writer Streams in Advanced Java Programming
Buffered Input & Output Streams in Advanced Java Programming
File Input & Output Stream in Advanced Java Programming
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:
- File(String directoryPath): The string parameter directory path determines the path name of the file.
- 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.
- 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.
- 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
|
||
|
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
Circles & Ellipses in Advance Java Graphics Programming
Lines in Advance Java Graphics Programming
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:
|
| ||
|
Download Complete Program
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.
This applet runs using the following 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:
First Applet Program in Java
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 » | |


























