0% found this document useful (0 votes)
56 views

File Handling

The code creates a File object for a file called "Attributes.java" and prints out various attributes of the file like whether it exists, its path, length, if it is a directory or regular file, and if it is readable. It then prompts the user to save sample text to a file, write code to read and display the text using try/catch, and display the output. Finally, it asks the user to write code to display the length, path and number of words in the sample text file, append new text to the file, and re-run the display code to include the additional text.
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)
56 views

File Handling

The code creates a File object for a file called "Attributes.java" and prints out various attributes of the file like whether it exists, its path, length, if it is a directory or regular file, and if it is readable. It then prompts the user to save sample text to a file, write code to read and display the text using try/catch, and display the output. Finally, it asks the user to write code to display the length, path and number of words in the sample text file, append new text to the file, and re-run the display code to include the additional text.
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/ 3

1. What will be the output of the following code?

a. Explain your answer per line


import java.io.File; //imports the File class
public class Attributes
{
public static void main(String[] args)
{
File file = new File("src/Attributes.java"); //Creating an object of a new file
System.out.println("exists: "+file.exists()); //tests if the file exists
System.out.println("path: "+file.path()); //returns the pathname of the file
System.out.println("length: "+file.length()); //Returns the size of the file in bytes
System.out.println("isDirectory: "+file.isDirectory()); //tests if the file is a directory or not
System.out.println("isFile: "+file.isFile()); //tests whether the file is a regular file or not
System.out.println("canRead: "+file.canRead()); //tests whether the file is readable or not
}
}
2. In your IDE, follow the instruction to produce the
“The journey of a thousand miles begins with a single step” output.

a. Save this “The journey of a thousand miles begins with a single step” to
SampleText.txt inside your eclipse project folder.
b. Write the correct code to generate the given output using try catch with exception.
c. Make sure that you split your screen showing the correct code and the console
output. (paste your solution with output here in console)

Code:
3. Create an OutputFile.java that will display the length, the specific path, and the number of

words of the SampleText.txt then store and display it using SampleOuputText.txt. Use a try
catch with exception. (paste your solution with output here in console and text file)

4. Append a new row data with the text ”This little light of mine, I’m gonna let it shine” in
SampleText.txt then follow the instruction in number 3. Take note that it should contain
additional row data. (paste your solution with output here in console and text file)

You might also like