版本更新至:3.76
调整:开发框架改为 Gradle 新增:Language2 工具新增 [book] 类型
This commit is contained in:
70
src/main/java/me/skymc/taboolib/fileutils/ConfigUtils.java
Normal file
70
src/main/java/me/skymc/taboolib/fileutils/ConfigUtils.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class ConfigUtils {
|
||||
|
||||
public static FileConfiguration decodeYAML(String args) {
|
||||
return YamlConfiguration.loadConfiguration(new StringReader(Base64Coder.decodeString(args)));
|
||||
}
|
||||
|
||||
public static String encodeYAML(FileConfiguration file) {
|
||||
return Base64Coder.encodeLines(file.saveToString().getBytes()).replaceAll("\\s+", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 UTF-8 的格式释放配置文件并载入
|
||||
*
|
||||
* 录入时间:2018年2月10日21:28:30
|
||||
* 录入版本:3.49
|
||||
*
|
||||
* @param plugin
|
||||
* @return
|
||||
*/
|
||||
public static FileConfiguration saveDefaultConfig(Plugin plugin, String name) {
|
||||
File file = new File(plugin.getDataFolder(), name);
|
||||
if (!file.exists()) {
|
||||
plugin.saveResource(name, true);
|
||||
}
|
||||
return load(plugin, file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 UTF-8 的格式载入配置文件
|
||||
*
|
||||
* @param main
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
public static FileConfiguration load(Plugin plugin, File file) {
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
try {
|
||||
yaml = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8));
|
||||
} catch (FileNotFoundException e) {
|
||||
MsgUtils.warn("配置文件载入失败!");
|
||||
MsgUtils.warn("插件: &4" + plugin.getName());
|
||||
MsgUtils.warn("文件: &4" + file.getName());
|
||||
}
|
||||
return yaml;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static FileConfiguration load(Plugin plugin, String file) {
|
||||
return load(plugin, FileUtils.file(file));
|
||||
}
|
||||
}
|
||||
98
src/main/java/me/skymc/taboolib/fileutils/CopyUtils.java
Normal file
98
src/main/java/me/skymc/taboolib/fileutils/CopyUtils.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class CopyUtils {
|
||||
|
||||
public static long Copy(File file1, File file2) throws IOException {
|
||||
// CHECK THE FILE
|
||||
if (!file1.exists()) {
|
||||
file1.createNewFile();
|
||||
}
|
||||
if (!file2.exists()) {
|
||||
file2.createNewFile();
|
||||
}
|
||||
|
||||
// RESET TIME
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
// I/O SETTING
|
||||
FileInputStream in = new FileInputStream(file1);
|
||||
FileOutputStream out = new FileOutputStream(file2);
|
||||
FileChannel inC = in.getChannel();
|
||||
FileChannel outC = out.getChannel();
|
||||
ByteBuffer b = null;
|
||||
|
||||
// CAPACITY [2GB]
|
||||
Integer length = 2097152;
|
||||
|
||||
// WORKSPACE
|
||||
while (true) {
|
||||
if (inC.position() == inC.size()) {
|
||||
inC.close();
|
||||
outC.close();
|
||||
return System.currentTimeMillis() - time;
|
||||
}
|
||||
if ((inC.size() - inC.position()) < length) {
|
||||
length = (int) (inC.size()-inC.position());
|
||||
}
|
||||
else {
|
||||
length = 2097152;
|
||||
}
|
||||
|
||||
b = ByteBuffer.allocateDirect(length);
|
||||
inC.read(b);
|
||||
b.flip();
|
||||
outC.write(b);
|
||||
outC.force(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static long Copy(FileInputStream in, File file2) throws IOException {
|
||||
// CHECK THE FILE
|
||||
if (!file2.exists()) {
|
||||
file2.createNewFile();
|
||||
}
|
||||
|
||||
// RESET TIME
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
// I/O SETTING
|
||||
FileOutputStream out = new FileOutputStream(file2);
|
||||
FileChannel inC = in.getChannel();
|
||||
FileChannel outC = out.getChannel();
|
||||
ByteBuffer b = null;
|
||||
|
||||
// CAPACITY [2GB]
|
||||
Integer length = 2097152;
|
||||
|
||||
// WORKSPACE
|
||||
while (true) {
|
||||
if (inC.position() == inC.size()) {
|
||||
inC.close();
|
||||
outC.close();
|
||||
return System.currentTimeMillis() - time;
|
||||
}
|
||||
if ((inC.size() - inC.position()) < length) {
|
||||
length = (int) (inC.size()-inC.position());
|
||||
}
|
||||
else {
|
||||
length = 2097152;
|
||||
}
|
||||
|
||||
b = ByteBuffer.allocateDirect(length);
|
||||
inC.read(b);
|
||||
b.flip();
|
||||
outC.write(b);
|
||||
outC.force(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
124
src/main/java/me/skymc/taboolib/fileutils/EncodeUtils.java
Normal file
124
src/main/java/me/skymc/taboolib/fileutils/EncodeUtils.java
Normal file
@@ -0,0 +1,124 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
|
||||
public class EncodeUtils {
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param fileName 要转换的文件
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(String fileName, String fromCharsetName, String toCharsetName) throws Exception {
|
||||
convert(new File(fileName), fromCharsetName, toCharsetName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName) throws Exception {
|
||||
convert(file, fromCharsetName, toCharsetName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 转换的编码
|
||||
* @param filter 文件名过滤器
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(String fileName, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
convert(new File(fileName), fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @param filter 文件名过滤器
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
if (file.isDirectory()) {
|
||||
File[] fileList = null;
|
||||
if (filter == null) {
|
||||
fileList = file.listFiles();
|
||||
} else {
|
||||
fileList = file.listFiles(filter);
|
||||
}
|
||||
for (File f : fileList) {
|
||||
convert(f, fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
} else {
|
||||
if (filter == null
|
||||
|| filter.accept(file.getParentFile(), file.getName())) {
|
||||
String fileContent = getFileContentFromCharset(file,
|
||||
fromCharsetName);
|
||||
saveFile2Charset(file, toCharsetName, fileContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 以指定编码方式读取文件,返回文件内容
|
||||
*
|
||||
* @param file 要转换的文件
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String getFileContentFromCharset(File file, String fromCharsetName) throws Exception {
|
||||
if (!Charset.isSupported(fromCharsetName)) {
|
||||
throw new UnsupportedCharsetException(fromCharsetName);
|
||||
}
|
||||
InputStream inputStream = new FileInputStream(file);
|
||||
InputStreamReader reader = new InputStreamReader(inputStream,
|
||||
fromCharsetName);
|
||||
char[] chs = new char[(int) file.length()];
|
||||
reader.read(chs);
|
||||
String str = new String(chs).trim();
|
||||
reader.close();
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以指定编码方式写文本文件,存在会覆盖
|
||||
*
|
||||
* @param file 要写入的文件
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @param content 文件内容
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void saveFile2Charset(File file, String toCharsetName, String content) throws Exception {
|
||||
if (!Charset.isSupported(toCharsetName)) {
|
||||
throw new UnsupportedCharsetException(toCharsetName);
|
||||
}
|
||||
OutputStream outputStream = new FileOutputStream(file);
|
||||
OutputStreamWriter outWrite = new OutputStreamWriter(outputStream, toCharsetName);
|
||||
outWrite.write(content);
|
||||
outWrite.close();
|
||||
|
||||
System.out.println("[Encodeing...] 更改文件: " + file.getPath());
|
||||
}
|
||||
}
|
||||
203
src/main/java/me/skymc/taboolib/fileutils/FileUtils.java
Normal file
203
src/main/java/me/skymc/taboolib/fileutils/FileUtils.java
Normal file
@@ -0,0 +1,203 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.List;
|
||||
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
public static String ip() {
|
||||
try {
|
||||
InputStream ins = null;
|
||||
URL url = new URL("http://1212.ip138.com/ic.asp");
|
||||
URLConnection con = url.openConnection();
|
||||
ins = con.getInputStream();
|
||||
InputStreamReader isReader = new InputStreamReader(ins, "GB2312");
|
||||
BufferedReader bReader = new BufferedReader(isReader);
|
||||
StringBuffer webContent = new StringBuffer();
|
||||
String str = null;
|
||||
while ((str = bReader.readLine()) != null) {
|
||||
webContent.append(str);
|
||||
}
|
||||
int start = webContent.indexOf("[") + 1;
|
||||
int end = webContent.indexOf("]");
|
||||
ins.close();
|
||||
return webContent.substring(start, end);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
return "[IP ERROR]";
|
||||
}
|
||||
|
||||
public static File file(String filePath) {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
public static File file(File Path, String filePath) {
|
||||
File file = new File(Path, filePath);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
public static String getStringFromInputStream(InputStream in, int size, String encode) {
|
||||
try {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] b = new byte[size];
|
||||
int i = 0;
|
||||
|
||||
while ((i = in.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
|
||||
bos.close();
|
||||
return new String(bos.toByteArray(), encode);
|
||||
} catch (IOException e) {
|
||||
MsgUtils.warn("输入流读取出错: &4" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getStringFromFile(File file, int size, String encode) {
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
BufferedInputStream bin = new BufferedInputStream(fin);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] b = new byte[size];
|
||||
int i = 0;
|
||||
|
||||
while ((i = bin.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
|
||||
bos.close();
|
||||
bin.close();
|
||||
fin.close();
|
||||
return new String(bos.toByteArray(), encode);
|
||||
} catch (IOException e) {
|
||||
MsgUtils.warn("文件读取出错: &4" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getStringFromURL(String url, int size) {
|
||||
try {
|
||||
URLConnection conn = new URL(url).openConnection();
|
||||
BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] b = new byte[size];
|
||||
int i = 0;
|
||||
|
||||
while ((i = bin.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
|
||||
bos.close();
|
||||
bin.close();
|
||||
return new String(bos.toByteArray(), conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding());
|
||||
} catch (IOException e) {
|
||||
MsgUtils.warn("网络访问出错: &4" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a UTF8 file
|
||||
*
|
||||
* @param strs list of lines
|
||||
* @param f file to write
|
||||
*
|
||||
*/
|
||||
public FileUtils(List<String> strs, File f) throws IOException
|
||||
{
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
||||
new FileOutputStream(f), "UTF8"));
|
||||
for(String s : strs) {
|
||||
out.write(s+"\n");
|
||||
}
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
public FileUtils(String str, File f) throws IOException
|
||||
{
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
||||
new FileOutputStream(f), "UTF8"));
|
||||
out.write(str);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static void download(String urlStr, String filename, File saveDir) {
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
|
||||
// 超时时间
|
||||
conn.setConnectTimeout(5 * 1000);
|
||||
// 防止屏蔽程序抓取而返回 403 错误
|
||||
conn.setRequestProperty("User-Agent", "Mozilla/31.0 (compatible; MSIE 10.0; Windows NT; DigExt)");
|
||||
|
||||
// 得到输入流
|
||||
InputStream inputStream = conn.getInputStream();
|
||||
// 获取数组
|
||||
byte[] data = read(inputStream);
|
||||
|
||||
// 创建文件夹
|
||||
if (!saveDir.exists()) {
|
||||
saveDir.mkdirs();
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
File file = new File(saveDir, filename);
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
|
||||
// 写入文件
|
||||
fos.write(data);
|
||||
|
||||
// 结束
|
||||
fos.close();
|
||||
inputStream.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] read(InputStream in) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int len = 0;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
try {
|
||||
while((len = in.read(buffer)) != -1) {
|
||||
bos.write(buffer, 0, len);
|
||||
}
|
||||
bos.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return bos.toByteArray();
|
||||
}
|
||||
}
|
||||
60
src/main/java/me/skymc/taboolib/fileutils/LogUtils.java
Normal file
60
src/main/java/me/skymc/taboolib/fileutils/LogUtils.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
import me.skymc.taboolib.other.DateUtils;
|
||||
|
||||
@Deprecated
|
||||
public class LogUtils {
|
||||
|
||||
public static void Log(String s, String s2)
|
||||
{
|
||||
try
|
||||
{
|
||||
File file = new File(Main.getInst().getDataFolder(), s2+".txt");
|
||||
if(!file.exists())
|
||||
{
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write(s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.getInst().getLogger().warning(s2+":"+s);
|
||||
}
|
||||
}
|
||||
|
||||
public static void newLog(Plugin main, String s, String s2)
|
||||
{
|
||||
try
|
||||
{
|
||||
File file = new File(main.getDataFolder(), s2+".txt");
|
||||
if(!file.exists())
|
||||
{
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write("["+DateUtils.CH_ALL.format(System.currentTimeMillis())+"]"+s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.getInst().getLogger().warning(s2+":"+s);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user