Commit 7e01608d authored by a.guest's avatar a.guest

Initial Commit

Demo of how to read from and write to files
parents
Pipeline #224 failed with stages
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>FileAccess</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
First line to file
Second line to file
Third line to file
Fourth line to file
Output number 0
Output number 1
Output number 2
Output number 3
Output number 4
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileHandler {
File file = null; // fh forms a connection to a file but does not open it
PrintWriter pw = null; // pw is used to open a file for writing, write to it and close it after
Scanner sc=null; // sc is used to open a file for reading, read from it and close it after
// Class constructor - mkaes connection to a file
public FileHandler(String filename)
{
file = new File(filename);
}
// open a file for writing. Exit elegantly if we cannot open the file
public void prepare_to_write()
{
try
{
// Connect the a PrintWriter Object to our file
// If it fails it will trigger a FileNotFoundException
// which will be handled by the catch
pw = new PrintWriter(file);
}
catch (FileNotFoundException e)
{
System.out.println("Program is closing, unable to create file.");
System.exit(-1); // Terminates program
}
}
// open a file for reading. Exit elegantly if we cannot open the file
public void prepare_to_read()
{
try
{
sc = new Scanner(file);
}
catch (FileNotFoundException e)
{
System.out.println("Program is closing, unable to read file.");
System.exit(-1); // Terminates program
}
}
// Writes a string to the next line in the file.
// NB must have used prepare_to_write() before you can write_to_file()
public void write_to_file(String text)
{
pw.println(text);
}
// checks to see if we have reached the end of a file we have opened for reading
public Boolean end_of_file()
{
return !(sc.hasNextLine());
}
// returns the next line from a file as a string
// NB must have used prepare_to_read() before you can read_line_from_file()
public String read_line_from_file()
{
return sc.nextLine();
}
// close a file that was opened for writing
public void end_writing()
{
pw.close();
}
// close a file that was opened for reading
public void end_reading()
{
sc.close();
}
}
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Creates an object linked to a file but doesn't actually open it
FileHandler fh = new FileHandler("me.txt");
// opens the file for writing
fh.prepare_to_write();
// writes text to the file
fh.write_to_file("First line to file");
fh.write_to_file("Second line to file");
fh.write_to_file("Third line to file");
fh.write_to_file("Fourth line to file");
for (int i=0; i<5; i++)
{
fh.write_to_file("Output number " + i);
}
// remember to do this when you are finished
// closes the file (when writing)
fh.end_writing();
// Open the file for reading
// Note because we have already connected the FileHandler fh to a file we
// are simply using that again.
// You would need to connect to a file if you hadn't already
fh.prepare_to_read();
String tempString = "";
// check to see if we've reached the end of the line
while (!fh.end_of_file())
{
// read a line of text from the file
tempString = fh.read_line_from_file();
// you might need to process this if you are reading specific data
System.out.println(tempString);
}
// Close the file when we are finished reading
fh.end_reading();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment