package rocks.zipcode; import java.io.*; import java.util.Scanner; /** * @author leon on 16/11/2018. */ public class AlphaCharDocument extends Document { //private String fileName; private File file; //private FileWriter fw = new FileWriter(file); public AlphaCharDocument(String fileName) throws IOException { super(fileName); this.file = new File(fileName); // this.fileName = fileName; // fw = new FileWriter(file); } @Override public void write(String contentToBeWritten) { try { FileWriter fw = new FileWriter(file); isAlpha(contentToBeWritten); fw.write(contentToBeWritten); fw.close(); } catch (IOException ex) { ex.printStackTrace(); } } public String read() throws FileNotFoundException { try { Scanner scanner = new Scanner(file); StringBuilder sb = new StringBuilder(); while(scanner.hasNextLine()){ sb.append(scanner.nextLine()); } String content = sb.toString(); return content; } catch (IOException e) { e.printStackTrace(); } return null; } private void isAlpha(String s) throws IOException { if(!s.matches("[a-zA-Z\\s]*")) throw new IllegalArgumentException(); } }