3:43 pm - Sunday June 25, 2017

FileOutputStream example program for write(int b) () in java

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
/**
 *@author: candidjava
 *@description: FileOutputStream example program for write(int b)() in java
 **/

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo1 {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = null;
		FileInputStream fis = null;
		byte b = 66;
		int i = 0;
		char c;
		try {
			// create new file output stream
			fos = new FileOutputStream("C://test.txt");
			// writes byte 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();
		}
	}
}

Output

B

BACK

Filed in: Z-A All Java Codes

Comments are closed.