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


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

Perusing and Writing Files


As  mentioned  previously,  a  stream  can  be  defined  as  a  sequence  of  information.  The
Inputstream  is  utilized  to  peruse  information  from  a  source  and  the  Outputstream  is
utilized for outputting information to a terminus.
Here  is  a  chain  of  importance  of  classes  to  manage  Input  and  Output  streams.  The  two
essential streams are Fileinputstream and Fileoutputstream, which would be talked about
in the following section:
Fileinputstream:
This stream is utilized for perusing information from the documents. Objects can be made
utilizing the keyword new and there are a few sorts of constructors accessible. Inputstream
can be used for reading files in the following manner:
Inputstream myfx = new Fileinputstream(“c:/java/hi”);
The constructor takes a record item to make a data stream object to peruse the document.
Initially, we make a record item utilizing File() technique in the following manner:
File myfx = new File(“c:/java/hi”);
Inputstream myfx = new Fileinputstream(myfx);
When  you  have  the  object  of  Inputstream  under  control,  there  is  a  rundown  of  assistant
methods,  which  can  be  utilized  to  peruse  to  stream  or  to  do  different  operations  on  the
stream.
protected void finalize() throws IOException {}
This  system  cleans  up  any  association  with  the  file  and  guarantees  that  the  local
method for this output stream for the file is called. Besides this, this method is also
capable of throwing an exception.
public void close() throws IOException {}
This  system  shuts  the  output  stream  of  the  file  and  discharges  any  framework
assets connected with the the same. It is also capable of throwing an exception.
public int available() throws IOException{}
This  function  returns  an  int,  indicating  the  number  of  bytes  that  the  input  stream
can still read.
public int read(int r)throws IOException{}
The read method is used for reading content from the InputStream and returns the


next byte of data in int data type. However, upon reaching the end of file, it returns
-1.
public int read(byte[] r) throws IOException{}
This read method is similar in operation to the read method described above with
the exception that it reads data length of r in the given array. The function returns
the number of bytes read and -1 upon reaching the end of file.
Other input streams are also available for use. Some of these include:
DataInputStream
ByteArrayInputStream

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   55   56   57   58   59   60   61   62   ...   95




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