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

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

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

FileOutputStream.write介绍

[英]Writes the specified byte to this file output stream. Implements the write method of OutputStream.
[中]将指定的字节写入此文件输出流。实现OutputStreamwrite方法。

代码示例

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);
  }
 }
}

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

FileOutputStream fos = new FileOutputStream("pathname");
fos.write(myByteArray);
fos.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;
}

代码示例来源: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: commons-io/commons-io

void openOutputStream_noParent(final boolean createFile) throws Exception {
  final File file = new File("test.txt");
  assertNull(file.getParentFile());
  try {
    if (createFile) {
      TestUtils.createLineBasedFile(file, new String[]{"Hello"});
    }
    try (FileOutputStream out = FileUtils.openOutputStream(file)) {
      out.write(0);
    }
    assertTrue(file.exists());
  } finally {
    if (!file.delete()) {
      file.deleteOnExit();
    }
  }
}

代码示例来源: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: redisson/redisson

protected static void outString(File dest, String data, String encoding, boolean append) throws IOException {
  if (dest.exists()) {
    if (!dest.isFile()) {
      throw new IOException(MSG_NOT_A_FILE + dest);
    }
  }
  FileOutputStream out = null;
  try {
    out = new FileOutputStream(dest, append);
    out.write(data.getBytes(encoding));
  } finally {
    StreamUtil.close(out);
  }
}

代码示例来源:origin: deathmarine/Luyten

private void doSaveUnknownFile(File inFile, File outFile) throws Exception {
  try (FileInputStream in = new FileInputStream(inFile); FileOutputStream out = new FileOutputStream(outFile);) {
    System.out.println("[SaveAll]: " + inFile.getName() + " -> " + outFile.getName());
    byte data[] = new byte[1024];
    int count;
    while ((count = in.read(data, 0, 1024)) != -1) {
      out.write(data, 0, count);
    }
  }
}

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

public static void writeResourceToFile(File file, String resourceName, Class<?> clasz) throws IOException {
 InputStream inputStream = clasz.getResourceAsStream("/" + resourceName);
 if (inputStream == null) {
  LOG.error("Couldn't find resource on the class path: " + resourceName);
  return;
 }
 try {
  try (FileOutputStream outputStream = new FileOutputStream(file)) {
   int nread;
   byte[] buffer = new byte[4096];
   while (0 < (nread = inputStream.read(buffer))) {
    outputStream.write(buffer, 0, nread);
   }
  }
 } finally {
  inputStream.close();
 }
}

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

InputStream ins = context.getResources().openRawResource (R.raw.FILENAME)
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(buffer);
fos.close();

File file = getFileStreamPath (FILENAME);
file.setExecutable(true);

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

//create a file to write bitmap data
File f = new File(context.getCacheDir(), filename);
f.createNewFile();

//Convert bitmap to byte array
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();

//write the bytes in file
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();

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

@Test
public void test_openOutputStream_notExists() throws Exception {
  final File file = new File(getTestDirectory(), "a/test.txt");
  try (FileOutputStream out = FileUtils.openOutputStream(file)) {
    out.write(0);
  }
  assertTrue(file.exists());
}

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

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

代码示例来源:origin: pxb1988/dex2jar

public static void writeFile(byte[] data, File out) throws IOException {
  FileOutputStream fos = new FileOutputStream(out);
  fos.write(data);
  fos.close();
}

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

protected static void outBytes(File dest, byte[] data, int off, int len, boolean append) throws IOException {
  if (dest.exists()) {
    if (!dest.isFile()) {
      throw new IOException(MSG_NOT_A_FILE + dest);
    }
  }
  FileOutputStream out = null;
  try {
    out = new FileOutputStream(dest, append);
    out.write(data, off, len);
  } finally {
    StreamUtil.close(out);
  }
}

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

/**
  * Checks if writing {@code len} bytes would go over threshold, and switches to file buffering if
  * so.
  */
 private void update(int len) throws IOException {
  if (file == null && (memory.getCount() + len > fileThreshold)) {
   File temp = File.createTempFile("FileBackedOutputStream", null);
   if (resetOnFinalize) {
    // Finalizers are not guaranteed to be called on system shutdown;
    // this is insurance.
    temp.deleteOnExit();
   }
   FileOutputStream transfer = new FileOutputStream(temp);
   transfer.write(memory.getBuffer(), 0, memory.getCount());
   transfer.flush();

   // We've successfully transferred the data; switch to writing to file
   out = transfer;
   file = temp;
   memory = null;
  }
 }
}

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

File f = new File(getCacheDir()+"/m1.map");
if (!f.exists()) try {
 InputStream is = getAssets().open("m1.map");
 int size = is.available();
 byte[] buffer = new byte[size];
 is.read(buffer);
 is.close();
 FileOutputStream fos = new FileOutputStream(f);
 fos.write(buffer);
 fos.close();
} catch (Exception e) { throw new RuntimeException(e); }
mapView.setMapFile(f.getPath());

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

@Test
public void test_openOutputStream_exists() throws Exception {
  final File file = new File(getTestDirectory(), "test.txt");
  TestUtils.createLineBasedFile(file, new String[]{"Hello"});
  try (FileOutputStream out = FileUtils.openOutputStream(file)) {
    out.write(0);
  }
  assertTrue(file.exists());
}

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

private static void generateRandomDirs(File dir, int numFiles, int numDirs, int depth) throws IOException {
  // generate the random files
  for (int i = 0; i < numFiles; i++) {
    File file = new File(dir, new AbstractID().toString());
    try (FileOutputStream out = new FileOutputStream(file)) {
      out.write(1);
    }
  }
  if (depth > 0) {
    // generate the directories
    for (int i = 0; i < numDirs; i++) {
      File subdir = new File(dir, new AbstractID().toString());
      assertTrue(subdir.mkdir());
      generateRandomDirs(subdir, numFiles, numDirs, depth - 1);
    }
  }
}

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

FileOutputStream stream = new FileOutputStream(path);
try {
  stream.write(bytes);
} finally {
  stream.close();
}

相关文章