Game-of-Life-WSU-CEG4180  v0.1
RefactoringtheGameofLife
EasyFile.java
Go to the documentation of this file.
1 /* This page is part of the Game of Life source code */
2 
3 /*
4  * Easy text file operations
5  * Copyright 2003, 2004 Edwin Martin <edwin@bitstorm.org>
6  *
7  */
8 package org.bitstorm.util;
9 
10 import java.awt.FileDialog;
11 import java.awt.Frame;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.net.URL;
19 
20 
21 /**
22  * Class for easy text file read and write operations.
23  * @author Edwin Martin
24  *
25  */
26 public class EasyFile {
27  private String filepath;
28  private String filename;
29  private InputStream textFileReader;
30  private OutputStream textFileWriter;
31  private final int bufferLength=1024;
32  private Frame parent;
33  private String title;
34  private String fileExtension;
35 
36  /**
37  * Constructs a EasyFile. Open file by filename.
38  * @param filepath path of file
39  * @throws FileNotFoundException
40  */
41  public EasyFile( String filepath ) {
42  this.filepath = filepath;
43  textFileReader = null;
44  textFileWriter = null;
45  fileExtension = null;
46  }
47 
48  /**
49  * Constructs a EasyFile. Open file by url.
50  * @param url url of file
51  * @throws FileNotFoundException
52  */
53  public EasyFile( URL url ) throws IOException {
54  this.filepath = null;
55  textFileWriter = null;
56  fileExtension = null;
57 
58  textFileReader = url.openStream();
59  }
60 
61  /**
62  * Constructs a EasyFile. Open file with file selector.
63  * @param parent parent frame
64  * @param title title of fileselector
65  * @throws FileNotFoundException
66  */
67  public EasyFile( Frame parent, String title ) {
68  this.parent = parent;
69  this.title = title;
70  textFileReader = null;
71  textFileWriter = null;
72  fileExtension = null;
73  }
74 
75  /**
76  * Constructs a EasyFile. Read file from stream.
77  * @param textFileReader stream to read from
78  */
79  public EasyFile( InputStream textFileReader ) {
80  this.textFileReader = textFileReader;
81  textFileWriter = null;
82  filepath = null;
83  fileExtension = null;
84  }
85 
86  /**
87  * Constructs a EasyFile. Write file to stream.
88  * @param textFileWriter stream to write to
89  */
90  public EasyFile( OutputStream textFileWriter ) {
91  this.textFileWriter = textFileWriter;
92  textFileReader = null;
93  filepath = null;
94  fileExtension = null;
95  }
96 
97  /**
98  * Reads a text file into a string.
99  * @return contents of file
100  */
101  public String readText() throws IOException {
102  int bytesRead;
103 
104  if ( textFileReader == null ) {
105  if ( filepath == null ) {
106  FileDialog filedialog = new FileDialog( parent, title, FileDialog.LOAD );
107  filedialog.setFile( filename );
108  filedialog.show();
109  if ( filedialog.getFile() != null ) {
110  filename = filedialog.getFile();
111  filepath = filedialog.getDirectory()+filename;
112  } else
113  return "";
114  }
115  textFileReader = new FileInputStream( filepath );
116  }
117 
118  StringBuffer text = new StringBuffer();
119  byte[] buffer = new byte[bufferLength];
120 
121  try {
122  while ( ( bytesRead = textFileReader.read( buffer, 0, bufferLength ) ) != -1 )
123  text.append( new String( buffer, 0, bytesRead ) );
124  } finally {
125  textFileReader.close();
126  }
127  return text.toString();
128  }
129 
130  /**
131  * Writes a string to a text file.
132  * @param text text to write
133  */
134  public void writeText( String text ) throws IOException {
135  if ( textFileWriter == null ) {
136  if ( filepath == null ) {
137  FileDialog filedialog = new FileDialog( parent, title, FileDialog.SAVE );
138  filedialog.setFile( filename );
139  filedialog.show();
140  if ( filedialog.getFile() != null ) {
141  filename = filedialog.getFile();
142  filepath = filedialog.getDirectory()+filename;
143  if ( fileExtension != null && filepath.indexOf('.') == -1 ) {
144  filepath = filepath+fileExtension;
145  }
146  } else
147  return;
148  }
149  textFileWriter = new FileOutputStream( filepath );
150  }
151 
152  try {
153  textFileWriter.write( text.getBytes() );
154  } finally {
155  textFileWriter.close();
156  }
157  }
158 
159  /**
160  * Sets filename to use
161  * @param s filename
162  */
163  public void setFileName( String s ) {
164  filename = s;
165  }
166 
167  /**
168  * Gets filename
169  * @return filename
170  */
171  public String getFileName() {
172  return filename == null ? filepath : filename;
173  }
174 
175  /**
176  * Sets file extension to use
177  * @param s filename
178  */
179  public void setFileExtension( String s ) {
180  fileExtension = s;
181  }
182 }
EasyFile(InputStream textFileReader)
Definition: EasyFile.java:79
EasyFile(String filepath)
Definition: EasyFile.java:41
OutputStream textFileWriter
Definition: EasyFile.java:30
void setFileExtension(String s)
Definition: EasyFile.java:179
void setFileName(String s)
Definition: EasyFile.java:163
void writeText(String text)
Definition: EasyFile.java:134
InputStream textFileReader
Definition: EasyFile.java:29
EasyFile(OutputStream textFileWriter)
Definition: EasyFile.java:90
EasyFile(Frame parent, String title)
Definition: EasyFile.java:67