Lab 6
Lab 6
xhtml
Go Back
Completed Programs
The program must accept the name F of a text file as the input. The program must print all
the lines in the file as the output. If the file does NOT exist, the program must print
FileNotPresent as the output. Please fill in the missing lines of code so that the program
runs successfully.
Note: The file F will be in the same folder where the program executes.
Input Format:
The first line contains F.
Output Format:
The lines containing the content of the file F or FileNotPresent based on the given
condition.
Example Input/Output 1:
Input:
fileABC.txt
Output:
calendar
year
month
week
day
https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml 1/6
9/13/2023 https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml
Explanation:
Here fileABC.txt is present in the same folder where the program executes and the file
contains the following values.
calendar
year
month
week
day
Example Input/Output 2:
Input:
fileXYZ.txt
Output:
FileNotPresent
Explanation:
Here fileXYZ.txt is not present.
Hence the output is FileNotPresent
Show My Solution
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml 2/6
9/13/2023 https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml
Fill in the missing lines of code in the class FileManager.java by implementing the static
method writeContentToFile whose method signature is given below.
Example Input/Output 1:
Input:
fileABC.txt
Output:
SkillRack
Java
Webinar
Explanation:
Here fileABC.txt is present in the same folder where the program executes. So you must
write the given content to the file.
Example Input/Output 2:
Input:
fileXYZ.txt
Output:
SkillRack
Java
Webinar
Explanation:
Here fileXYZ.txt is not present. So you must create a new file and then write to the newly
created file.
Show My Solution
https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml 3/6
9/13/2023 https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml
package com.skillrack.webinar;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
Fill in the missing lines of code in the class FileManager.java by implementing the static
method appendContentToFile whose method signature is given below.
Example Input/Output 1:
Input:
number.txt
Three
Output:
OneTwoThree
https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml 4/6
9/13/2023 https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml
Explanation:
Here number.txt is present in the same folder where the program executes and the file
contains the following value.
OneTwo
Example Input/Output 2:
Input:
file1.txt
A chain is only as strong as its weakest link.
Output:
A chain is only as strong as its weakest link.
Explanation:
Here file1.txt is not present. So you must create a new file and then append the content to
the newly created file.
Show My Solution
package com.skillrack.webinar;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml 5/6
9/13/2023 https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml
https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml 6/6