import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
public class MyFileHandler {
private String content;
private File file;
private FileReader reader;
private String fileName;
private int numOfLines;
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;
}
public void addLine(String str){
try {
Writer output;
str += System.lineSeparator() ;
output = new BufferedWriter(new FileWriter(fileName,true)); //clears file every time
output.append(str);
output.close();
}
catch (IOException e) {
e.printStackTrace();
}
readMyFile();
}
public void rewriteFile(String str){
try {
Writer output;
str += "" ;;
output = new BufferedWriter(new FileWriter(fileName)); //clears file every time
output.append(str);
output.close();
}
catch (IOException e) {
e.printStackTrace();
}
readMyFile();
}
}