# Reading from…
```java
try
{
Scanner scan = new Scanner(new File("M:/samplefile.txt"));
while(scan.hasNextLine())
{
System.out.println(scan.nextLine());
}
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
}
```
# Writing to…
```java
try
{
PrintWriter out = new PrintWriter(new FileWriter("M:/samplefile.txt"));
out.println("Knives");
out.println("Forks");
out.println("Spoons");
out.close();
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
```
Read the following: [[File Paths]]
# Exercise
1. Write a program that writes the numbers 1-10 to a file "numbers.txt"
2. Write a program that writes the following lines to a file "web.html"
* "\<h1> This is a main heading \</h1>"
* "Welcome to my <b>web</b> page"
* Open the file in a web browser. What do you notice?
3. Use a for loop to write a program that writes the first 100 square and cube numbers to file named powers.txt
4. Save the file [colours.txt](https://vle.blue-coat.org/pluginfile.php/62908/mod_resource/content/1/colours.txt) to your m: drive. This file contains a number of colours, red, blue and green.
5. Write a program to count how many reds there are in the file
6. Extend your program so that it counts the number of blues and greens
7. Now get your program to find the two words hidden in the list that are not red, blue or green.
8. Write a program that records a shopping list. The program will prompting the user to enter an item to buy and then writes the item to the the file "shopping.txt". The program loops until the user enters the item "done" at which point the program terminates.
![[colours.txt]]
[[colours.txt]]
## **Extension**
Write a program that reads in the file "shopping.txt" and outputs a file "Shopping List.html". "Shopping List.html" should have the list formatted as a table with spaces to tick off items purchased.