0% found this document useful (0 votes)
112 views8 pages

Java File I/O Operations and Examples

The document contains 9 Java programs that demonstrate various file input/output operations in Java like reading/writing to files, copying files, using buffered readers/writers. Program 1 reads data from a file into a byte array and prints it. Program 2 writes bytes to a file and reads them back printing the characters. The other programs demonstrate additional file I/O operations like writing strings to files, copying files, reading/writing using FileReader/FileWriter and BufferedReader.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views8 pages

Java File I/O Operations and Examples

The document contains 9 Java programs that demonstrate various file input/output operations in Java like reading/writing to files, copying files, using buffered readers/writers. Program 1 reads data from a file into a byte array and prints it. Program 2 writes bytes to a file and reads them back printing the characters. The other programs demonstrate additional file I/O operations like writing strings to files, copying files, reading/writing using FileReader/FileWriter and BufferedReader.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Program#1

import [Link];
import [Link];

class Main {
public static void main(String args[]) {

byte[] array = new byte[100];

try {
FileInputStream input = new FileInputStream("[Link]");

[Link]("Available bytes in the file: " +


[Link]());

// Read byte from the input stream


[Link](array);
[Link]("Data read from the file: ");

// Convert byte array into string


String data = new String(array);
[Link](data);

// Close the input stream


[Link]();
} catch (Exception e) {
[Link]();
}
}
}
Program#2
import [Link].*;
public class fileStreamTest{
public static void main(String args[]){
try{
byte b [] = {11,21,3,40,5};
FileOutputStream os = new FileOutputStream("[Link]");
for(int x=0; x < [Link] ; x++){
[Link]( b [x] ); // writes the bytes
}
[Link]();
InputStream is = new FileInputStream("[Link]");
int size = [Link]();
for(int i=0; i< size; i++){
[Link]((char)[Link]() + " ");
}
[Link]();
}catch(IOException e){
[Link]("Exception");
}
}
}
Program #3
import [Link];
import [Link];

public class Main {

public static void main(String args[]) {


String data = "This is a line of text inside the file.";

try {
OutputStream out = new FileOutputStream("[Link]");

// Converts the string into bytes


byte[] dataBytes = [Link]();

// Writes data to the output stream


[Link](dataBytes);
[Link]("Data is written to the file.");

// Closes the output stream


[Link]();
}

catch (Exception e) {
[Link]();
}
}
}
Program # 4
import [Link].*;
public class CopyFile {

public static void main(String args[]) throws IOException {


FileInputStream in = null;
FileOutputStream out = null;

try {
in = new FileInputStream("[Link]");
out = new FileOutputStream("[Link]");

int c;
while ((c = [Link]()) != -1) {
[Link](c);
}
}finally {
if (in != null) {
[Link]();
}
if (out != null) {
[Link]();
}
}
}
}
Program # 5

import [Link];
import [Link];

public class abc {

public static void main(String[] args) {

// Creating file object and specifying path


File file = new File("[Link]");

try {
FileInputStream input= new FileInputStream(file);
int character;
while ([Link]()!=0) {
character = [Link]();
[Link]((char)character);
}

[Link]();
}

catch (Exception e) {

[Link]();
}
}
}

Program#6
import [Link];
import [Link];
class CreateFile
{
public static void main(String[] args) throws IOException
{
String str = "File Handling in Java using "+
FileWriter fw=new FileWriter("[Link]");
for (int i = 0; i < [Link](); i++)
[Link]([Link](i));
[Link]("Writing successful");
[Link]();
}
}
Program #7
import [Link];
import [Link];
import [Link];
class ReadFile
{
public static void main(String[] args) throws IOException
{
int ch;

FileReader fr=null;
try
{
fr = new FileReader("[Link]");
}
catch (FileNotFoundException fe)
{
[Link]("File not found");
}

while ((ch=[Link]())!=-1)
[Link]((char)ch);

[Link]();
}
}
Program#8
import [Link].*;
public class CopyFile {

public static void main(String args[]) throws IOException {


FileReader in = null;
FileWriter out = null;

try {
in = new FileReader("[Link]");
out = new FileWriter("[Link]");

int c;
while ((c = [Link]()) != -1) {
[Link](c);
}
}finally {
if (in != null) {
[Link]();
}
if (out != null) {
[Link]();
}
}
}
}

Program#9

import [Link].*;
public class BufferedReaderExample{
public static void main(String args[])throws Exception{
InputStreamReader r=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(r);
[Link]("Enter your name");
String name=[Link]();
[Link]("Welcome "+name);
}
}
import [Link].*;
public class GFG {
public static void main(String[] args)
{

FileReader fileReader = new FileReader("c:/[Link]");


BufferedReader buffReader = new BufferedReader(fileReader);
while ([Link]()) {
[Link](
[Link]());
}
}
}

You might also like