Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)


Download 0.82 Mb.
Pdf ko'rish
bet56/95
Sana09.01.2022
Hajmi0.82 Mb.
#261112
1   ...   52   53   54   55   56   57   58   59   ...   95
Bog'liq
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )

The finalize( ) Method:
It  is  conceivable  to  call  a  method  that  will  be  called  just  before  an  object’s  last
annihilation.  This  method  is  referred  to  as  finalize(),  and  it  can  be  utilized  to  guarantee
that  an  item  ends  neatly.  For  instance,  you  may  utilize  finalize(  )  to  verify  that  an  open
record possessed by that object is shut. To add a finalizer to a class, you basically jus call
finalize(). The Java runtime calls that technique at whatever point it is going to reuse an
object of that class.
Inside this function, you will point out those activities that must be performed before an
object is removed. The syntax of using and implementing this function is:
protected void finalize( ) {
//Statements
}
The access modifier used for the method ensures that the method cannot be accessed by
elements  outside  the  particular  class.  This  implies  that  you  can’t  know  when  or  how  the
method executes.



File Handling
All the classes that you may require on a day to day I/O programming basis are contained
in the package java.io. The streams present in this package broadly represent output and
input  locations.  Moreover,  the  streams  supported  in  Java  include  object,  primitives  and
localized  characters.    A  stream  can  simply  be  described  as  data,  arranged  in  a  sequence.
While the inputStream can be used for inputting data from a source, the OutputStream can
be sued for outputting data to a sink. The support for I/O provided by Java is flexible and
extensive. This chapter sims to cover all the basic facets of File Handling in Java.
Byte Streams
Byte streams in Java are utilized to perform output and input of 8-bit bytes. In spite of the
fact that there are numerous classes identified with byte streams yet most utilized classes
are, Fileoutputstream and Fileinputstream. Here is an example of they can be used in real-
life programming.
import java.io.*;
public class Filecopy {
public static void main(string args[]) throws IOException {
FileInputStream inputx = invalid;
FileOutputStream outputx = invalid;
try {
inputx = new FileInputStream(“inputfile.txt”);
outputx = new FileOutputStream(“outputfile.txt”);
int charx;
while ((charx = inputx.read()) != -1) {
outputx.write(charx);
}
}
finally {
if (inputx != invalid) {


inputx.close();
}
if (outputx != invalid) {
outputx.close();
}
}
}
Presently we should have a record inputfile.txt with the accompanying content:
This is for testing purpose only.
As  an  important  step,  compile  and  execute  the  code  shown  above.  The  execution  of  the
code shall result in the creation of outputfile.txt file.

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   52   53   54   55   56   57   58   59   ...   95




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling