import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; public class MyFileHandler { private String content; private File file; private FileReader reader; private String fileName; private int numOfLines; public void addLine(String [] line){ String newLine = ""; for (int i=0;i<line.length-1;i++){ newLine += line[i]+","; } newLine += line[line.length-1]; content = content.trim(); content += "rn"+newLine; writeAll(); } private void writeAll(){ try { PrintWriter writer = new PrintWriter(fileName, "UTF-8"); writer.println(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } readMyFile(); } public void updateFile(String [][] arr){ content = ""; for (int row = 0;row<arr.length;row++){ for (int col = 0;col<arr[0].length-1;col++){ content += arr[row][col]+" , "; } if (row != arr.length-1) content += arr[row][arr[0].length-1]+"\r\n"; else content += arr[row][arr[0].length-1]; } writeAll(); } public MyFileHandler(String fileName){ this.fileName = fileName; content = ""; file = new File(fileName); numOfLines = 0; } public void readMyFile(){ try { reader = new FileReader(file); char[] chars = new char[(int) file.length()]; reader.read(chars); content = new String(chars); reader.close(); } catch (IOException e) { e.printStackTrace(); } String [] LinesArr = content.split("n"); numOfLines = LinesArr.length; } public String [] Lines(){ String [] LinesArr = content.split("n"); numOfLines = LinesArr.length; return LinesArr; } public String [] LineItems(int lineNumber){ String [] words = Lines()[lineNumber].split(","); return words; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getContent() { return content; } public int getNumOfLines() { return numOfLines; } }