Tabnine Logo
FileInputStream
Code IndexAdd Tabnine to your IDE (free)

How to use
FileInputStream
in
java.io

Best Java code snippets using java.io.FileInputStream (Showing top 20 results out of 116,172)

Refine searchRefine arrow

  • FileOutputStream
  • InputStreamReader
  • BufferedReader
  • BufferedInputStream
  • InputStream
  • OutputStream
  • BufferedOutputStream
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;
}
java.ioFileInputStream

Javadoc

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.

Most used methods

  • <init>
    Creates a FileInputStream by opening a connection to an actual file, the file named by the path name
  • close
    Closes this file input stream and releases any system resources associated with the stream. If this
  • read
    Reads up to len bytes of data from this input stream into an array of bytes. If len is not zero, th
  • getChannel
    Returns a read-only FileChannel that shares its position with this stream.
  • available
    Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this in
  • skip
    Skips over and discards n bytes of data from the input stream.The skip method may, for a variety of
  • getFD
    Returns the FileDescriptor object that represents the connection to the actual file in the file syst
  • mark
  • markSupported
  • reset
  • finalize
    Ensures that the close method of this file input stream is called when there are no more references
  • close0
  • finalize,
  • close0,
  • isRunningFinalize,
  • open,
  • readBytes,
  • transferTo

Popular in Java

  • Updating database using SQL prepared statement
  • getSharedPreferences (Context)
  • compareTo (BigDecimal)
  • notifyDataSetChanged (ArrayAdapter)
  • HttpServer (com.sun.net.httpserver)
    This class implements a simple HTTP server. A HttpServer is bound to an IP address and port number a
  • BufferedWriter (java.io)
    Wraps an existing Writer and buffers the output. Expensive interaction with the underlying reader is
  • Queue (java.util)
    A collection designed for holding elements prior to processing. Besides basic java.util.Collection o
  • AtomicInteger (java.util.concurrent.atomic)
    An int value that may be updated atomically. See the java.util.concurrent.atomic package specificati
  • Reference (javax.naming)
  • BasicDataSource (org.apache.commons.dbcp)
    Basic implementation of javax.sql.DataSource that is configured via JavaBeans properties. This is no
  • Best IntelliJ plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now