Added file handling

master
John 2022-06-02 14:48:44 -04:00
parent 588f7a5263
commit d640f5a926
1 changed files with 22 additions and 0 deletions

View File

@ -11,9 +11,12 @@ import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*; import javax.swing.*;
public class GamePanel extends JPanel implements Runnable, KeyListener{ public class GamePanel extends JPanel implements Runnable, KeyListener{
@ -193,4 +196,23 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
return flippedImage; return flippedImage;
} }
public void writeFile(String fileLocation, String writeString) throws IOException {
File newFile = new File(fileLocation);
FileWriter fileWriter = new FileWriter(newFile);
fileWriter.write(writeString);
fileWriter.close();
}
// will create file if it doesn't exist
public String readFile(String fileLocation) throws IOException {
File newFile = new File(fileLocation);
if (newFile.createNewFile()) {
return null;
} else {
Scanner fileReader = new Scanner(newFile);
// using the delimiter \\Z reads the entire file at once
return fileReader.useDelimiter("\\Z").next();
}
}
} }