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


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

FileOutputStream:
Fileoutputstream is utilized to make a file and write text into it. The stream would create a
file, in the event that it doesn’t as of now exist, before opening it for outputting. Here are
two constructors which can be utilized to make a Fileoutputstream object.
Method 1:
OutputStream myfx = new FileOutputStream(“c:/java/hi”)
Method 2:
File myfx = new File(“c:/java/hi”);
OutputStream myfx = new FileOutputStream(myfx);
When you have OutputStream object under control, there is a rundown of aide methods,
which  can  be  utilized  to  keep  in  touch  with  stream  or  to  do  different  operations  on  the
stream.
public void write(int w) throws IOException {}
This method composes the tagged byte to the output stream.
protected void finalize() throws IOException {}
This  strategy  cleans  up  any  associations  with  the  record.  Besides  this,  it  also
guarantees  that  the  local  method  for  this  output  stream  for  file  is  called.  This
method is capable of throwing an exception.
public void close() throws IOException {}
This  method  shuts  the  output  stream  of  the  file.  Moreover,  it  discharges  any


framework  assets  connected  with  the  document.  This  method  also  throws  an
IOException.
public void write(byte[] w)
This  method  composes  w.length  bytes  from  the  specified  byte  exhibit  to  the
Outputstream.
There are other imperative output streams accessible, which are as follows:
ByteArrayOutputStream
DataOutputStream
Sample Implementations:
import java.io.*;
public class Mytestfile{
public static void main(string args[]){
try{
byte bytewrite [] = {45,64,22,49,1};
OutputStream myos = new FileOutputStream(“mytest.txt”);
for(int i=0; i < bytewrite.length ; i++){
myos.write( bytewrite[x] );
}
myos.close();
InputStream myis = new FileInputStream(“mytest.txt”);
int sizex = myis.available();
for(int z=0; z< sizex; z++){
System.out.print((char)myis.read() + “ “);
}
myis.close();
}catch(IOException e){
System.out.print(“Exception Caught!”);


}
}
The  above  code  would  make  a  file  mytest.txt  and  would  compose  given  numbers  in
parallel organization. Same would be outputted to the stdout screen.

Download 0.82 Mb.

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




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