Files Read/Write
Files Read/Write
Read a File:
We can also read the contents of a file in java. This is done using the scanner class. This method throws an FileNotFoundException if any error occurs. Hence it is important to write it in a try…….catch block.
Example:
Output:
Here we do not see an output, because the stundents.txt file created has no content.
Let’s read a file which has some content in it.
Example:
Output:
Write a File:
We use the FileWriter class to write into a file in java.
Example:
Output: