String and I/O
String and I/O
String and I/O
Test
/**
* @param args the command line arguments
*/
public static void main(String[] args){
// TODO code application logic here
fos.write(b1);
fos.close();
FileInputStream fis=new FileInputStream("C:\\Users\\richa\\Desktop\\BIT\\Course Related\\SP-2024\\OOPDP -
Lab\\source.txt");
int x;
while((x=fis.read())!=-1)
{ catch(FileNotFoundException e)
System.out.println((char)x); {
} System.out.println(e);
System.out.println("Second file starts here"); }
fis=new FileInputStream("C:\\Users\\richa\\Desktop\\BIT\\Course Related\\SP-2024\\OOPDPcatch(IOException
- Lab\\ e)
source1.txt"); {
byte b2[]=new byte[fis.available()]; System.out.println(e);
fis.read(b2); }
}
String str2=new String(b2);
System.out.println(str2);
}
}
Read the file from a file letter by letter
import java.io.FileInputStream;
public class Test
{
public static void main(String[] args) throws Exception
{
try (FileInputStream fis = new FileInputStream("C:\\Users\\richa\\Documents\\NetBeansProjects\\Test\\Test.txt"))
{
int x;
while((x=fis.read())!=-1)
System.out.print((char)x);
}
}
}
Using FileReader
import java.io.FileInputStream;
public class Test
{
public static void main(String[] args) throws Exception
{
try (FileReader fis = new FileReader("C:\\Users\\richa\\Documents\\NetBeansProjects\\Test\\Test.txt"))
{
int x;
while((x=fis.read())!=-1)
System.out.print((char)x);
}
}
}
Using FileWriter
import java.io.FileWriter;
import java.io.IOException;
public class Test
{
public static void main(String[] args) throws Exception
{
// TODO code application logic here
try (FileWriter fos = new FileWriter("C:\\Users\\richa\\Documents\\NetBeansProjects\\Test\\Test.txt"))
{
String str="Learn Java Programming using File Writer";
fos.write(str);
}
}
}
Practice Problem
• Consider there are two files named source.txt and destination.txt
read the file and write them into another file
• Using FileInputStream open the file named source.txt and
FileOutputStream opening a file named destination.txt. When we are
using FileInputStream file must be in the mentioned path otherwise it
will throw exception FileNotFound.
• Using loop we are reading a file using FileInputStream and read and
store them until it will reach to the end of file
Copying content of one file into another
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
public class Test {
public static void main(String[] args) throws Exception{
FileInputStream fis=new FileInputStream("C:\\Users\\richa\\Documents\\NetBeansProjects\\Test\\source.txt");
FileOutputStream fos=new FileOutputStream("C:\\Users\\richa\\Documents\\NetBeansProjects\\Test\\
dest.txt");
int b;
while((b=fis.read())!=-1)
{
fos.write(b);
}
fis.close();
fos.close();
}
}
Practice Problem2: Combining the two files
into one
• sequenceInputStream will help to do the needful
• Sequentially read the file from source files and write it down in the
destination
Combining two files into another file
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.SequenceInputStream;
public class Test {
public static void main(String[] args) throws Exception{
FileInputStream fis=new FileInputStream("C:\\Users\\richa\\Documents\\NetBeansProjects\\Test\\source1.txt");
FileInputStream fis2=new FileInputStream("C:\\Users\\richa\\Documents\\NetBeansProjects\\Test\\source2.txt");
FileOutputStream fos=new FileOutputStream("C:\\Users\\richa\\Documents\\NetBeansProjects\\Test\\
destination1.txt");
SequenceInputStream s=new SequenceInputStream(fis,fis2);
int b;
while((b=s.read())!=-1)
{
fos.write(b);
}
s.close();
fis.close();
fis2.close();
fos.close() }
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.*;
public class File {
public static void main(String[] args){
// TODO code application logic here
try{
FileInputStream fis=new FileInputStream("C:\\Users\\richa\\Desktop\\BIT\\Course Related\\SP-2024\\OOPDP - Lab\\
source.txt");
FileInputStream fis2=new FileInputStream("C:\\Users\\richa\\Desktop\\BIT\\Course Related\\SP-2024\\OOPDP -
Lab\\source1.txt");
FileOutputStream fos=new FileOutputStream("C:\\Users\\richa\\Desktop\\BIT\\Course Related\\SP-2024\\OOPDP -
Lab\\destination.txt");
SequenceInputStream s=new SequenceInputStream(fis, fis2);
catch(FileNotFoundException e)
{
System.out.println(e);
}
int b;
catch(IOException e)
while((b=s.read())!=-1) {
{ System.out.println(e);
fos.write(b); }
} }
s.close();
}
fis.close();
fis2=new FileInputStream("C:\\Users\\richa\\Desktop\\BIT\\Course Related\\SP-2024\\OOPDP - Lab\\
destination.txt");
byte b1[]=new byte[fis2.available()];
fis2.read(b1);
String str=new String(b1);
System.out.println(str);
}
ByteStreams and CharArrayReader
• Here source of data is not a file, it is an array
• ByteArrayInputStream will help to read the data from array of bytes.
The array may have integer or character
• ByteArrayOutputStream will help to modify the array and to write the
content in the array.
• This array will be treated like a stream so we will use it using
ByteArrayInputStream
Reading one byte at a time
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception{
byte b[]={'a','b','c','d'};
ByteArrayInputStream bis=new ByteArrayInputStream(b);
//to read the data from the stream
int x;
while((x=bis.read())!=-1)
{
System.out.println((char)x);
}
bis.close();
}
}
Reading all bytes at once
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception
{
byte b[]={'a','b','c','d'};
ByteArrayInputStream bis=new ByteArrayInputStream(b);
//to read the data from the stream
//creating a string and pushing all byte at once
String str=new String(bis.readAllBytes());
System.out.println(str);
bis.close();
}
}
To check whether it support mark and reset
• Instead of printing the string it will print true or false depending on
whether it support mark or not.
• Since its an array where we are using mark so it can come back to the
previous pointer whereas in file the pointer always moves forward so
we can not use mark with files.
byteArrayOutputStream
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception{
ByteArrayOutputStream bos=new ByteArrayOutputStream(20);
bos.write('a');
bos.write('b');
bos.write('c');
bos.write('d');
//the alphabets are stored inside the array of bytes
//in order to see those letter the method bos.toByteArray
byte b[]=bos.toByteArray();//from the stream you are getting bytes of array and store them in array of bytes
for(byte x:b)
System.out.println(x);
//since we are printing byte value so it will give ASCII
bos.close();
}
Typecasting the bytes into character will print
the character of array
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception{
ByteArrayOutputStream bos=new ByteArrayOutputStream(20);
bos.write('a');
bos.write('b');
bos.write('c');
bos.write('d');
//the alphabets are stored inside the array of bytes
//in order to see those letter the method bos.toByteArray
byte b[]=bos.toByteArray();//from the stream you are getting bytes of array and store them in array of bytes
for(byte x:b)
System.out.println((char)x);
//since we are printing byte value so it will give ASCII
bos.close();
}
}
charArrayReader Example
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception{
char c[]={'a','b','c','d','e'};
CharArrayReader c1=new CharArrayReader(c);
int x;
while((x=c1.read())!=-1)
System.out.print((char)x);
c1.close();
}
}
BufferedReader and Write
• Over fileInputStream we will attach bufferedInputStream.
• Buffer is a temporary memory area which will hold the data so that
we can utilize the data smoothly when there is speed mismatch
between the input and output
• The data will come inside the fileInputStream
• For writing the data into the file we require outputStream and over
this we will attach BufferedOutputStream.