ByteArrayInputStream:
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
public class ByteArrayInputStreamDemo
{
public static void main(String args[])throws IOException
{
int size;
String s1="";
int c;
InputStream f1=new FileInputStream("file1.txt"); //reads text from existing file
System.out.println("Total size:" + (size=f1.available()));
for(int i=0;i<size;i++)
{
s1=s1+(char)f1.read();
}
System.out.println("S1:" + s1);
byte b[]=s1.getBytes();
ByteArrayInputStream input1=new ByteArrayInputStream(b);
ByteArrayInputStream input2=new ByteArrayInputStream(b,0,5);
//printing the input1 values 2 times(one in lowercase and another in uppercase)
for(int i=0;i<2;i++)
{
for(int j=0;j<b.length;j++)
while((c=input1.read())!=-1)
if(i==0)
System.out.print((char)c);
else
System.out.print(Character.toUpperCase((char)c));
input1.reset();
}
//printing the input2 values
for(int k=0;k<b.length;k++)
while((c=input2.read())!=-1)
System.out.print((char)c);
f1.close();
}
}
output:
Total size:34
S1:Hello Welcome to File Handling Bye
Hello Welcome to File Handling ByeHELLO WELCOME TO FILE HANDLING BYEHello
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
public class ByteArrayInputStreamDemo
{
public static void main(String args[])throws IOException
{
int size;
String s1="";
int c;
InputStream f1=new FileInputStream("file1.txt"); //reads text from existing file
System.out.println("Total size:" + (size=f1.available()));
for(int i=0;i<size;i++)
{
s1=s1+(char)f1.read();
}
System.out.println("S1:" + s1);
byte b[]=s1.getBytes();
ByteArrayInputStream input1=new ByteArrayInputStream(b);
ByteArrayInputStream input2=new ByteArrayInputStream(b,0,5);
//printing the input1 values 2 times(one in lowercase and another in uppercase)
for(int i=0;i<2;i++)
{
for(int j=0;j<b.length;j++)
while((c=input1.read())!=-1)
if(i==0)
System.out.print((char)c);
else
System.out.print(Character.toUpperCase((char)c));
input1.reset();
}
//printing the input2 values
for(int k=0;k<b.length;k++)
while((c=input2.read())!=-1)
System.out.print((char)c);
f1.close();
}
}
output:
Total size:34
S1:Hello Welcome to File Handling Bye
Hello Welcome to File Handling ByeHELLO WELCOME TO FILE HANDLING BYEHello
No comments:
Post a Comment