Java File to String fileToString(String fileName, String timestamp, String adapterName)

Here you can find the source of fileToString(String fileName, String timestamp, String adapterName)

Description

file To String

License

Apache License

Declaration

public static String fileToString(String fileName, String timestamp, String adapterName) throws IOException 

Method Source Code


//package com.java2s;
/*//from w w w .j  a  v a  2 s .co m
   Copyright 2013 Nationale-Nederlanden
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static String fileToString(String fileName, String timestamp, String adapterName) throws IOException {
        StringBuffer sb = new StringBuffer();
        BufferedReader in = new BufferedReader(new FileReader(fileName));
        String line;
        boolean timestampActive = false;
        boolean adapterNameActive = false;
        while ((line = in.readLine()) != null) {
            line = line.trim();
            if (line.length() > 0) {
                if (line.startsWith("<statisticsCollection")) {
                    sb.append(line);
                    if (timestamp != null && line.indexOf("timestamp=\"" + timestamp + "\"") > 0) {
                        timestampActive = true;
                    } else {
                        timestampActive = false;
                    }
                } else {
                    if (line.startsWith("</statisticsCollection")) {
                        sb.append(line);
                    } else {
                        if (line.startsWith("<statgroup")) {
                            sb.append(line);
                            if (adapterName != null && line.indexOf("type=\"adapter\"") > 0) {
                                if (line.indexOf("name=\"" + adapterName + "\"") > 0) {
                                    adapterNameActive = true;
                                } else {
                                    adapterNameActive = false;
                                }
                            }
                        } else {
                            if (line.startsWith("</statgroup")) {
                                sb.append(line);
                            } else {
                                if (timestampActive && adapterNameActive) {
                                    sb.append(line);
                                }
                            }
                        }
                    }
                }
            }
        }
        in.close();

        return sb.toString();
    }
}

Related

  1. fileToString(String filename)
  2. fileToString(String filename)
  3. fileToString(String fileName)
  4. fileToString(String fileName)
  5. fileToString(String fileName)
  6. fileToString(String filePath)
  7. fileToString(String filePath, String encoding)
  8. fileToString(String nomeArquivo)
  9. fileToString(String path)