java 代码
- /**
- * Reads a text file to a String. The result String includes a '\n' after
- * each line in the file.
- *
- * @param fileName the name of the file to read
- * @return the content of the file in String format. The return String
- * includes a '\n' after each line in the file.
- * @throws IOException any i/o error is encounted
- */
- private String read(String fileName) throws IOException {
- StringBuffer sb = new StringBuffer();
- BufferedReader in = new BufferedReader(new FileReader(fileName));
- String currentLine;
- try {
- while ((currentLine = in.readLine()) != null) {
- sb.append(currentLine);
- sb.append("\n");
- }
- } finally {
- if (in != null)
- in.close();
- }
- return sb.toString();
- }
- /**
- * Writes a String to a file.
- *
- * @param fileName the file to write to
- * @param text the String to write
- * @throws IOException if any i/o error occurs
- */
- private void write(String fileName, String text) throws IOException {
- PrintWriter out = null;
- try {
- out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
- out.print(text);
- } finally {
- if (out != null)
- out.close();
- }
- }