I'm writing a Table class, which stores column name and length information about a relation. The class itself doesn't store the records/tuples, however, as that information is supposed to be directly entered into a txt file via an Insert method. To make the insert method easier by not recreating a printwriter for every call, can the Table class have its own Printwriter that I can call from Table.insert? Basically,
public class Table{
private String tableName;
private PrintWriter writer;
public void Table(){
//get table name
writer = new PrintWriter(tableName+".txt");
...}
public void insert(){
//get, format, add info to file
writer.format(formatstring, args);
}
Are there any flaws with this use of PrintWriter?
In a word - yes, you can hold a PrintWriter
member. Since you're dealing with a filesystem here, there are all sorts of things that could go wrong when creating the PrintWriter
. It may be a better practice to open it in a constructor (instead of inlining it in the member's definition) to allow proper exception handling.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.