首页 > 其他 > 详细

FileStringSource

时间:2017-09-13 01:05:20      阅读:425      评论:0      收藏:0      [点我收藏+]

/**
* Copyright (c) 2011-2017, 
*
* 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.
*/

package com.jfinal.template;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import com.jfinal.core.Const;

/**
* FileStringSource
*/
public class FileStringSource implements IStringSource {

private String finalFileName;
private String fileName;
private String encoding;

public FileStringSource(String baseTemplatePath, String fileName, String encoding) {
this.finalFileName = buildFinalFileName(baseTemplatePath, fileName);
this.fileName = fileName;
this.encoding= encoding;
}

public FileStringSource(String baseTemplatePath, String fileName) {
this(baseTemplatePath, fileName, Const.DEFAULT_ENCODING);
}

public String getKey() {
return fileName;
}

public StringBuilder getContent() {
return loadFile(finalFileName, encoding);
}

public String getEncoding() {
return encoding;
}

public String getFinalFileName() {
return finalFileName;
}

private String buildFinalFileName(String baseTemplatePath, String fileName) {
char firstChar = fileName.charAt(0);
String finalFileName;
if (firstChar == ‘/‘ || firstChar == ‘\\‘) {
finalFileName = baseTemplatePath + fileName;
} else {
finalFileName = baseTemplatePath + File.separator + fileName;
}
return finalFileName;
}

public static StringBuilder loadFile(String fileName, String encoding) {
File file = new File(fileName);
if (!file.exists()) {
throw new RuntimeException("File not found : " + fileName);
}

StringBuilder ret = new StringBuilder((int)file.length() + 3);
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
// br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
if (line != null) {
ret.append(line);
} else {
return ret;
}

while ((line=br.readLine()) != null) {
ret.append("\n").append(line);
}
return ret;
} catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
com.jfinal.kit.LogKit.error(e.getMessage(), e);
}
}
}
}
}

 

FileStringSource

原文:http://www.cnblogs.com/mostearth/p/7512914.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!