init
This commit is contained in:
148
src/com/qy/utils/Tools.java
Executable file
148
src/com/qy/utils/Tools.java
Executable file
@@ -0,0 +1,148 @@
|
||||
package com.qy.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Random;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
||||
public class Tools {
|
||||
public static boolean stringIsNull(String string) {
|
||||
if (string == null || string.length() == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getRandomString(int length) {
|
||||
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
Random random = new Random();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int number = random.nextInt(str.length());
|
||||
sb.append(str.charAt(number));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
public static int getRandomInt(int min,int max) {
|
||||
int i=new Random().nextInt(max);
|
||||
if(i<min) {
|
||||
return getRandomInt(min, max);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
public static void deleteFiles(String path) {
|
||||
File files = new File(path);
|
||||
if (files.exists()) {
|
||||
if (files.isDirectory()) {
|
||||
for (File file : files.listFiles()) {
|
||||
deleteFiles(file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
// System.out.println("ɾ<><C9BE><EFBFBD>ļ<EFBFBD> "+files.getAbsolutePath());
|
||||
files.delete();
|
||||
}
|
||||
}
|
||||
public static boolean copyFileToName(String srcFileName,String destFileName,String fileName,boolean overlay) {
|
||||
File srcFile = new File(srcFileName);
|
||||
// <20>ж<EFBFBD>Դ<EFBFBD>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>
|
||||
if (!srcFile.exists()) {
|
||||
return false;
|
||||
} else if (!srcFile.isFile()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// <20>ж<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>
|
||||
File destFile = new File(destFileName);
|
||||
// <20><><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>Ŀ¼
|
||||
if (!destFile.exists()) {
|
||||
// Ŀ<><C4BF><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if (!destFile.mkdirs()) {
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ŀ¼ʧ<C2BC><CAA7>
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
||||
int byteread = 0; // <20><>ȡ<EFBFBD><C8A1><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
|
||||
InputStream in = null;
|
||||
OutputStream out = null;
|
||||
|
||||
try {
|
||||
if(fileName==null) {
|
||||
fileName=srcFile.getName();
|
||||
}
|
||||
in = new FileInputStream(srcFile);
|
||||
out = new FileOutputStream(destFile + "/" +fileName );
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
while ((byteread = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, byteread);
|
||||
}
|
||||
return true;
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (out != null)
|
||||
out.close();
|
||||
if (in != null)
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* <20><><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD>ļ<EFBFBD>
|
||||
*
|
||||
* @param srcFileName <20><><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
* @param destFileName Ŀ<><C4BF><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
* @param overlay <20><><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3>Ƿ<C7B7>
|
||||
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴɹ<C6B3><C9B9><EFBFBD><EFBFBD><EFBFBD>true<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>false
|
||||
*/
|
||||
public static boolean copyFile(String srcFileName, String destFileName,
|
||||
boolean overlay) {
|
||||
return copyFileToName(srcFileName, destFileName, null, overlay);
|
||||
}
|
||||
|
||||
public static JSONObject loadConfig(File file) {
|
||||
JSONObject json = null;
|
||||
BufferedReader reader;
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
reader = new BufferedReader(new FileReader(file));
|
||||
String tmp, str = "";
|
||||
while ((tmp = reader.readLine()) != null) {
|
||||
str += tmp;
|
||||
}
|
||||
json = new JSONObject(str);
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user