Professional Documents
Culture Documents
OOP Lab Session 7
OOP Lab Session 7
OOP Lab Session 7
Objective
Pre-lab Exercise
try{
File f= new File(“a.txt”);
}catch(Exception e){
}catch(IOException io){
}
A. True C. False
B. Compilation error D. None
3. Which of these packages contain classes and interfaces used for input & output
operations of a program?
A. java.util C. java.lang
B. java.io D. All of the mentioned
1|Page
4. Which of these is method for testing whether the specified element is a file or a
directory?
A. IsFile() B. isFile() C. Isfile() D. isfile()
5. Which of this class is used to read and write bytes in a file?
A. FileReader C. FileWriter
B. FileInputStream D. All of the mentioned
6. Which of these exception is thrown in cases when the file specified for writing is not
found?
A. IOException C. FileException
B. FileNotFoundException D. FileInputException
7. What will be output for the following code?
import java.io.*;
class files
{
public static void main(String args[])
{
File obj = new File("\\java\\system");
System.out.print(obj.getName());
}
}
A. java C. System
B. java/system D. /java/system
8. Complete the program: Write a program that accept a file name from the user and
create the file?
import java.io.*;
import java.util.Scanner;
class Files
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
// take the path from the user
String path= ???;
// Check if the file is not exist and create the file
2|Page
File obj = new File(path);
???
}
}
9. A text file words.txt is given, enroll the numbers from 1 to 20, each number on a separate
line in the given file.
10. Write a program that reads a text file and prints its odd lines on the screen.
11. Write a program that joins two text files and records the results in a third file.
In-lab Exercise
12. Write a program to create a text file in the path c:\java\abc.txt and check whether that
file is exists. Using the command exists(), isDirectory(), isFile(), getName() and
getAbsolutePath().
13. Write a program to get 10 usernames from the user and store it into file. Using
FileInputStream and FileOutputStream.
14. (File of Student Profiles) Create a program that stores student profile in a text file. The file
should contain first name, last name, ID number, class year and GPA of every student.
The entries should be displayed in the following format:
FirstName LastName ID# Class-Year GPA
Some sample data below:
Mekdes Demise 1000 2 3.85
Abel G/Kidan 2000 2 3.75
Abdu Nuredin 3000 2 3.65
[Hint: Create class Student containing the attributes, add appropriate methods.
Then on the main method create file, create an instance of student, write each
attribute. Finally, read the file to be displayed as it is.]
3|Page
15. (Object Serialization :) Create a program that stores student object profile in a text file.
The file should contain first name, last name, ID number, class year and GPA of every
student.
The entries should be displayed in the following format:
FirstName LastName ID# Class-Year GPA
Some sample data below:
Mekdes Demise 1000 2 3.85
Abel G/Kidan 2000 2 3.75
Abdu Nuredin 3000 2 3.65
[Hint: Create class Student (make the class Serializable) containing the attributes,
add appropriate methods. Then on the main method create file, create an
instance of student, write the instance via serialization process. Finally, read the
file via deserialization process.]
Post-lab Exercise
package lab_session_7;;
public class Lab_Session_7 {
public static void main(String[] args) {
System.out.Println(“No post lab exercise found!!!”);
}
}
4|Page