Java classes and method
Type the following in the text file using Notepad:
“You are my sunshine, my only sunshine You make me happy when skies are gray You’ll never know dear, how much I love you Please don’t take my sunshine away.”
Save the file as myfile.txt
Open Eclipse as you have done in the past and start coding. Create a new project called LYRIC and within the project create a class called testfiles ). The code below has minor errors that you will have to fix on your own. The errors are intentional. Also, do not just copy this code, try to rewrite using classes and methods.
Start a Java project
import java.io.*; //needed to handle files
public class testFiles {
public static void main(String[] args) {
String fileName = “myfile.txt”; //you need to include the full file path in between ” “
// This will reference one line at a time
String line = null;
//below is the error handling if the file does not exist
try {
System.out.println(“place your name here”);
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line)
}
bufferedReader.close(); //closing file after processing
}
//error if the file does not exits
catch(FileNotFoundException ex) {
System.out.println(“Unable to open file ‘’);
}
catch(IOException ex) {
System.out.println( “Error reading file “)
}
}
}