From b110749cc302cbf45aaf3ea3caec5fe5b838dada Mon Sep 17 00:00:00 2001 From: James Su Date: Fri, 20 Sep 2019 15:44:18 +0000 Subject: [PATCH] Add new file --- .../ICS4U1/Reading And Writing To Files.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Grade 10/Computer Science/ICS4U1/Reading And Writing To Files.md diff --git a/Grade 10/Computer Science/ICS4U1/Reading And Writing To Files.md b/Grade 10/Computer Science/ICS4U1/Reading And Writing To Files.md new file mode 100644 index 0000000..2584715 --- /dev/null +++ b/Grade 10/Computer Science/ICS4U1/Reading And Writing To Files.md @@ -0,0 +1,60 @@ +## Reading and Outputting Files + +Outputing To Files +- you can save it for future use +- The printwriter class can be used to createa file and wrtie data to it +- necessay imports: +```java +import java.io.IOException; +import java.io.PrintWriter; +``` + +- Or: +``` +import java.io.*; +``` + +- It is declared similarly to the scanner class + +Different types of file can be created for data to be written to, depends on the extension eg. ".txt" creates a text file while docsx creates a docx document + +```java +code + +``` + +## Reading To Files +- Speed +- Less errors +- Easier to read individual cases +- able to get data from sources other than keyboard +- changing information is easy only to file +- more efficient less time to test +- large amounts of data can be entered quickly +- at the testing stage, data can be easily + +## How +- The file class is java's representation of a file or directoy path +- it is used to identify/ocate the file that is to be read +- code +- Scanner calss is used to read from file + +## Sample Code +```java +import java.util.Scanner; +import java.io.File; +import java.io.IOException; +public class ReadingToFileDemo { + public static void (String[]args) throws IOException { + File file = new File("test.txt); // the file you are reading from + Scanner read = new Scanner(file); + while(read.hasNext()) { + System.out.println(read.nextLine()); + read.next(); + } + } +} +``` + + +