JAVA I/O Streams , FileInputStream and FileOutputStream

 Reading a data from a file:

import java.io.*;

class FISDemo

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

{

FileInputStream fis = new FileInputStream("g.txt");

int data ;

while ((data=fis.read()) !=-1) {

System.out.println("data:"+ (char)data);

}

fis.close();

}

}

 Writing a data from a file:

import java.io.*;

class FOSDemo{

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

{

FileOutputStream fos= new FileOutputStream("bikas.txt");

fos.write(5);

System.out.println("data is saved");

fos.close();

}

}

FILE COPY PROGRAM IN JAVA:

import java.io.*;

class FileCopy{

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

{

FileInputStream fis = new FileInputStream("abc.txt");

FileOutputStream fos = new FileOutputStream("bbc.txt");

int data;

while((data=fis.read())!=-1){

fos.write(data);

}

System.out.println("file copied");

fis.close();

fos.close();

}

}

FILE COPY PROGRAM IN JAVA(dynamic):

import java.io.*;

class FileCopyDyn{

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

{

FileInputStream fis = new FileInputStream(args[0]);

FileOutputStream fos = new FileOutputStream(args[1]);

int data;

while((data=fis.read())!=-1){

fos.write(data);

}

System.out.println("file copied");

fis.close();

fos.close();

}

}

FILE COPY PROGRAM IN JAVA using SCAN:

import java.util.*;

import java.io.*;

class FileCopyScan{

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

{

Scanner sc = new Scanner(System.in);

System.out.println("Enter source file");

String srcFile = sc.nextLine();


System.out.println("Enter Destination file");

String destFile = sc.nextLine();


FileInputStream fis= new FileInputStream(srcFile);

FileOutputStream fos = new FileOutputStream(destFile);

int data;

while((data=fis.read())!=-1)

{

fos.write(data);

}

System.out.println("File copied");

fis.close();

fos.close();

}

}


Comments

Popular posts from this blog

Java Program to find average of Three numbers

Java Swing Complete examples step by step(make a project with swing)

A simple method using string to check whether the input password is correct or not