Download Java.io.FileOutputStream.write(byte[] b) Method

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
JAVA.IO.FILEOUTPUTSTREAM.WRITE METHOD
http://www.tutorialspoint.com/java/io/fileoutputstream_write_byte.htm
Copyright © tutorialspoint.com
Description
The java.io.FileOutputStream.writebyte[]b method writes b.length bytes from the specified byte
array to this file output stream.
Declaration
Following is the declaration for java.io.FileOutputStream.writebyte[]b method:
public void write(byte[] b)
Parameters
b -- the source buffer.
Return Value
This method does not return any value.
Exception
IOException - if any I/O error occurs.
Example
The following example shows the usage of java.io.FileOutputStream.writebyte[]b method.
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = null;
FileInputStream fis = null;
byte[] b = {65,66,67,68,69};
int i=0;
char c;
try{
// create new file output stream
fos=new FileOutputStream("C://test.txt");
// writes bytes to the output stream
fos.write(b);
// flushes the content to the underlying stream
fos.flush();
// create new file input stream
fis = new FileInputStream("C://test.txt");
// read till the end of the file
while((i=fis.read())!=-1)
{
// convert integer to character
c=(char)i;
// prints
System.out.print(c);
}
}catch(Exception ex){
// if an error occurs
ex.printStackTrace();
}finally{
// closes and releases system resources from stream
if(fos!=null)
fos.close();
if(fis!=null)
fis.close();
}
}
}
Let us compile and run the above program, this will produce the following result:
ABCDE
Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js