java.io.FileOutputStream.<init>()方法的使用及代码示例

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

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

FileOutputStream.<init>介绍

[英]Constructs a new FileOutputStream that writes to file. The file will be truncated if it exists, and created if it doesn't exist.
[中]构造写入文件的新FileOutputStream。如果文件存在,将被截断;如果文件不存在,将被创建。

代码示例

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

// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);

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

FileOutputStream out = null;
try {
  out = new FileOutputStream(filename);
  bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
  // PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
  e.printStackTrace();
} finally {
  try {
    if (out != null) {
      out.close();
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
}

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

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

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

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());

// remember close de FileOutput
fo.close();

代码示例来源: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: eclipse-vertx/vert.x

private File setupFile(String testDir, String fileName, String content) throws Exception {
 File file = new File(testDir, fileName);
 if (file.exists()) {
  file.delete();
 }
 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
 out.write(content);
 out.close();
 return file;
}

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

private String extractLibrary (String sharedLibName) {
  String srcCrc = crc(JniGenSharedLibraryLoader.class.getResourceAsStream("/" + sharedLibName));
  File nativesDir = new File(System.getProperty("java.io.tmpdir") + "/jnigen/" + srcCrc);
  File nativeFile = new File(nativesDir, sharedLibName);
  if (nativeFile.exists()) {
    try {
      extractedCrc = crc(new FileInputStream(nativeFile));
    } catch (FileNotFoundException ignored) {
        input = getFromJar(nativesJar, sharedLibName);
      if (input == null) return null;
      nativeFile.getParentFile().mkdirs();
      FileOutputStream output = new FileOutputStream(nativeFile);
      byte[] buffer = new byte[4096];
      while (true) {
        int length = input.read(buffer);
        if (length == -1) break;
        output.write(buffer, 0, length);
      output.close();
    } catch (IOException ex) {
      ex.printStackTrace();
  return nativeFile.exists() ? nativeFile.getAbsolutePath() : null;

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

public static void copyFileUsingStream(File source, File dest) throws IOException {
  FileInputStream is = null;
  FileOutputStream os = null;
  File parent = dest.getParentFile();
  if (parent != null && (!parent.exists())) {
    parent.mkdirs();
  }
  try {
    is = new FileInputStream(source);
    os = new FileOutputStream(dest, false);
    byte[] buffer = new byte[TypedValue.BUFFER_SIZE];
    int length;
    while ((length = is.read(buffer)) > 0) {
      os.write(buffer, 0, length);
    }
  } finally {
    StreamUtil.closeQuietly(os);
    StreamUtil.closeQuietly(is);
  }
}

代码示例来源:origin: apache/incubator-druid

private OutputStreamWriter getFileWriter() throws FileNotFoundException
{
 return new OutputStreamWriter(
   new FileOutputStream(new File(baseDir, filePattern.print(currentDay)), true),
   StandardCharsets.UTF_8
 );
}

代码示例来源:origin: org.testng/testng

public static void copyFile(InputStream from, File to) throws IOException {
 if (! to.getParentFile().exists()) {
  to.getParentFile().mkdirs();
 }
 try (OutputStream os = new FileOutputStream(to)) {
  byte[] buffer = new byte[65536];
  int count = from.read(buffer);
  while (count > 0) {
   os.write(buffer, 0, count);
   count = from.read(buffer);
  }
 }
}

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

FileOutputStream fos = new FileOutputStream("pathname");
fos.write(myByteArray);
fos.close();

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

public static void copyFile( File from, File to ) throws IOException {

  if ( !to.exists() ) { to.createNewFile(); }

  try (
    FileChannel in = new FileInputStream( from ).getChannel();
    FileChannel out = new FileOutputStream( to ).getChannel() ) {

    out.transferFrom( in, 0, in.size() );
  }
}

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

protected static void outChars(File dest, char[] data, String encoding, boolean append) throws IOException {
  if (dest.exists()) {
    if (!dest.isFile()) {
      throw new IOException(MSG_NOT_A_FILE + dest);
    }
  }
  Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest, append), encoding));
  try {
    out.write(data);
  } finally {
    StreamUtil.close(out);
  }
}

代码示例来源:origin: thinkaurelius/titan

public static ElasticsearchStatus write(String path, int pid) {
  File f = new File(path);
  FileOutputStream fos = null;
  try {
    fos = new FileOutputStream(path);
    fos.write(String.format("%d", pid).getBytes());
  } catch (IOException e) {
    throw new RuntimeException(e);
  } finally {
    IOUtils.closeQuietly(fos);
  }
  return new ElasticsearchStatus(f, pid);
}

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

private Revision createNewFileAndCheckIn(File directory) {
  try {
    new FileOutputStream(new File(directory, "test.txt")).close();
    addremove(directory);
    commit("created test.txt", directory);
  } catch (IOException e) {
    e.printStackTrace();
  }
  return latestRevisionOf();
}

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

protected static void touchFile(File file) throws IOException {
  if (!file.exists()) {
    new FileOutputStream(file).close();
  }
  if (!file.setLastModified(System.currentTimeMillis())) {
    throw new IOException("Could not touch the file.");
  }
}

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

private void addHeaderToManifest(String header, String value) throws IOException {
  FileInputStream manifestInputStream = new FileInputStream(manifestFile);
  Manifest manifest = new Manifest(manifestInputStream);
  Attributes entries = manifest.getMainAttributes();
  entries.put(new Attributes.Name(header), value);
  FileOutputStream manifestOutputStream = new FileOutputStream(manifestFile, false);
  manifest.write(manifestOutputStream);
  manifestOutputStream.close();
  manifestInputStream.close();
}

代码示例来源:origin: apache/incubator-druid

public File createSegmentDescriptorFile(final ObjectMapper jsonMapper, final DataSegment segment) throws
                                                 IOException
{
 File descriptorFile = File.createTempFile("descriptor", ".json");
 try (FileOutputStream stream = new FileOutputStream(descriptorFile)) {
  stream.write(jsonMapper.writeValueAsBytes(segment));
 }
 return descriptorFile;
}

相关文章