?? fileutil.java
字號:
/*
* 創建日期 2004-10-18
*
* TODO
*/
package com.ntsky.file;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.regex.*;
/**
* @author Administrator
*
* TODO
*/
public class FileUtil {
/**
* 特殊字符轉
*/
public static String htmlEncode(String str) {
if (str == null) {
str = "";
return str;
}
str = str.trim(); //去掉首尾空格
/*// 正則表達式替換
// 定義替換模板
Pattern pattern = Pattern.compile("\\");
// 定義替換的原始主字符串
Matcher matcher = pattern.matcher(str);
str = matcher.replaceAll("/");*/
str = replace(str, "\\", "/");
return str;
}
/**
* 創建目錄的方法
* @param fileDir
* @param context
*/
public static void makeDir(String fileDir,String context){
// 根據目錄參數,創建無限層的目錄結構
StringTokenizer stringTokenizer = new StringTokenizer(fileDir, "/");
String strTemp = "";
while (stringTokenizer.hasMoreTokens()) {
String str = stringTokenizer.nextToken();
if("".equals(strTemp)){
strTemp = str;
}
else{
strTemp = strTemp + "/" + str;
}
// System.out.println("context + strTemp " + context + strTemp);
File dir = new File(context + strTemp);
if (!dir.isDirectory()) {
dir.mkdirs();
}
}
}
/**
* 創建文件的方法
*
*/
public static File makeFile(String filePath){
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException ex) {
System.out.println("創建文件錯誤... " + ex.getMessage());
}
}
return file;
}
public static void testPattern(){
Pattern p=null;
Matcher m=null; //操作的字符串
p = Pattern.compile("ab");
m = p.matcher("aaaaab");
String s = m.replaceAll("d");
System.out.println(s+"< br>");
}
/**
* 字符串轉換方法
* @param source String
* @param oldString String
* @param newString String
* @return String
*/
public static String replace(String source, String oldString,
String newString) {
StringBuffer output = new StringBuffer();
int lengthOfsource = source.length(); //源字符串長度
int lengthOfold = oldString.length(); //老字符串長度
int posStart = 0; //開始搜索位置
int pos; //搜索到的老字符串的位置
//source.indexOf(oldString,posStart)檢索某子串在字符串postStart以后第一次出現的位置,如果未找到就返回一個-1。
while ( (pos = source.indexOf(oldString, posStart)) >= 0) { //得到字符串的位置(eg:如果有<br>就執行,沒有就跳出,不要處理。)
//將以posStart起始以pos-1結束之間的內容拷貝到另一個字符串中。因為posStar從0開始的。
output.append(source.substring(posStart, pos)); //append方法將文本添加到當前StringBuffer對象內容的結尾。
output.append(newString); //替換成新字符串
posStart = pos + lengthOfold; //位置也變為找到了之后的位置,pos為得到第一次出現字符的位置,lengthold為字符的長度
}
if (posStart < lengthOfsource) {
//source.substring(posStart)以lengthOfsource開始的字符串拷貝到列一個字符串中
output.append(source.substring(posStart));
}
//這個方法將其內容轉換成一個可以被用于輸出的字符串對象。它允許操作對應的文本用于輸出或數據存儲。
return output.toString();
}
/**
* 設置的文件類型是否符合
*
* @param fileType
* @return
*/
public static List getFileType(String fileType) {
StringTokenizer st = new StringTokenizer(fileType, ",");
List listType = new ArrayList();
// System.out.println(listType + "包含','的元素 : " + st.countTokens() + "\n\r");
while (st.hasMoreTokens()) {
listType.add(st.nextToken());
}
return listType;
}
/**
* 獲得目錄全名
*
* @return String
*/
public static String getAllPath(String filePath) {
if (!filePath.endsWith("/")) {
filePath += "/";
}
return filePath;
}
public static void main(String[] args) {
FileUtil.makeDir("abcdaa","D:/webserver/resin/webapps/upload/");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -