package com.aawant.nlp.featureExtraction.tfidf;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class StringUtils{
public static BufferedReader getBr(String inputPath, String code){
File inputFile = null;
InputStreamReader isr = null;
InputStream is = null;
BufferedReader br = null;
try {
inputFile = new File(inputPath);
is = new FileInputStream(inputFile);
isr = new InputStreamReader(is, code);
br = new BufferedReader(isr);
} catch (Exception e) {
e.printStackTrace();
}
return br;
}
public static String path2String(String inputPath, String code){
StringBuilder result = new StringBuilder();
BufferedReader br = null;
try {
br = StringUtils.getBr(inputPath, "utf-8");
String temp = "";
while((temp = br.readLine()) != null){
result.append(temp+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result.toString();
}
public static BufferedReader getBr(File file, String code){
InputStreamReader isr = null;
InputStream is = null;
BufferedReader br = null;
try {
is = new FileInputStream(file);
isr = new InputStreamReader(is, code);
br = new BufferedReader(isr);
} catch (Exception e) {
e.printStackTrace();
}
return br;
}
public static boolean isEmpty(String str){
if(null == str || "".equals(str)){
return true;
}else{
return false;
}
}
public static boolean isNotEmpty(String str){
if(null != str && !"".equals(str)){
return true;
}else{
return false;
}
}
/**
* 灏嗗瓧绗︿覆鍐欏叆 鏂囦欢
* @param str
* @param outputPath
* @return
*/
public static boolean string2File(String str, String outputPath, String code){
boolean b = false;
if(isEmpty(outputPath)){
return b;
}
OutputStreamWriter osw = null;
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(outputPath);
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos,code);
bw = new BufferedWriter(osw);
bw.write(str);
b = true;
} catch (Exception e) {
b = false;
e.printStackTrace();
}finally{
try {
if(bw != null){
bw.close();
}
if(fos != null){
fos.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return b;
}
public static boolean string2File(String str, String outputPath, String code, boolean isAppend){
boolean b = false;
if(isEmpty(outputPath)){
return b;
}
OutputStreamWriter osw = null;
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(outputPath);
fos = new FileOutputStream(file, isAppend);
osw = new OutputStreamWriter(fos,code);
bw = new BufferedWriter(osw);
bw.write(str);
b = true;
} catch (Exception e) {
b = false;
e.printStackTrace();
}finally{
try {
if(bw != null){
bw.close();
}
if(fos != null){
fos.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return b;
}
public static String setToString(Set<String> set){
if(set == null || set.size() < 1){
return "";
}
Iterator<String> iter = set.iterator();
StringBuilder sb = new StringBuilder();
sb.append("[");
while(iter.hasNext()){
sb.append(iter.next()+",");
}
sb.append("]");
return sb.toString();
}
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); //鍒犻櫎瀹岄噷闈㈡墍鏈夊唴锟?
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //鍒犻櫎绌烘枃浠跺す
} catch (Exception e) {
e.printStackTrace();
}
}
//鍒犻櫎鎸囧畾鏂囦欢澶逛笅锟?锟斤拷鏂囦欢
//param path 鏂囦欢澶瑰畬鏁寸粷瀵硅矾锟?
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//鍏堝垹闄ゆ枃浠跺す閲岄潰鐨勬枃锟?
delFolder(path + "/" + tempList[i]);//鍐嶅垹闄ょ┖鏂囦欢锟?
flag = true;
}
}
return flag;
}
public static String getStrFromReader(Reader in){
StringBuilder sb = new StringBuilder();
try {
BufferedReader br = new BufferedReader(in);
String line = "";
while((line = br.readLine()) != null){
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public static String map2String(Map<String,String> map){
StringBuilder sb = new StringBuilder();
Set<String> set = map.keySet();
Iterator<String> iter = set.iterator();
while(iter.hasNext()){
String key = iter.next();
String value = map.get(key);
sb.append(key);
sb.append(" : ");
sb.append(value);
sb.append("/n");
}
return sb.toString();
}
}