You are given the following class structure:
import java.io.*;
public class FileManipulator {
public static void main(String[] args) {
String fileName = "data.txt";
// TODO: Perform file operations
}
}
Your task is to complete the TODO
section in the main
method to perform the following file operations:
Check if the file data.txt
exists in the current directory. If it exists, print "File exists", otherwise print "File does not exist".
Create a new file named output.txt
in the current directory. If the file is successfully created, print "Output file created", otherwise print "Unable to create output file".
Write the following string to the output.txt
file: "This is a sample file."
To complete the given file operations, you need to use the File
class provided by Java's java.io
package. Here's how you can perform each operation:
import java.io.*;
public class FileManipulator {
public static void main(String[] args) {
String fileName = "data.txt";
// 1. Check if the file 'data.txt' exists
File file = new File(fileName);
if (file.exists()) {
System.out.println("File exists");
} else {
System.out.println("File does not exist");
}
// 2. Create a new file 'output.txt'
String outputFileName = "output.txt";
File outputFile = new File(outputFileName);
try {
if (outputFile.createNewFile()) {
System.out.println("Output file created");
} else {
System.out.println("Unable to create output file");
}
} catch (IOException e) {
System.out.println("An error occurred while creating the output file");
e.printStackTrace();
}
// 3. Write the string "This is a sample file" to 'output.txt' file
try (PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
writer.println("This is a sample file.");
System.out.println("Text written to output file");
} catch (IOException e) {
System.out.println("An error occurred while writing to the output file");
e.printStackTrace();
}
}
}
Let's explain the steps in detail.
To check if the file data.txt
exists, we create a new File
object using the file name. We then use the exists()
method to check if the file exists. If it does, we print "File exists", otherwise we print "File does not exist".
To create a new file output.txt
, we create another File
object using the desired file name. We then call the createNewFile()
method to create the file. This method returns a boolean value indicating whether the file was successfully created. If the file is created, we print "Output file created", otherwise we print "Unable to create output file". If any exception occurs during the file creation, we catch the IOException
and print an error message along with the stack trace.
To write the string "This is a sample file" to the output.txt
file, we use a PrintWriter
object wrapped in a FileWriter
object. We pass the outputFile
object to the FileWriter
constructor, which sets up the necessary file output stream. We then use the PrintWriter
to write the desired string to the file using the println()
method. If any exception occurs during the writing process, we catch the IOException
and print an error message along with the stack trace.
Overall, this program checks the existence of a file, creates a new file if it doesn't exist, and writes a string to a file.