java.io.FileNotFoundException类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(123)

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

FileNotFoundException介绍

[英]Thrown when a file specified by a program cannot be found.
[中]当找不到程序指定的文件时引发。

代码示例

代码示例来源:origin: redisson/redisson

/**
 * Reads lines from source files.
 */
public static String[] readLines(File file, String encoding) throws IOException {
  if (!file.exists()) {
    throw new FileNotFoundException(MSG_NOT_FOUND + file);
  }
  if (!file.isFile()) {
    throw new IOException(MSG_NOT_A_FILE + file);
  }
  List<String> list = new ArrayList<>();
  InputStream in = null;
  try {
    in = new FileInputStream(file);
    if (encoding.startsWith("UTF")) {
      in = new UnicodeInputStream(in, encoding);
    }
    BufferedReader br = new BufferedReader(new InputStreamReader(in, encoding));
    String strLine;
    while ((strLine = br.readLine()) != null)   {
      list.add(strLine);
    }
  } finally {
    StreamUtil.close(in);
  }
  return list.toArray(new String[list.size()]);
}

代码示例来源:origin: stanfordnlp/CoreNLP

public MorfetteFileIterator(String filename) {
 try {
  reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
  primeNext();
 } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
}

代码示例来源:origin: redwarp/9-Patch-Resizer

public static void copyfile(File input, File output) {
    try {
      InputStream in = new FileInputStream(input);
      OutputStream out = new FileOutputStream(output);

      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }
      in.close();
      out.close();
    } catch (FileNotFoundException ex) {
      System.out
          .println(ex.getMessage() + " in the specified directory.");
      System.exit(0);
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

private Set<String> loadMWEs() {
 Set<String> mweSet = Generics.newHashSet();  
 try {
  BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(mweFile), "UTF-8"));
  for (String line; (line = br.readLine()) != null;) {
   mweSet.add(line.trim());
  }
  br.close();
 
 } catch (UnsupportedEncodingException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 return mweSet;
}

代码示例来源:origin: netty/netty

FileNotFoundException fnf = new FileNotFoundException(fileName);
      ThrowableUtil.addSuppressedAndClear(fnf, suppressed);
      throw fnf;
    FileNotFoundException fnf = new FileNotFoundException(path);
    ThrowableUtil.addSuppressedAndClear(fnf, suppressed);
    throw fnf;
in = url.openStream();
out = new FileOutputStream(tmpFile);
if (TRY_TO_PATCH_SHADED_ID && PlatformDependent.isOsx() && !packagePrefix.isEmpty()) {
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(in.available());
  while ((length = in.read(buffer)) > 0) {
    byteArrayOutputStream.write(buffer, 0, length);
  out.write(bytes);
} else {
  while ((length = in.read(buffer)) > 0) {
    out.write(buffer, 0, length);
out.flush();

代码示例来源:origin: spotbugs/spotbugs

@ExpectWarning("RE,RV")
public static void main(String[] args) throws IOException {
  String name = "Mr. Ed";
  name = name.replaceAll(".", "s.");
  System.out.println(name);
  // FIXME:FindBugs only catches this error with name.indexOf(String)
  if (name.indexOf("s") > 0)
    System.out.println("Yay");
  else
    System.out.println("Boo");
  String result;
  try {
    BufferedReader findFiles = new BufferedReader(new FileReader("/mainList.txt"));
    if (findFiles.readLine() != null)
      result = findFiles.readLine();
    findFiles.close();
  } catch (FileNotFoundException e) {
    System.exit(7);
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
  LineNumberReader tmp = new LineNumberReader(new FileReader("/mainList.txt"));
  int count = 0;
  while (tmp.readLine() != null)
    count++;
  tmp.close();
}

代码示例来源:origin: stagemonitor/stagemonitor

private static void writeFromJarToTempFile(String path, File temp) throws IOException {
    // Prepare buffer for data copying
    byte[] buffer = new byte[1024];
    int readBytes;

    // Open and check input stream
    InputStream is = SigarNativeBindingLoader.class.getResourceAsStream(path);
    if (is == null) {
      throw new FileNotFoundException("File " + path + " was not found inside JAR.");
    }

    // Open output stream and copy data between source file in JAR and the temporary file
    OutputStream os = new FileOutputStream(temp);
    try {
      while ((readBytes = is.read(buffer)) != -1) {
        os.write(buffer, 0, readBytes);
      }
    } finally {
      // If read/write fails, close streams safely before throwing an exception
      os.close();
      is.close();
    }
  }
}

代码示例来源:origin: i2p/i2p.i2p

throw new FileNotFoundException("File not found: " + file);
long len = file.length();
if (len < BLOCK_LEN + HEADER_LEN + skip)
InputStream in = null;
try {
  in = new FileInputStream(file);
  if (skip > 0)
    DataHelper.skip(in, skip);
  return getComment(buffer);
} finally {
  if (in != null) try { in.close(); } catch (IOException ioe) {}

代码示例来源:origin: apache/hbase

/**
 * read the content of znode file, expects a single line.
 */
public static String readMyEphemeralNodeOnDisk() throws IOException {
 String fileName = getMyEphemeralNodeFileName();
 if (fileName == null){
  throw new FileNotFoundException("No filename; set environment variable HBASE_ZNODE_FILE");
 }
 FileReader znodeFile = new FileReader(fileName);
 BufferedReader br = null;
 try {
  br = new BufferedReader(znodeFile);
  String file_content = br.readLine();
  return file_content;
 } finally {
  if (br != null) br.close();
 }
}

代码示例来源:origin: iBotPeaches/Apktool

public static File extractToTmp(String resourcePath, String tmpPrefix, Class clazz) throws BrutException {
    try {
      InputStream in = clazz.getResourceAsStream(resourcePath);
      if (in == null) {
        throw new FileNotFoundException(resourcePath);
      }
      File fileOut = File.createTempFile(tmpPrefix, null);
      fileOut.deleteOnExit();
      OutputStream out = new FileOutputStream(fileOut);
      IOUtils.copy(in, out);
      in.close();
      out.close();
      return fileOut;
    } catch (IOException ex) {
      throw new BrutException("Could not extract resource: " + resourcePath, ex);
    }
  }
}

代码示例来源:origin: oracle/opengrok

BufferedReader input = null;
try {
  fin = new FileReader(file);
  input = new BufferedReader(fin);
  String line;
  StringBuilder contents = new StringBuilder();
  String EOL = System.getProperty("line.separator");
  while ((line = input.readLine()) != null) {
    contents.append(line).append(EOL);
} catch (java.io.FileNotFoundException e) {
  LOGGER.log(Level.WARNING, "failed to find file: {0}",
      e.getMessage());
} catch (java.io.IOException e) {
  LOGGER.log(Level.WARNING, "failed to read file: {0}",
  if (input != null) {
    try {
      input.close();
    } catch (Exception e) {
      fin.close();
    } catch (Exception e) {

代码示例来源:origin: neo4j/neo4j

if ( source == null )
  throw new FileNotFoundException( "Could not find resource '" + resource + "' to unzip" );
      try ( OutputStream file = new BufferedOutputStream( new FileOutputStream( new File( targetDirectory, entry.getName() ) ) ) )
          file.write( scratch, 0, read );
          toCopy -= read;
  source.close();

代码示例来源:origin: nanchen2251/CompressHelper

InputStream inputStream = null;
try {
  inputStream = new FileInputStream(filePath);
  BitmapFactory.decodeStream(inputStream, null, options);
  inputStream.close();
} catch (FileNotFoundException exception) {
  exception.printStackTrace();
} catch (IOException exception) {
  exception.printStackTrace();
  InputStream inputStream = null;
  try {
    inputStream = new FileInputStream(filePath);
    BitmapFactory.decodeStream(inputStream, null, options);
    inputStream.close();
  } catch (IOException exception) {
    exception.printStackTrace();

代码示例来源:origin: Javen205/IJPay

if (file.exists()) {
  try {
    in = new FileInputStream(file);
    properties = new Properties();
    properties.load(in);
    loadProperties(properties);
  } catch (FileNotFoundException e) {
    LogUtil.writeErrorLog(e.getMessage(), e);
  } catch (IOException e) {
    LogUtil.writeErrorLog(e.getMessage(), e);
    if (null != in) {
      try {
        in.close();
      } catch (IOException e) {
        LogUtil.writeErrorLog(e.getMessage(), e);

代码示例来源:origin: thymeleaf/thymeleaf

public Reader reader() throws IOException {
  final InputStream inputStream = this.servletContext.getResourceAsStream(this.path);
  if (inputStream == null) {
    throw new FileNotFoundException(String.format("ServletContext resource \"%s\" does not exist", this.path));
  }
  if (!StringUtils.isEmptyOrWhitespace(this.characterEncoding)) {
    return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream), this.characterEncoding));
  }
  return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream)));
}

代码示例来源:origin: jaydenxiao2016/AndroidFire

public static File from(Context context, Uri uri) throws IOException {
  InputStream inputStream = context.getContentResolver().openInputStream(uri);
  String fileName = getFileName(context, uri);
  String[] splitName = splitFileName(fileName);
  File tempFile = File.createTempFile(splitName[0], splitName[1]);
  tempFile = rename(tempFile, fileName);
  tempFile.deleteOnExit();
  FileOutputStream out = null;
  try {
    out = new FileOutputStream(tempFile);
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }
  if (inputStream != null) {
    copy(inputStream, out);
    inputStream.close();
  }
  if (out != null) {
    out.close();
  }
  return tempFile;
}

代码示例来源:origin: FudanNLP/fnlp

public SequenceReader(String file,boolean hasTarget, String charsetName) {
  this.hasTarget  = hasTarget;
  try {
    reader = new BufferedReader(new UnicodeReader(
        new FileInputStream(file), charsetName));
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: apache/nifi

fis = new FileInputStream(file);
  } catch (final FileNotFoundException fnfe) {
    fis = null;
    if (file.exists()) {
      try {
        fis = new FileInputStream(file);
        filename = baseName + extension;
        break openStream;
  throw new FileNotFoundException("Unable to locate file " + originalFile);
final String serializationName;
try {
  bufferedInStream.mark(4096);
  final InputStream in = filename.endsWith(".gz") ? new GZIPInputStream(bufferedInStream) : bufferedInStream;
  final DataInputStream dis = new DataInputStream(in);
  serializationName = dis.readUTF();
  bufferedInStream.reset();
} catch (final EOFException eof) {
  fis.close();
  return new EmptyRecordReader();
      throw new FileNotFoundException("Cannot create TOC Reader because the file " + tocFile + " does not exist");
      throw new FileNotFoundException("Cannot create TOC Reader because the file " + tocFile + " does not exist");

代码示例来源:origin: NLPchina/nlp-lang

public static void Writer(String path, String charEncoding, String content) {
  OutputStream fos = null;
  try {
    fos = new FileOutputStream(new File(path));
    fos.write(content.getBytes(charEncoding));
    fos.flush();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    close(fos);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Resolve the given resource URL to a {@code java.io.File},
 * i.e. to a file in the file system.
 * @param resourceUrl the resource URL to resolve
 * @param description a description of the original resource that
 * the URL was created for (for example, a class path location)
 * @return a corresponding File object
 * @throws FileNotFoundException if the URL cannot be resolved to
 * a file in the file system
 */
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
  Assert.notNull(resourceUrl, "Resource URL must not be null");
  if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
    throw new FileNotFoundException(
        description + " cannot be resolved to absolute file path " +
        "because it does not reside in the file system: " + resourceUrl);
  }
  try {
    return new File(toURI(resourceUrl).getSchemeSpecificPart());
  }
  catch (URISyntaxException ex) {
    // Fallback for URLs that are not valid URIs (should hardly ever happen).
    return new File(resourceUrl.getFile());
  }
}

相关文章