Package com.sos.JSHelper.io.Files

Source Code of com.sos.JSHelper.io.Files.JSTextFile

/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
**   GNU General Public License version 2.0 (GPL)
**
**   as published by the Free Software Foundation
**   http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
**   LICENSE.GPL included in the packaging of this file.
**
** or the
** 
**   Agreement for Purchase and Licensing
**
**   as offered by Software- und Organisations-Service GmbH
**   in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
********************************************************** end of preamble*/
package com.sos.JSHelper.io.Files;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
//import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/* ---------------------------------------------------------------------------
APL/Software GmbH - Berlin
##### generated by ClaviusXPress (http://www.sos-berlin.com) #########
Samstag, 27. Oktober 2007, Klaus.Buettner@sos-berlin.com (KB)
-------------------------------------------------------------------------------
<docu type="smcw" version="1.0">
<project>com.sos.IDocs</project>
<name>JSTextFile.java</name>
<title>plain-Text-File
</title>
<description>
<para>
plain-Text-File
</para>
</description>
<params>
</params>
<keywords>
  <keyword>File</keyword>
  <keyword>planText</keyword>
</keywords>
<categories>
<category>File</category>
</categories>
<date>Samstag, 27. Oktober 2007</date>
<copyright>� 2000, 2001 by SOS GmbH Berlin</copyright>
<author>Klaus.Buettner@sos-berlin.com</author>
<changes>
<change who='KB' when='Samstag, 27. Oktober 2007' id='created'>
   <what>
   <para>
   created
   </para>
   </what>
</change>
</changes>
</docu>
---------------------------------------------------------------------------- */
public class JSTextFile extends JSFile {
  private static final long  serialVersionUID  = 1L;
  @SuppressWarnings("unused")
  private final String    conClassName    = "JSTextFile";

  /* ---------------------------------------------------------------------------
  <method type="smcw" version="1.0">
  <name></name>
  <title>plain-Text-File</title>
  <description>
  <para>
  plain-Text-File
  </para>
  <para>
  Initial-Wert (Default) ist "true" (ohne Anf�hrungszeichen).
  </para>
  <mandatory>true</mandatory>
  </description>
  <params>
  </params>
  <keywords>
    <keyword>File</keyword>
    <keyword>planText</keyword>
  </keywords>
  <categories>
  <category>File</category>
  </categories>
  </method>
  ---------------------------------------------------------------------------- */

  public JSTextFile(String pstrFileName) {
    super(pstrFileName);
  } // public JSTextFile

  /**
   *
   * \brief replaceString - Zeichenfolge in der Datei ersetzen durch eine andere Zeichenfolge
   *
   * Die Methode ersetzt alle Vorkommen einer Zeichenfolge in der gesamten Datei
   * durch eine andere anzugebende Zeichenfolge.
   *
   * @param pstrRegEx - zu ersetzende Zeichenfolge oder regul�rer Ausdruck
   * @param pstrReplacement - einzusetzende Zeichenfolge
   * @throws IOException
   */
  public void replaceString(String pstrRegEx, String pstrReplacement) throws IOException {

    // TODO die zu ersetzenden Strings m�ssen als Stringpaare kommen

    String strFileN = this.getAbsolutePath();
    File tempFile = new File(strFileN + "~");
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

    File file = new File(strFileN);
    BufferedReader reader = new BufferedReader(new FileReader(file));
   
    while (true) {
      String line = reader.readLine();
      if (line == null) {
        break;
      }
      line = line.replaceAll(pstrRegEx, pstrReplacement);
      writer.write(line);
      writer.newLine();
    }
    writer.close();
    reader.close();
    file.delete(); // otherwise the rename leads into an error
    tempFile.renameTo(file);
  }// replaceString

} // public class JSTextFile
TOP

Related Classes of com.sos.JSHelper.io.Files.JSTextFile

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.