JAVA
Streams and Files – I & II
Fill in
the blanks
1.Java implements
streams within class hierarchies defined in the java.io package.
2.getParent()
method returns the name of the parent directory.
3.Write a method of
Creating Directory mkdir()
4.Write a function for
allows a buffer in memory inputstream ByteArrayInputStream
5.Write a function for
reading information from a file FileInputStream
6.Write a function for
converting a string into an InputStream StringBufferInputStream
7.FileStream consist of
FileInputStream and FileOutputStream
8.CharArrayWriter
is an implementation of an output stream that uses an array as the destination
Answer the Following Questions
(4
marks)
1.Write a program for FileInputStream using try …
catch block.
import java.io.*;
public class ReadBytes {
public static
void main(String args[]){
try{
FileInputStream fin=new
FileInputStream("E:/ja/city.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
2.Write a program for File using getName(),getParent(),exists(),isFile(),isAbsolute() function.
import java.io.File;
class FileDemo{
static
void p(String s){
System.out.println(s);
}
public static void main(String args[]){
File f1=new File("C:/Program
Files/Java/jdk1.8.0_181/COPYRIGHT");
p("File Name:"+f1.getName());
p("Path:"+f1.getPath());
p("Abs
Path:"+f1.getAbsolutePath());
p("Parent:"+f1.getParent());
p(f1.exists()?"exists":"does
not exist");
p(f1.canWrite()?"isWriteable":
"is not readable ");
p(f1.canRead()?"isreadable":"is
not readable");
p("is"+(f1.isDirectory()?"":"not"+"a
directory"));
p(f1.isFile()?"is normal
file":"might be a named pipe");
p(f1.isAbsolute()?"is
absolute":"is not absolute");
p("File last
modified:"+f1.lastModified());
p("File
size:"+f1.length()+"Bytes");
}
}
Answer the Following
Questions
(10
marks)
1.Write
a program to accept a specific number of character as input and Convert them
into Upper case Character.
File name: ByteArray.java
import java.io.*;
class ByteArray
{
public static void main (String
args[])throws Exception
{
ByteArrayOutputStream f=new
ByteArrayOutputStream(12);
System.out.println("Enter 10
characters and press the enter key");
System.out.println("These will be
converted to uppercase and displayed");
while(f.size()!=10)
{
f.write(System.in.read());
}
System.out.println("Accepted
characters into an array");
byte b[]= f.toByteArray();
System.out.println("Displaying
characters in the array");
for(int l= 0;l<b.length;l++)
{
System.out.println((char)b[l]);
}
ByteArrayInputStream inp=new
ByteArrayInputStream(b);
int c;
System.out.println("Converted
to uppercase characters");
for (int i=0;i<1;i++)
{
while ((c = inp.read())!=-1)
{
System.out.println(Character.toUpperCase((char)c));
}
System.out.println();
inp.reset();
}
}
}
No comments:
Post a Comment