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

x33g5p2x  于2022-01-16 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(178)

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

FileInputStream介绍

[英]An input stream that reads bytes from a file.

File file = ...finally  
if (in != null)  
in.close(); 
} 
} 
}

This stream is not buffered. Most callers should wrap this stream with a BufferedInputStream.

Use FileReader to read characters, as opposed to bytes, from a file.
[中]从文件中读取字节的输入流

File file = ...finally  
if (in != null)  
in.close(); 
} 
} 
}

此流未被缓冲。大多数调用者都应该用BufferedInputStream包装这个流。
使用FileReader从文件中读取字符,而不是字节。

代码示例

canonical example by Tabnine

public void copyFile(File srcFile, File dstFile) throws IOException {
 try (FileInputStream fis = new FileInputStream(srcFile);
   FileOutputStream fos = new FileOutputStream(dstFile)) {
  int len;
  byte[] buffer = new byte[1024];
  while ((len = fis.read(buffer)) > 0) {
   fos.write(buffer, 0, len);
  }
 }
}

canonical example by Tabnine

public void zipFile(File srcFile, File zipFile) throws IOException {
 try (FileInputStream fis = new FileInputStream(srcFile);
   ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
  zos.putNextEntry(new ZipEntry(srcFile.getName()));
  int len;
  byte[] buffer = new byte[1024];
  while ((len = fis.read(buffer)) > 0) {
   zos.write(buffer, 0, len);
  }
  zos.closeEntry();
 }
}

代码示例来源:origin: stackoverflow.com

public void copy(File src, File dst) throws IOException {
  InputStream in = new FileInputStream(src);
  OutputStream out = new FileOutputStream(dst);

  // Transfer bytes from in to out
  byte[] buf = new byte[1024];
  int len;
  while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
  }
  in.close();
  out.close();
}

代码示例来源:origin: stackoverflow.com

FileInputStream inputStream = new FileInputStream("foo.txt");
try {
  String everything = IOUtils.toString(inputStream);
} finally {
  inputStream.close();
}

代码示例来源:origin: lets-blade/blade

public static void compressGZIP(File input, File output) throws IOException {
  try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) {
    try (FileInputStream in = new FileInputStream(input)) {
      byte[] buffer = new byte[1024];
      int    len;
      while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
      }
    }
  }
}

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

/**
 * Creates a new instance that fetches data from the specified file.
 *
 * @param chunkSize the number of bytes to fetch on each
 *                  {@link #readChunk(ChannelHandlerContext)} call
 */
public ChunkedNioFile(File in, int chunkSize) throws IOException {
  this(new FileInputStream(in).getChannel(), chunkSize);
}

代码示例来源:origin: hankcs/HanLP

public static String readTxt(File file, String charsetName) throws IOException
{
  FileInputStream is = new FileInputStream(file);
  byte[] targetArray = new byte[is.available()];
  int len;
  int off = 0;
  while ((len = is.read(targetArray, off, targetArray.length - off)) != -1 && off < targetArray.length)
  {
    off += len;
  }
  is.close();
  return new String(targetArray, charsetName);
}

代码示例来源:origin: google/guava

/**
 * Returns a buffered reader that reads from a file using the given character set.
 *
 * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link
 * java.nio.file.Files#newBufferedReader(java.nio.file.Path, Charset)}.
 *
 * @param file the file to read from
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @return the buffered reader
 */
public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException {
 checkNotNull(file);
 checkNotNull(charset);
 return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
}

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

public void chop() {
  File targetFile = new File(txnLogFile.getParentFile(), txnLogFile.getName() + ".chopped" + zxid);
  try (
      InputStream is = new BufferedInputStream(new FileInputStream(txnLogFile));
      OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile))
  ) {
    if (!LogChopper.chop(is, os, zxid)) {
      throw new TxnLogToolkitException(ExitCode.INVALID_INVOCATION.getValue(), "Failed to chop %s", txnLogFile.getName());
    }
  } catch (Exception e) {
    System.out.println("Got exception: " + e.getMessage());
  }
}

代码示例来源:origin: stackoverflow.com

String line;
try (
  InputStream fis = new FileInputStream("the_file_name");
  InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
  BufferedReader br = new BufferedReader(isr);
) {
  while ((line = br.readLine()) != null) {
    // Deal with the line
  }
}

代码示例来源:origin: Tencent/tinker

public static void bsdiff(File oldFile, File newFile, File diffFile) throws IOException {
  InputStream oldInputStream = new BufferedInputStream(new FileInputStream(oldFile));
  InputStream newInputStream = new BufferedInputStream(new FileInputStream(newFile));
  OutputStream diffOutputStream = new FileOutputStream(diffFile);
  try {
    byte[] diffBytes = bsdiff(oldInputStream, (int) oldFile.length(), newInputStream, (int) newFile.length());
    diffOutputStream.write(diffBytes);
  } finally {
    diffOutputStream.close();
  }
}

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

public void publicizeResources(File arscFile) throws AndrolibException {
  byte[] data = new byte[(int) arscFile.length()];
  try(InputStream in = new FileInputStream(arscFile);
    OutputStream out = new FileOutputStream(arscFile)) {
    in.read(data);
    publicizeResources(data);
    out.write(data);
  } catch (IOException ex){
    throw new AndrolibException(ex);
  }
}

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

public SetupPreferences () {		
  if (!file.exists()) return;
  InputStream in = null;
  try {
    in = new BufferedInputStream(new FileInputStream(file));
    properties.loadFromXML(in);
  } catch (Throwable t) {
    t.printStackTrace();
  } finally {
    if (in != null)
      try {
        in.close();
      } catch (IOException e) {
      }
  }
}

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

public static void cpdir(File src, File dest) throws BrutException {
  dest.mkdirs();
  File[] files = src.listFiles();
  for (int i = 0; i < files.length; i++) {
    File file = files[i];
    File destFile = new File(dest.getPath() + File.separatorChar
      + file.getName());
    if (file.isDirectory()) {
      cpdir(file, destFile);
      continue;
    }
    try {
      InputStream in = new FileInputStream(file);
      OutputStream out = new FileOutputStream(destFile);
      IOUtils.copy(in, out);
      in.close();
      out.close();
    } catch (IOException ex) {
      throw new BrutException("Could not copy file: " + file, ex);
    }
  }
}

代码示例来源:origin: commons-httpclient/commons-httpclient

public void writeRequest(final OutputStream out) throws IOException {
  byte[] tmp = new byte[4096];
  int i = 0;
  InputStream instream = new FileInputStream(this.file);
  try {
    while ((i = instream.read(tmp)) >= 0) {
      out.write(tmp, 0, i);
    }        
  } finally {
    instream.close();
  }
}

代码示例来源:origin: stackoverflow.com

try (
  InputStream in = new FileInputStream(src);
  OutputStream out = new FileOutputStream(dest))
{
 // code
}

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

if (extractedFile.exists()) {
  try {
    extractedCrc = crc(new FileInputStream(extractedFile));
  } catch (FileNotFoundException ignored) {
    input = readFile(sourcePath);
    extractedFile.getParentFile().mkdirs();
    output = new FileOutputStream(extractedFile);
    byte[] buffer = new byte[4096];
    while (true) {
      int length = input.read(buffer);
      if (length == -1) break;
      output.write(buffer, 0, length);

代码示例来源:origin: stackoverflow.com

public static int countLines(String filename) throws IOException {
  InputStream is = new BufferedInputStream(new FileInputStream(filename));
  try {
    byte[] c = new byte[1024];
    int count = 0;
    int readChars = 0;
    boolean empty = true;
    while ((readChars = is.read(c)) != -1) {
      empty = false;
      for (int i = 0; i < readChars; ++i) {
        if (c[i] == '\n') {
          ++count;
        }
      }
    }
    return (count == 0 && !empty) ? 1 : count;
  } finally {
    is.close();
  }
}

代码示例来源:origin: loklak/loklak_server

public static void gzip(File source, File dest, boolean deleteSource) throws IOException {
  byte[] buffer = new byte[2^20];
  GZIPOutputStream out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(dest), 65536)){{def.setLevel(Deflater.BEST_COMPRESSION);}};
  FileInputStream in = new FileInputStream(source);
  int l; while ((l = in.read(buffer)) > 0) out.write(buffer, 0, l);
  in.close(); out.finish(); out.close();
  if (deleteSource && dest.exists()) source.delete();
}

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

/**
 * Gets an input stream for the corresponding file.
 *
 * @param file The file to create the input stream from.
 * @return InputStream
 */
protected InputStream getInputStream(File file) throws IOException,
  FileNotFoundException {
 BufferedInputStream reader =
   new BufferedInputStream(new FileInputStream(file));
 return reader;
}

相关文章