Source for de.webdings.tools.files.TextFiles

   1: /* TextFiles.java - Copyright (c) 2005 by Stefan Thesing
   2:  <p>This file is part of Webdings Tools.</p>
   3:  <p>Webdings Tools is free software; you can redistribute it and/or modify
   4:  it under the terms of the GNU General Public License as published by
   5:  the Free Software Foundation; either version 2 of the License, or
   6:  (at your option) any later version.</p>
   7: <p>Webdings Tools is distributed in the hope that it will be useful,
   8: but WITHOUT ANY WARRANTY; without even the implied warranty of
   9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10: GNU General Public License for more details.</p>
  11: <p>You should have received a copy of the GNU General Public License
  12: along with Webdings Tools; if not, write to the<br>
  13: Free Software Foundation, Inc.,<br>
  14: 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA<br>
  15: */
  16: package de.webdings.tools.files;
  17: 
  18: import java.io.File;
  19: import java.io.FileReader;
  20: import java.io.FileWriter;
  21: import java.io.IOException;
  22: 
  23: /**
  24:  * TextFiles is used to write and read text files from and
  25:  * to strings.
  26:  * 
  27:  * @author Stefan Thesing<br>
  28:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  29:  * @version 1.0 26.05.2005
  30:  */
  31: public class TextFiles {
  32:     /**
  33:      * Writes a specified String to a text file
  34:      * with a specified file name.
  35:      * @param fileName Name of the text file
  36:      * @param content content to be written
  37:      * @throws IOException
  38:      */
  39:     public static void writeToFile(String fileName, String content) throws IOException {
  40:         File f = new File(fileName);
  41:         FileWriter w = new FileWriter(f);
  42:         w.write(content);
  43:         w.close();
  44:     }
  45:     /**
  46:      * Reads the contents of the specified file
  47:      * and returns it as a String
  48:      * @param fileName Name of the file to be read
  49:      * @return the contents of the file as a String
  50:      * @throws IOException
  51:      * @throws IOException
  52:      */
  53:     public static String readFromFile(String fileName) 
  54:         throws IOException
  55:     {    
  56:             File f = new File(fileName);
  57:             int fileSize = (int) f.length();
  58:             int chars_gelesen = 0;
  59:             FileReader r = new FileReader(f);
  60:             char[] buffer = new char[fileSize];
  61:             while (r.ready())
  62:             {
  63:                 chars_gelesen +=
  64:                 r.read(buffer, chars_gelesen, 
  65:                         fileSize - chars_gelesen);
  66:             }
  67:             r.close();
  68:             String s = new String(buffer, 0, chars_gelesen);
  69:             return s;
  70:     }
  71: }

© 2005 by Stefan Thesing;
Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.