# # # # # # # # # # # # # # # # # #
First program to understand FileInputStream class -:
import java.io.FileInputStream;
public class DemoOfFileInputStreamPartOne
{
public static void main(String[] args)
{
try
{
// create object of scanner to get input from keyboard
// open this file in read mode
FileInputStream fi = new FileInputStream
(filepath);
// start an inifinite loop
while(true)
{
// fetch a byte from file and store it
int var = fi.read();
// check for end of the data
if(var == -1)
{
break; // go out of loop
}
// convert the byte into the ASCII char and store
char ch = (char)var;
System.out.print(ch);
}
// close the file
fi.close();
}
catch(Exception e)
{
System.out.println("error "+e);
}
}
}
# # # # # # # # # # # # # # # # # #
Second program to understand FileInputStream class -:
import java.io.FileInputStream;
First program to understand FileInputStream class -:
import java.io.FileInputStream;
public class DemoOfFileInputStreamPartOne
{
public static void main(String[] args)
{
try
{
// create object of scanner to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Enter a the name of file with path to open in read mode: ");
System.out.print("Enter a the name of file with path to open in read mode: ");
String filepath = sc.nextLine();
// open this file in read mode
FileInputStream fi = new FileInputStream
(filepath);
// start an inifinite loop
while(true)
{
// fetch a byte from file and store it
int var = fi.read();
// check for end of the data
if(var == -1)
{
break; // go out of loop
}
// convert the byte into the ASCII char and store
char ch = (char)var;
System.out.print(ch);
}
// close the file
fi.close();
}
catch(Exception e)
{
System.out.println("error "+e);
}
}
}
# # # # # # # # # # # # # # # # # #
Second program to understand FileInputStream class -:
public class DemoOfFileInputStreamPartTwo
{
public static void main(String[] args)
{
try
{
// create object of scanner to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Enter a the name of file with path to open in read mode: ");
System.out.print("Enter a the name of file with path to open in read mode: ");
String filepath = sc.nextLine();
// open this file in read mode
FileInputStream fi = new FileInputStream(filepath);
// get no of bytes those can be read by the read() method
int size = fi.available();
// create an array of byte to store the data of file
byte[] array = new byte[size];
// fetch file data and store it inside this array
fi.read(array);
// convert array of byte into string
String str = new String(array);
System.out.println(str);
// close the file
fi.close();
}
catch(Exception e)
{
System.out.println("error "+e);
}
}
}
# # # # # # # # # # # # # # # # # #
First program to understand FileOutputStream class -:
import java.io.FileOutputStream;
First program to understand FileOutputStream class -:
public class DemoOfFileOutputStreamPartOne
{
public static void main(String[] args)
{
try
{
// create object of scanner to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Enter a the name of file with path to open in write mode: ");
System.out.print("Enter a the name of file with path to open in write mode: ");
String filepath1 = sc.nextLine();
System.out.print("Enter a the name of file with path to open in append mode: ");
String filepath2 = sc.nextLine();
// open a file in write mode
FileOutputStream fo1 = new FileOutputStream(filepath1,false);
// open a file in append mode
FileOutputStream fo2 = new FileOutputStream
(filepath2,true);
System.out.println("files has been created");
// close the files
fo1.close();
fo2.close();
// close the files
fo1.close();
fo2.close();
}
catch (Exception e)
{
System.out.println("error "+e);
}
}
}
# # # # # # # # # # # # # # # # # #
Second program to understand FileOutputStream class -:
import java.io.FileOutputStream;
Second program to understand FileOutputStream class -:
public class DemoOfFileOutputStreamPartTwo
{
public static void main(String[] args)
{
try
{
// create object of scanner to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Enter a the name of file with path to open in write mode: ");
System.out.print("Enter a the name of file with path to open in write mode: ");
String filepath = sc.nextLine();
System.out.print("Enter the data to be stored inside the file: ");
String filedata = sc.nextLine();
// open a file in write mode
FileOutputStream fo = new FileOutputStream
(filepath,false);
// convert string into array of byte and store
byte[] array = text.getBytes();
// fetch the data of array using for loop
for (int i = 0; i < array.length; i++)
{
// store the data of ith element of array inside file
fo.write(array[i]);
}
// close the file
fo.close();
}
catch (Exception e)
{
System.out.println("error "+e);
}
}
}
# # # # # # # # # # # # # # # # # #
Third program to understand FileOutputStream class -:
import java.io.FileOutputStream;
Third program to understand FileOutputStream class -:
public class DemoOfFileOutputStreamPartThree
{
public static void main(String[] args)
{
try
{
// create object of scanner to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Enter a the name of file with path to open in read mode: ");
// create object of scanner to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Enter a the name of file with path to open in read mode: ");
String filepath = sc.nextLine();
// open file in write mode
FileOutputStream fo = new FileOutputStream(filepath,false);
// convert string into array of byte and store
byte[] array = text.getBytes();
// store the data of file inside the array in one go (matlab ek baar mai)
fo.write(array);
// close the file
fo.close();
}
catch (Exception e)
{
System.out.println("error "+e);
}
}
}
# # # # # # # # # # # # # # # # # #
No comments:
Post a Comment