📄 fileutil.java
字号:
}
public static boolean renameFile(File from_file, File to_file, boolean fail_on_existing_directory, FileFilter file_filter)
{
boolean success;
FileInputStream fis;
FileOutputStream fos;
if (!from_file.exists())
{
Logger.log(new LogAlert(true, 3, (new StringBuilder()).append("renameFile: source file '").append(from_file).append("' doesn't exist, failing").toString()));
return false;
}
if (to_file.exists() && (fail_on_existing_directory || from_file.isFile() || to_file.isFile()))
{
Logger.log(new LogAlert(true, 3, (new StringBuilder()).append("renameFile: target file '").append(to_file).append("' already exists, failing").toString()));
return false;
}
File to_file_parent = to_file.getParentFile();
if (!to_file_parent.exists())
mkdirs(to_file_parent);
if (from_file.isDirectory())
{
File files[] = null;
if (file_filter != null)
files = from_file.listFiles(file_filter);
else
files = from_file.listFiles();
if (files == null)
return true;
int last_ok = 0;
if (!to_file.exists())
to_file.mkdir();
int i = 0;
do
{
if (i >= files.length)
break;
File ff = files[i];
File tf = new File(to_file, ff.getName());
try
{
if (!renameFile(ff, tf, fail_on_existing_directory, file_filter))
break;
last_ok++;
}
catch (Throwable e)
{
Logger.log(new LogAlert(true, (new StringBuilder()).append("renameFile: failed to rename file '").append(ff.toString()).append("' to '").append(tf.toString()).append("'").toString(), e));
break;
}
i++;
} while (true);
if (last_ok == files.length)
{
File remaining[] = from_file.listFiles();
if (remaining != null && remaining.length > 0)
{
if (file_filter == null)
Logger.log(new LogAlert(true, 3, (new StringBuilder()).append("renameFile: files remain in '").append(from_file.toString()).append("', not deleting").toString()));
else
return true;
} else
if (!from_file.delete())
Logger.log(new LogAlert(true, 3, (new StringBuilder()).append("renameFile: failed to delete '").append(from_file.toString()).append("'").toString()));
return true;
}
for (remaining = 0; remaining < last_ok; remaining++)
{
File ff = files[remaining];
File tf = new File(to_file, ff.getName());
try
{
if (!renameFile(tf, ff, false, null))
Logger.log(new LogAlert(true, 3, (new StringBuilder()).append("renameFile: recovery - failed to move file '").append(tf.toString()).append("' to '").append(ff.toString()).append("'").toString()));
}
catch (Throwable e)
{
Logger.log(new LogAlert(true, (new StringBuilder()).append("renameFile: recovery - failed to move file '").append(tf.toString()).append("' to '").append(ff.toString()).append("'").toString(), e));
}
}
return false;
}
if (!COConfigurationManager.getBooleanParameter("Copy And Delete Data Rather Than Move") && from_file.renameTo(to_file))
return true;
success = false;
fis = null;
fos = null;
boolean flag;
fis = new FileInputStream(from_file);
fos = new FileOutputStream(to_file);
byte buffer[] = new byte[0x10000];
do
{
int len = fis.read(buffer);
if (len <= 0)
break;
fos.write(buffer, 0, len);
} while (true);
fos.close();
fos = null;
fis.close();
fis = null;
if (!from_file.delete())
{
Logger.log(new LogAlert(true, 3, (new StringBuilder()).append("renameFile: failed to delete '").append(from_file.toString()).append("'").toString()));
throw new Exception((new StringBuilder()).append("Failed to delete '").append(from_file.toString()).append("'").toString());
}
success = true;
flag = true;
if (fis != null)
try
{
fis.close();
}
catch (Throwable e) { }
if (fos != null)
try
{
fos.close();
}
catch (Throwable e) { }
if (!success && to_file.exists())
to_file.delete();
return flag;
Throwable e;
e;
Logger.log(new LogAlert(true, (new StringBuilder()).append("renameFile: failed to rename '").append(from_file.toString()).append("' to '").append(to_file.toString()).append("'").toString(), e));
flag = false;
if (fis != null)
try
{
fis.close();
}
catch (Throwable e) { }
if (fos != null)
try
{
fos.close();
}
catch (Throwable e) { }
if (!success && to_file.exists())
to_file.delete();
return flag;
Exception exception;
exception;
if (fis != null)
try
{
fis.close();
}
catch (Throwable e) { }
if (fos != null)
try
{
fos.close();
}
catch (Throwable e) { }
if (!success && to_file.exists())
to_file.delete();
throw exception;
}
public static void writeBytesAsFile(String filename, byte file_data[])
{
writeBytesAsFile2(filename, file_data);
}
public static boolean writeBytesAsFile2(String filename, byte file_data[])
{
File file = new File(filename);
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
out.write(file_data);
out.close();
return true;
Throwable t;
t;
Debug.out("writeBytesAsFile:: error: ", t);
return false;
}
public static boolean deleteWithRecycle(File file)
{
if (!COConfigurationManager.getBooleanParameter("Move Deleted Data To Recycle Bin"))
break MISSING_BLOCK_LABEL_47;
PlatformManager platform = PlatformManagerFactory.getPlatformManager();
if (!platform.hasCapability(PlatformManagerCapabilities.RecoverableFileDelete))
break MISSING_BLOCK_LABEL_36;
platform.performRecoverableFileDelete(file.getAbsolutePath());
return true;
return file.delete();
PlatformManagerException e;
e;
return file.delete();
return file.delete();
}
public static String translateMoveFilePath(String old_root, String new_root, String file_to_move)
{
if (!file_to_move.startsWith(old_root))
return null;
String file_suffix = file_to_move.substring(old_root.length());
if (new_root.endsWith(File.separator))
new_root = new_root.substring(0, new_root.length() - 1);
if (file_suffix.startsWith(File.separator))
file_suffix = file_suffix.substring(1);
return (new StringBuilder()).append(new_root).append(File.separator).append(file_suffix).toString();
}
public static void runAsTask(AzureusCoreOperationTask task)
{
AzureusCore core = AzureusCoreFactory.getSingleton();
core.createOperation(2, task);
}
public static boolean mkdirs(File f)
{
if (Constants.isOSX)
{
Pattern pat = Pattern.compile("^(/Volumes/[^/]+)");
Matcher matcher = pat.matcher(f.getParent());
if (matcher.find())
{
String sVolume = matcher.group();
File fVolume = new File(sVolume);
if (!fVolume.isDirectory())
{
Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append(sVolume).append(" is not mounted or not available.").toString()));
return false;
}
}
}
return f.mkdirs();
}
public static String getExtension(String fName)
{
int fileSepIndex = fName.lastIndexOf(File.separator);
int fileDotIndex = fName.lastIndexOf('.');
if (fileSepIndex == fName.length() - 1 || fileDotIndex == -1 || fileSepIndex > fileDotIndex)
return "";
else
return fName.substring(fileDotIndex);
}
public static String readFileAsString(File file, int size_limit, String charset)
throws IOException
{
FileInputStream fis = new FileInputStream(file);
String s = readInputStreamAsString(fis, size_limit, charset);
fis.close();
return s;
Exception exception;
exception;
fis.close();
throw exception;
}
public static String readFileAsString(File file, int size_limit)
throws IOException
{
FileInputStream fis = new FileInputStream(file);
String s = readInputStreamAsString(fis, size_limit);
fis.close();
return s;
Exception exception;
exception;
fis.close();
throw exception;
}
public static String readInputStreamAsString(InputStream is, int size_limit)
throws IOException
{
return readInputStreamAsString(is, size_limit, "ISO-8859-1");
}
public static String readInputStreamAsString(InputStream is, int size_limit, String charSet)
throws IOException
{
StringBuffer result = new StringBuffer(1024);
byte buffer[] = new byte[1024];
do
{
int len = is.read(buffer);
if (len <= 0)
break;
result.append(new String(buffer, 0, len, charSet));
if (size_limit < 0 || result.length() <= size_limit)
continue;
result.setLength(size_limit);
break;
} while (true);
return result.toString();
}
public static String readInputStreamAsStringWithTruncation(InputStream is, int size_limit)
throws IOException
{
StringBuffer result;
label0:
{
result = new StringBuffer(1024);
byte buffer[] = new byte[1024];
try
{
do
{
int len = is.read(buffer);
if (len <= 0)
break label0;
result.append(new String(buffer, 0, len, "ISO-8859-1"));
} while (size_limit < 0 || result.length() <= size_limit);
result.setLength(size_limit);
}
catch (SocketTimeoutException e) { }
}
return result.toString();
}
public static String readFileEndAsString(File file, int size_limit)
throws IOException
{
FileInputStream fis = new FileInputStream(file);
String s;
if (file.length() > (long)size_limit)
fis.skip(file.length() - (long)size_limit);
StringBuffer result = new StringBuffer(1024);
byte buffer[] = new byte[1024];
do
{
int len = fis.read(buffer);
if (len <= 0)
break;
result.append(new String(buffer, 0, len, "ISO-8859-1"));
if (result.length() <= size_limit)
continue;
result.setLength(size_limit);
break;
} while (true);
s = result.toString();
fis.close();
return s;
Exception exception;
exception;
fis.close();
throw exception;
}
public static byte[] readInputStreamAsByteArray(InputStream is)
throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768);
byte buffer[] = new byte[32768];
do
{
int len = is.read(buffer);
if (len > 0)
baos.write(buffer, 0, len);
else
return baos.toByteArray();
} while (true);
}
public static byte[] readFileAsByteArray(File file)
throws IOException
{
ByteArrayOutputStream baos;
byte buffer[];
InputStream is;
baos = new ByteArrayOutputStream((int)file.length());
buffer = new byte[32768];
is = new FileInputStream(file);
byte abyte0[];
do
{
int len = is.read(buffer);
if (len <= 0)
break;
baos.write(buffer, 0, len);
} while (true);
abyte0 = baos.toByteArray();
is.close();
return abyte0;
Exception exception;
exception;
is.close();
throw exception;
}
public static final boolean getUsableSpaceSupported()
{
return reflectOnUsableSpace != null;
}
public static final long getUsableSpace(File f)
{
return ((Long)reflectOnUsableSpace.invoke(f, null)).longValue();
Exception e;
e;
return -1L;
}
static
{
LOGID = LogIDs.CORE;
try
{
reflectOnUsableSpace = java/io/File.getMethod("getUsableSpace", (Class[])null);
}
catch (NoSuchMethodException e)
{
reflectOnUsableSpace = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -