jodd.io.FileUtil.checkExists()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(204)

本文整理了Java中jodd.io.FileUtil.checkExists()方法的一些代码示例,展示了FileUtil.checkExists()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtil.checkExists()方法的具体详情如下:
包路径:jodd.io.FileUtil
类名称:FileUtil
方法名:checkExists

FileUtil.checkExists介绍

[英]Checks if File exists. Throws IOException if not.
[中]检查文件是否存在。如果不是,则抛出IOException。

代码示例

代码示例来源:origin: oblac/jodd

/**
 * Checks if {@link File} exists. Throws IllegalArgumentException if not.
 *
 * @param file {@link File}
 * @throws IllegalArgumentException if file does not exist.
 */
private static void checkReferenceExists(final File file) throws IllegalArgumentException {
  try {
    checkExists(file);
  } catch (FileNotFoundException e) {
    throw new IllegalArgumentException("Reference file not found: " + file);
  }
}

代码示例来源:origin: oblac/jodd

/**
 * @see #unicodeInputStreamOf(InputStream, String)
 * @see #checkExists(File)
 * @see #checkIsFile(File)
 */
private static UnicodeInputStream unicodeInputStreamOf(final File file) throws IOException {
  checkExists(file);
  checkIsFile(file);
  return unicodeInputStreamOf(new FileInputStream(file), null);
}

代码示例来源:origin: oblac/jodd

/**
 * Read file and returns byte array with contents.
 *
 * @param file  {@link File} to read
 * @param count number of bytes to read
 * @return byte array from {@link File} contents.
 * @throws IOException if not a {@link File} or {@link File} does not exist or file size is
 *                     larger than {@link Integer#MAX_VALUE}.
 */
public static byte[] readBytes(final File file, final int count) throws IOException {
  checkExists(file);
  checkIsFile(file);
  long numToRead = file.length();
  if (numToRead >= Integer.MAX_VALUE) {
    throw new IOException("File is larger then max array size");
  }
  if (count > NEGATIVE_ONE && count < numToRead) {
    numToRead = count;
  }
  byte[] bytes = new byte[(int) numToRead];
  RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
  randomAccessFile.readFully(bytes);
  randomAccessFile.close();
  return bytes;
}

代码示例来源:origin: oblac/jodd

/**
 * Checks that srcDir exists, that it is a directory and if srcDir and destDir are not equal.
 *
 * @param srcDir  Source directory
 * @param destDir Destination directory
 * @throws IOException if any of the above conditions are not true.
 */
private static void checkDirCopy(final File srcDir, final File destDir) throws IOException {
  checkExists(srcDir);
  checkIsDirectory(srcDir);
  if (equals(srcDir, destDir)) {
    throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are equal");
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Checks that file copy can occur.
 *
 * @param srcFile  Source {@link File}
 * @param destFile Destination {@link File}
 * @throws IOException if srcFile does not exist or is not a file or
 *                     srcFile and destFile are equal or cannot create ancestor directories.
 */
private static void checkFileCopy(final File srcFile, final File destFile) throws IOException {
  checkExists(srcFile);
  checkIsFile(srcFile);
  if (equals(srcFile, destFile)) {
    throw new IOException("Files '" + srcFile + "' and '" + destFile + "' are equal");
  }
  File destParent = destFile.getParentFile();
  if (destParent != null && !destParent.exists()) {
    checkCreateDirectory(destParent);
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Cleans a directory without deleting it.
 *
 * @param destDir destination to clean.
 * @throws IOException if something went wrong.
 */
public static void cleanDir(final File destDir) throws IOException {
  checkExists(destDir);
  checkIsDirectory(destDir);
  File[] files = destDir.listFiles();
  if (files == null) {
    throw new IOException("Failed to list contents of: " + destDir);
  }
  IOException exception = null;
  for (File file : files) {
    try {
      if (file.isDirectory()) {
        deleteDir(file);
      } else {
        file.delete();
      }
    } catch (IOException ioex) {
      exception = ioex;
      continue;
    }
  }
  if (exception != null) {
    throw exception;
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Reads lines from source {@link File} with specified encoding and returns lines as {@link String}s in array.
 *
 * @param file     {@link File} to read.
 * @param encoding Endoing to use.
 * @return array of Strings which represents lines in the {@link File}.
 * @throws IOException if {@link File} does not exist or is not a {@link File} or there is an issue reading
 *                     the {@link File}.
 */
public static String[] readLines(final File file, final String encoding) throws IOException {
  checkExists(file);
  checkIsFile(file);
  List<String> list = new ArrayList<>();
  InputStream in = streamOf(file, encoding);
  try {
    BufferedReader br = new BufferedReader(StreamUtil.inputStreamReadeOf(in, encoding));
    String strLine;
    while ((strLine = br.readLine()) != null) {
      list.add(strLine);
    }
  } finally {
    StreamUtil.close(in);
  }
  return list.toArray(new String[0]);
}

代码示例来源:origin: oblac/jodd

/**
 * Reads file content as char array.
 *
 * @param file     {@link File} to read.
 * @param encoding Encoding to use.
 * @return array of characters.
 * @throws IOException if something went wrong.
 */
public static char[] readChars(final File file, final String encoding) throws IOException {
  checkExists(file);
  checkIsFile(file);
  InputStream in = streamOf(file, encoding);
  try {
    return StreamUtil.readChars(in, encoding);
  } finally {
    StreamUtil.close(in);
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Reads UTF file content as char array.
 *
 * @param file {@link File} to read.
 * @return array of characters.
 * @throws IOException if something went wrong.
 */
public static char[] readUTFChars(final File file) throws IOException {
  checkExists(file);
  checkIsFile(file);
  UnicodeInputStream in = unicodeInputStreamOf(file);
  try {
    return StreamUtil.readChars(in, detectEncoding(in));
  } finally {
    StreamUtil.close(in);
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Reads {@link File} content as {@link String} encoded in provided encoding.
 * For UTF encoded files, detects optional BOM characters.
 *
 * @param file     {@link File} to read.
 * @param encoding Encoding to use.
 * @return String representing {@link File} content.
 * @throws IOException if copy to {@link InputStream} errors.
 * @see #streamOf(File, String)
 * @see StreamUtil#copy(InputStream, String)
 */
public static String readString(final File file, final String encoding) throws IOException {
  checkExists(file);
  checkIsFile(file);
  InputStream in = streamOf(file, encoding);
  try {
    return StreamUtil.copy(in, encoding).toString();
  } finally {
    StreamUtil.close(in);
  }
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * Checks if {@link File} exists. Throws IllegalArgumentException if not.
 *
 * @param file {@link File}
 * @throws IllegalArgumentException if file does not exist.
 */
private static void checkReferenceExists(final File file) throws IllegalArgumentException {
  try {
    checkExists(file);
  } catch (FileNotFoundException e) {
    throw new IllegalArgumentException("Reference file not found: " + file);
  }
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * @see #unicodeInputStreamOf(InputStream, String)
 * @see #checkExists(File)
 * @see #checkIsFile(File)
 */
private static UnicodeInputStream unicodeInputStreamOf(final File file) throws IOException {
  checkExists(file);
  checkIsFile(file);
  return unicodeInputStreamOf(new FileInputStream(file), null);
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * Read file and returns byte array with contents.
 *
 * @param file  {@link File} to read
 * @param count number of bytes to read
 * @return byte array from {@link File} contents.
 * @throws IOException if not a {@link File} or {@link File} does not exist or file size is
 *                     larger than {@link Integer#MAX_VALUE}.
 */
public static byte[] readBytes(final File file, final int count) throws IOException {
  checkExists(file);
  checkIsFile(file);
  long numToRead = file.length();
  if (numToRead >= Integer.MAX_VALUE) {
    throw new IOException("File is larger then max array size");
  }
  if (count > NEGATIVE_ONE && count < numToRead) {
    numToRead = count;
  }
  byte[] bytes = new byte[(int) numToRead];
  RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
  randomAccessFile.readFully(bytes);
  randomAccessFile.close();
  return bytes;
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * Checks that srcDir exists, that it is a directory and if srcDir and destDir are not equal.
 *
 * @param srcDir  Source directory
 * @param destDir Destination directory
 * @throws IOException if any of the above conditions are not true.
 */
private static void checkDirCopy(final File srcDir, final File destDir) throws IOException {
  checkExists(srcDir);
  checkIsDirectory(srcDir);
  if (equals(srcDir, destDir)) {
    throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are equal");
  }
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * Cleans a directory without deleting it.
 *
 * @param destDir destination to clean.
 * @throws IOException if something went wrong.
 */
public static void cleanDir(final File destDir) throws IOException {
  checkExists(destDir);
  checkIsDirectory(destDir);
  File[] files = destDir.listFiles();
  if (files == null) {
    throw new IOException("Failed to list contents of: " + destDir);
  }
  IOException exception = null;
  for (File file : files) {
    try {
      if (file.isDirectory()) {
        deleteDir(file);
      } else {
        file.delete();
      }
    } catch (IOException ioex) {
      exception = ioex;
      continue;
    }
  }
  if (exception != null) {
    throw exception;
  }
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * Checks that file copy can occur.
 *
 * @param srcFile  Source {@link File}
 * @param destFile Destination {@link File}
 * @throws IOException if srcFile does not exist or is not a file or
 *                     srcFile and destFile are equal or cannot create ancestor directories.
 */
private static void checkFileCopy(final File srcFile, final File destFile) throws IOException {
  checkExists(srcFile);
  checkIsFile(srcFile);
  if (equals(srcFile, destFile)) {
    throw new IOException("Files '" + srcFile + "' and '" + destFile + "' are equal");
  }
  File destParent = destFile.getParentFile();
  if (destParent != null && !destParent.exists()) {
    checkCreateDirectory(destParent);
  }
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * Reads lines from source {@link File} with specified encoding and returns lines as {@link String}s in array.
 *
 * @param file     {@link File} to read.
 * @param encoding Endoing to use.
 * @return array of Strings which represents lines in the {@link File}.
 * @throws IOException if {@link File} does not exist or is not a {@link File} or there is an issue reading
 *                     the {@link File}.
 */
public static String[] readLines(final File file, final String encoding) throws IOException {
  checkExists(file);
  checkIsFile(file);
  List<String> list = new ArrayList<>();
  InputStream in = streamOf(file, encoding);
  try {
    BufferedReader br = new BufferedReader(StreamUtil.inputStreamReadeOf(in, encoding));
    String strLine;
    while ((strLine = br.readLine()) != null) {
      list.add(strLine);
    }
  } finally {
    StreamUtil.close(in);
  }
  return list.toArray(new String[0]);
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * Reads file content as char array.
 *
 * @param file     {@link File} to read.
 * @param encoding Encoding to use.
 * @return array of characters.
 * @throws IOException if something went wrong.
 */
public static char[] readChars(final File file, final String encoding) throws IOException {
  checkExists(file);
  checkIsFile(file);
  InputStream in = streamOf(file, encoding);
  try {
    return StreamUtil.readChars(in, encoding);
  } finally {
    StreamUtil.close(in);
  }
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * Reads UTF file content as char array.
 *
 * @param file {@link File} to read.
 * @return array of characters.
 * @throws IOException if something went wrong.
 */
public static char[] readUTFChars(final File file) throws IOException {
  checkExists(file);
  checkIsFile(file);
  UnicodeInputStream in = unicodeInputStreamOf(file);
  try {
    return StreamUtil.readChars(in, detectEncoding(in));
  } finally {
    StreamUtil.close(in);
  }
}

代码示例来源:origin: org.jodd/jodd-core

/**
 * Reads {@link File} content as {@link String} encoded in provided encoding.
 * For UTF encoded files, detects optional BOM characters.
 *
 * @param file     {@link File} to read.
 * @param encoding Encoding to use.
 * @return String representing {@link File} content.
 * @throws IOException if copy to {@link InputStream} errors.
 * @see #streamOf(File, String)
 * @see StreamUtil#copy(InputStream, String)
 */
public static String readString(final File file, final String encoding) throws IOException {
  checkExists(file);
  checkIsFile(file);
  InputStream in = streamOf(file, encoding);
  try {
    return StreamUtil.copy(in, encoding).toString();
  } finally {
    StreamUtil.close(in);
  }
}

相关文章