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

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

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

FileOutputStream.close介绍

[英]Closes this file output stream and releases any system resources associated with this stream. This file output stream may no longer be used for writing bytes.

If this stream has an associated channel then the channel is closed as well.
[中]关闭此文件输出流并释放与此流关联的所有系统资源。此文件输出流可能不再用于写入字节。
如果此流具有关联的通道,则该通道也将关闭。

代码示例

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

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

代码示例来源: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: stackoverflow.com

public void copy(File src, File dst) throws IOException {
  FileInputStream inStream = new FileInputStream(src);
  FileOutputStream outStream = new FileOutputStream(dst);
  FileChannel inChannel = inStream.getChannel();
  FileChannel outChannel = outStream.getChannel();
  inChannel.transferTo(0, inChannel.size(), outChannel);
  inStream.close();
  outStream.close();
}

代码示例来源:origin: skylot/jadx

private Object test(Object obj) {
    File file = new File("r");
    FileOutputStream output = null;
    try {
      output = new FileOutputStream(file);
      if (obj.equals("a")) {
        return new Object();
      } else {
        return null;
      }
    } catch (IOException e) {
      System.out.println("Exception");
      return null;
    } finally {
      if (output != null) {
        try {
          output.close();
        } catch (IOException e) {
          // Ignored
        }
      }
      file.delete();
    }
  }
}

代码示例来源:origin: android10/Android-CleanArchitecture

/**
 * Cache an element.
 *
 * @param bitmap The bitmap to be put in the cache.
 * @param fileName A string representing the name of the file to be cached.
 */
synchronized void put(Bitmap bitmap, String fileName) {
 final File file = buildFileFromFilename(fileName);
 if (!file.exists()) {
  try {
   final FileOutputStream fileOutputStream = new FileOutputStream(file);
   bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
   fileOutputStream.flush();
   fileOutputStream.close();
  } catch (IOException e) {
   Log.e(TAG, e.getMessage());
  }
 }
}

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

boolean success = false;
 // Encode the file as a PNG image.
 FileOutputStream outStream;
 try {
   outStream = new FileOutputStream(image);
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
   /* 100 to keep full quality of the image */
   outStream.flush();
   outStream.close();
   success = true;
 } catch (FileNotFoundException e) {
   e.printStackTrace();
 } catch (IOException e) {
   e.printStackTrace();
 }

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

/**
 * Implements the same behaviour as the "touch" utility on Unix. It creates
 * a new file with size 0 or, if the file exists already, it is opened and
 * closed without modifying it, but updating the file date and time.
 * <p>
 * NOTE: As from v1.3, this method throws an IOException if the last
 * modified date of the file cannot be set. Also, as from v1.3 this method
 * creates parent directories if they do not exist.
 *
 * @param file the File to touch
 * @throws IOException If an I/O problem occurs
 */
public static void touch(final File file) throws IOException {
  if (!file.exists()) {
    openOutputStream(file).close();
  }
  final boolean success = file.setLastModified(System.currentTimeMillis());
  if (!success) {
    throw new IOException("Unable to set the last modification time for " + file);
  }
}

代码示例来源: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: stackoverflow.com

FileInputStream in = new FileInputStream("First.properties");
Properties props = new Properties();
props.load(in);
in.close();

FileOutputStream out = new FileOutputStream("First.properties");
props.setProperty("country", "america");
props.store(out, null);
out.close();

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

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

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

private static void writeUriContentToTempFileIfNotExists(Context context, Uri uri, File tempFile)
    throws IOException {
  synchronized (tempFileWriteMonitor) {
    if (tempFile.exists()) {
      return;
    }
    FileOutputStream outputStream = new FileOutputStream(tempFile);
    InputStream inputStream = context.getContentResolver().openInputStream(uri);
    if (inputStream == null) {
      throw new IOException("Failed to resolve content at uri: " + uri);
    }
    IOUtils.copy(inputStream, outputStream);
    outputStream.close();
    IOUtils.closeQuietly(inputStream);
  }
}

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

String strMyImagePath = f.getAbsolutePath();
      FileOutputStream fos = null;
      try {
        fos = new FileOutputStream(f);
        bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);
        fos.flush();
        fos.close();
      //   MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
      }catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }

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

@Override
public void close() throws IOException {
  delegate.close();
  // if already closed, there should be no exception (see spec Closeable)
  if (temp.exists()) {
    Files.move(temp, file);
  }
}

代码示例来源:origin: junit-team/junit4

public static void savePreferences() throws IOException {
  FileOutputStream fos = new FileOutputStream(getPreferencesFile());
  try {
    getPreferences().store(fos, "");
  } finally {
    fos.close();
  }
}

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

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
              "/PhysicsSketchpad";
  File dir = new File(file_path);
if(!dir.exists())
  dir.mkdirs();
  File file = new File(dir, "sketchpad" + pad.t_id + ".png");
  FileOutputStream fOut = new FileOutputStream(file);

  bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
  fOut.flush();
  fOut.close();

代码示例来源:origin: yahoo/squidb

/**
 * Copy a file from one place to another
 */
public static void copyFile(File in, File out) throws IOException {
  FileInputStream fis = new FileInputStream(in);
  FileOutputStream fos = new FileOutputStream(out);
  try {
    copyStream(fis, fos);
  } finally {
    fis.close();
    fos.close();
  }
}

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

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

代码示例来源: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.");
  }
}

相关文章