Honestly, I don't know if this is the best option, but I think that is a System
like, common accepted abstraction:
In.java:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class In {
public In() {
}
/**
* Tranforms the content of a file (i.e: "origin.txt") in a String
*
* @param fileName: Name of the file to read
* @return String which represents the content of the file
*/
public String readFile(String fileName) {
FileReader fr = null;
try {
fr = new FileReader(fileName);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BufferedReader bf = new BufferedReader(fr);
String file = "", aux = "";
try {
while ((file = bf.readLine()) != null) {
aux = aux + file + "\n";
}
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
return aux;
}
}
Out.java:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Out {
public Out() {}
/**
* Writes on the file named as the first parameter the String which gets as second parameter.
*
* @param fileName: Destiny file
* @param message: String to write on the destiny file
*/
public void writeStringOnFile(String fileName, String message) {
FileWriter w = null;
try {
w = new FileWriter(fileName);
} catch (IOException e) {
e.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(w);
PrintWriter wr = new PrintWriter(bw);
try {
wr.write(message);
wr.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
IO.java:
public class IO {
public Out out;
public In in;
/**
* Object which provides an equivalent use to Java 'System' class
*/
public IO() {
this.setIn(new In());
this.setOut(new Out());
}
public void setIn(In in) {
this.in = in;
}
public In getIn() {
return this.in;
}
public void setOut(Out out) {
this.out = out;
}
public Out getOut() {
return this.out;
}
}
Therefore, you can use the class IO
almost in the same way you will use System
.
Hope it helps.
Clemencio Morales Lucas.