0% found this document useful (0 votes)
20 views6 pages

Lab 6

The document displays the solutions to 3 Java programming problems submitted by a candidate on a skills assessment platform: 1. A file reading program that reads lines from a text file and prints them or prints "FileNotPresent" if the file does not exist. 2. A file writing program that writes specific content to a new file or appends to an existing file. 3. A file appending program that appends a string to the end of a file, creating the file if it does not exist. The candidate's solutions for each problem are shown, demonstrating how to open, read, and write to files in Java.

Uploaded by

LAVANYA M CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Lab 6

The document displays the solutions to 3 Java programming problems submitted by a candidate on a skills assessment platform: 1. A file reading program that reads lines from a text file and prints them or prints "FileNotPresent" if the file does not exist. 2. A file writing program that writes specific content to a new file or appends to an existing file. 3. A file appending program that appends a string to the end of a file, creating the file if it does not exist. The candidate's solutions for each problem are shown, demonstrating how to open, read, and write to files in Java.

Uploaded by

LAVANYA M CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

9/13/2023 https://www.skillrack.com/faces/candidate/labcodeprograms.

xhtml

AFRIN DINUSHA J-2112049@nec  0/10  13 0 0  744  2860

 Valid Till: 31-May-2025

 Home  Reports  Profile  Help  Logout

Go Back

Completed Programs

File Reading (Id-13952)

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

2112049@nec 2-Aug-2023 15:45:48 AFRIN DINUSHA J 2112049 CSE

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

public class Hello {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
String fn=sc.nextLine();
File file=new File(fn);
if(file.exists())
{
try(BufferedReader br=new BufferedReader(new FileReader(file)))
{
String line;
while((line=br.readLine())!=null)
{
System.out.println(line);
}
}
catch(Throwable e)
{
System.out.print("Error reading file: "+e.getMessage());
}
}
else
{
System.out.println("FileNotPresent");
}

https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml 2/6
9/13/2023 https://www.skillrack.com/faces/candidate/labcodeprograms.xhtml

} //end of main method


} //end of Hello class

File Writing (Id-13953)

Fill in the missing lines of code in the class FileManager.java by implementing the static
method writeContentToFile whose method signature is given below.

public static void writeContentToFile(String fileName)

The method must perform the following.


- If the file does NOT exist, then create a new file with the given fileName. Then the method
must write the following three lines to the file.
SkillRack
Java
Webinar

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

2112049@nec 2-Aug-2023 15:51:27 AFRIN DINUSHA J 2112049 CSE

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;

public class FileManager {


public static void writeContentToFile(String fileName)
{
try
{
FileWriter fw=new FileWriter(new File(fileName));
fw.write("SkillRack\nJava\nWebinar");
fw.close();
}
catch(IOException e)
{
System.out.println("Error writing to file:"+e.getMessage());
}
}
public static void printContentFromFile(String fileName) {
try {
FileReader fr = new FileReader(new File(fileName));
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.out.println("FileNotPresent");
}
}
} // end of FileManager class

File Append (Id-13954)

Fill in the missing lines of code in the class FileManager.java by implementing the static
method appendContentToFile whose method signature is given below.

public static void appendContentToFile(String fileName, String content)

The method must perform the following.


- If the file does NOT exist, then create a new file with the given fileName. Then the method
must append the given content to the file.

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

2112049@nec 2-Aug-2023 15:57:51 AFRIN DINUSHA J 2112049 CSE

package com.skillrack.webinar;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileManager {


public static void appendContentToFile(String fileName,String content)
{
try
{
FileWriter fw=new FileWriter(new File(fileName),true);
fw.write(content);
fw.close();
}
catch(IOException e)
{
System.out.println("Error writing to the file: "+e.getMessage());
}
}
public static void printContentFromFile(String fileName) {
try {
FileReader fr = new FileReader(new File(fileName));
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.out.println("FileNotPresent");
}
}
} // end of FileManager class

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

You might also like