0% found this document useful (0 votes)
13 views

Buffer-File Writer-Output

The document contains code examples demonstrating how to write data to output streams in Java using FileOutputStream, BufferedOutputStream, BufferedWriter, and FileWriter classes. It shows how to write strings and byte arrays to files and console using these classes.

Uploaded by

Dhimas N
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Buffer-File Writer-Output

The document contains code examples demonstrating how to write data to output streams in Java using FileOutputStream, BufferedOutputStream, BufferedWriter, and FileWriter classes. It shows how to write strings and byte arrays to files and console using these classes.

Uploaded by

Dhimas N
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.io.

*;
public class FileOutputStreamDemo
{
public static void main(String[] args) throws IOException
{
boolean bool;
long pos;
String s = "This is a FileOutputStream Program";
byte buf[] = s.getBytes();
try (FileOutputStream fos = new FileOutputStream("D:\\Files\\File.txt
"))
{
for (int i = 0; i < buf.length; i++)
{
fos.write(buf[i]);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}

/////////////////////

import java.io.BufferedOutputStream;
import java.io.IOException;

public class BufferedOutputStream


{
public static void main(String[] args)
{
try (BufferedOutputStream b = new BufferedOutputStream(System.out))
{
String s = "This is a BufferedOutputStream Demo Program";
byte buf[] = s.getBytes();

b.write(buf);
b.flush();
}
catch (IOException e)
{
System.out.println(e);
}
}
}

///////////////////
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class BufferedWriterDemo


{
public static void main(String args[])
{
try (BufferedWriter b = new BufferedWriter(new
OutputStreamWriter(System.out)))
{
String fruit[] = {"Apple", "Banana", "Grapes"};
b.write("Different types of fruit are:" + "\n");
for (int i = 0; i < 3; i++)
{
b.write(fruit[i] + "\n");
b.flush();
}
}
catch (IOException e)
{
System.out.println(e);
}
}
}

////////

import java.io.FileWriter;
import java.io.IOException;

class FileWriterDemo
{
public static void main(String args[])
{
try (FileWriter f = new FileWriter("D:\\Files\\file.txt "))
{
String source = "This is FileWriter Program";
char buffer[] = new char[source.length()];
source.getChars(0, source.length(), buffer, 0);
f.write(buffer);
}

catch (IOException e)
{
System.out.println(e);
}
}
}

////

You might also like