Java Code Examples for java.io.FileOutputStream.close()

The following are Jave code examples for showing how to use close() of the java.io.FileOutputStream class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: iosched-reader   File: MapUtils.java   View Source Code Vote up 6 votes
/**
 * Copy the file from the assets to the map tiles directory if it was
 * shipped with the APK.
 */
public static boolean copyTileAsset(Context context, String filename) {
    if (!hasTileAsset(context, filename)) {
        // file does not exist as asset
        return false;
    }

    // copy file from asset to internal storage
    try {
        InputStream is = context.getAssets().open(TILE_PATH + File.separator + filename);
        File f = getTileFile(context, filename);
        FileOutputStream os = new FileOutputStream(f);

        byte[] buffer = new byte[1024];
        int dataSize;
        while ((dataSize = is.read(buffer)) > 0) {
            os.write(buffer, 0, dataSize);
        }
        os.close();
    } catch (IOException e) {
        return false;
    }

    return true;
}
 
Example 2
Project: Text-Rabbit   File: Game.java   View Source Code Vote up 6 votes
public void writeToFile(ArrayList<String> list, String fileName)
{
    try
    {
        FileOutputStream stream = this.openFileOutput(fileName,Context.MODE_PRIVATE);

        for (String item : list)
        {
            stream.write((item + "\n").getBytes());
        }
        stream.close();
    }
    catch (Exception e)
    {

    }
}
 
Example 3
Project: ramus   File: AbstractDataPlugin.java   View Source Code Vote up 6 votes
public boolean saveNamedDataToFile(final String name, final File file)
        throws IOException {
    synchronized (this) {
        if (updateNameData.indexOf(name) < 0)
            updateNameData.add(name);
        files.put(name, file);
        if (file.exists())
            return true;
        final InputStream is = getNamedData(name);
        if (is == null)
            return false;
        file.getParentFile().mkdirs();
        final FileOutputStream fos = new FileOutputStream(file);
        copyStream(is, fos);
        fos.close();
        return true;
    }
}
 
Example 4
Project: javaide   File: Jar.java   View Source Code Vote up 6 votes
public static void createJarArchive(JavaProjectFolder projectFolder) throws IOException {
    //input file
    File dirBuildClasses = projectFolder.getDirBuildClasses();
    File archiveFile = projectFolder.getOutJarArchive();


    // Open archive file
    FileOutputStream stream = new FileOutputStream(archiveFile);
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");

    //Create the jar file
    JarOutputStream out = new JarOutputStream(stream, manifest);

    //Add the files..
    if (dirBuildClasses.listFiles() != null) {
        for (File file : dirBuildClasses.listFiles()) {
            addzz(dirBuildClasses.getPath(), file, out);
        }
    }

    out.close();
    stream.close();
    System.out.println("Adding completed OK");
}
 
Example 5
Project: OpenDiabetes   File: TestKarl.java   View Source Code Vote up 6 votes
private static void copy(String src, String dest) throws IOException {

        File inputFile  = new File(src);
        File outputFile = new File(dest);

        if (!inputFile.exists()) {
            return;
        }

        FileInputStream  in  = new FileInputStream(inputFile);
        FileOutputStream out = new FileOutputStream(outputFile);
        int              c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

        in.close();
        out.close();
    }
 
Example 6
Project: Android-BitherCompress-master   File: NativeUtil.java   View Source Code Vote up 6 votes
/**
 * 4.尺寸压缩(通过缩放图片像素来减少图片占用内存大小)
 *
 * @param bmp
 * @param file
 */

public static void sizeCompress(Bitmap bmp, File file) {
    // 尺寸压缩倍数,值越大,图片尺寸越小
    int ratio = 8;
    // 压缩Bitmap到对应尺寸
    Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio);
    canvas.drawBitmap(bmp, null, rect, null);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // 把压缩后的数据存放到baos中
    result.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baos.toByteArray());
        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Project: monarch   File: FTableCommandsDUnitTest.java   View Source Code Vote up 6 votes
private String[] createPropertiesFiles() throws IOException {
  String[] files = new String[] {"storeProps.props", "readerProps.props", "writerProps.props"};
  FileOutputStream fosStoreProps = new FileOutputStream(files[0]);
  FileOutputStream fosReaderProps = new FileOutputStream(files[1]);
  FileOutputStream fosWriterProps = new FileOutputStream(files[2]);

  fosStoreProps.write(Bytes.toBytes("store.prop.prop1=value1\n"));
  fosStoreProps.write(Bytes.toBytes("store.prop.prop2=value11\n"));
  fosReaderProps.write(Bytes.toBytes("store.reader.prop.prop1=value2\n"));
  fosReaderProps.write(Bytes.toBytes("store.reader.prop.prop2=value22\n"));
  fosWriterProps.write(Bytes.toBytes("store.writer.prop.prop1=value3\n"));
  fosWriterProps.write(Bytes.toBytes("store.writer.prop.prop2=value33\n"));

  fosStoreProps.close();
  fosReaderProps.close();
  fosWriterProps.close();

  return files;
}
 
Example 8
Project: dev-courses   File: TestKarl.java   View Source Code Vote up 6 votes
private static void copy(String src, String dest) throws IOException {

        File inputFile  = new File(src);
        File outputFile = new File(dest);

        if (!inputFile.exists()) {
            return;
        }

        FileInputStream  in  = new FileInputStream(inputFile);
        FileOutputStream out = new FileOutputStream(outputFile);
        int              c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

        in.close();
        out.close();
    }
 
Example 9
Project: Mens   File: TestActivity1.java   View Source Code Vote up 6 votes
public void goToNextTest(boolean wasRight)  {
    //Create scores file because this is the first test
    try {
        FileOutputStream fos = openFileOutput("scores.txt", Context.MODE_APPEND);
        if (wasRight) {
            Toast.makeText(TestActivity1.this, "Correct", Toast.LENGTH_SHORT).show();
            fos.write("1\n".getBytes());
        } else {
            Toast.makeText(TestActivity1.this, "Wrong", Toast.LENGTH_SHORT).show();
            fos.write("0\n".getBytes());
        }
        fos.close();
        Intent test2 = new Intent(TestActivity1.this, TestActivity2.class);
        startActivity(test2);
    }catch (Exception e){
    }
    testTime = false;
    finish();

}
 
Example 10
Project: PlusGram   File: Utilities.java   View Source Code Vote up 5 votes
static int copyFile(File sourceFile, File destFile, boolean save) {
    int i=-1;
    try{
        if (!sourceFile.exists()) {
            return i+1;
        }
        if (!destFile.exists()) {
            if(save)i=i+2;
            destFile.createNewFile();
        }
        FileChannel source;
        FileChannel destination;
        FileInputStream fileInputStream = new FileInputStream(sourceFile);
        source = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream(destFile);
        destination = fileOutputStream.getChannel();
        if (destination != null && source != null) {
            destination.transferFrom(source, 0, source.size());
            i=2;
        }
        if (source != null) {
            source.close();
            i=3;
        }
        if (destination != null) {
            destination.close();
            i=4;
        }
        fileInputStream.close();
        fileOutputStream.close();
    }catch (Exception e)
    {
        System.err.println("Error saving preferences: " + e.getMessage());
        Log.e(e.getMessage(), e.toString());
    }
    return i;
}
 
Example 11
Project: MCRenderCrashFix   File: RenderClassesProcess.java   View Source Code Vote up 5 votes
public byte[] testOutput(byte[] pData,String pName){
    try {
        FileOutputStream F = new FileOutputStream(new File(pName));
        F.write(pData);
        F.close();
    }catch (Exception e){}
    return pData;
}
 
Example 12
Project: yyox   File: PictureUtil.java   View Source Code Vote up 5 votes
public static void saveTempPicture(String filePath,String fileTempPath){
    File file = new File(fileTempPath);
    Bitmap bmp = PictureUtil.getSmallBitmap(filePath,120,120);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // 把压缩后的数据存放到baos中
    bmp.compress(Bitmap.CompressFormat.PNG, 10, baos);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baos.toByteArray());
        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 13
Project: container   File: AtomicFile.java   View Source Code Vote up 5 votes
/**
 * Call when you have successfully finished writing to the stream
 * returned by {@link #startWrite()}.  This will close, sync, and
 * commit the new data.  The next attempt to read the atomic file
 * will return the new file stream.
 */
public void finishWrite(FileOutputStream str) {
    if (str != null) {
        sync(str);
        try {
            str.close();
            mBackupName.delete();
        } catch (IOException e) {
            Log.w("AtomicFile", "finishWrite: Got exception:", e);
        }
    }
}
 
Example 14
Project: DIA-Umpire-Maven   File: LCMSPeakBase.java   View Source Code Vote up 5 votes
private void FS_PeakClusterWrite() {
    try {
        Logger.getRootLogger().info("Writing PeakCluster serialization to file:" + FilenameUtils.getBaseName(ScanCollectionName) + "_PeakCluster.serFS...");
        FileOutputStream fout = new FileOutputStream(FilenameUtils.getFullPath(ParentmzXMLName) + FilenameUtils.getBaseName(ParentmzXMLName) + "_Peak/" + FilenameUtils.getBaseName(ScanCollectionName) + "_PeakCluster.serFS", false);
        FSTObjectOutput out = new FSTObjectOutput(fout);
        out.writeObject(PeakClusters);
        out.close();
        fout.close();
    } catch (Exception ex) {
        Logger.getRootLogger().error(ExceptionUtils.getStackTrace(ex));
        JavaSerializationPeakClusterWrite();
    }
}
 
Example 15
Project: FileOperation   File: Segment.java   View Source Code Vote up 5 votes
public static void fileSeperate(File file, int number, String suffix) throws Exception {

		destPath = file.getParentFile().toString() + File.separator
				+ file.getName().substring(0, file.getName().indexOf("."));
		destFile = new File(destPath);
		if (!destFile.exists()) {
			destFile.mkdirs();
		}
		outInfoFile = new File(destFile, file.getName().substring(0, file.getName().indexOf(".")) + suffix + ".txt");
		outInfo = new FileWriter(outInfoFile);
		inSource = new FileInputStream(file);
		long fileLength = file.length();
		long size = fileLength / number + 1;
		long eIndex = 0;
		long bIndex = 0;
		for (int i = 0; i < number; i++) {
			outFile = new File(destFile,
					file.getName().substring(0, file.getName().indexOf(".")) + "part" + i + suffix);
			outDest = new FileOutputStream(outFile);
			eIndex += size;
			eIndex = (eIndex > fileLength) ? fileLength : eIndex;
			while (bIndex < eIndex) {
				outDest.write(inSource.read());
				bIndex++;
			}
			outInfo.write(outFile.getAbsolutePath());
			outInfo.write(System.getProperty("line.separator"));
			outInfo.flush();
		}
		outDest.close();
		outInfo.close();
		inSource.close();
	}
 
Example 16
Project: framework   File: Qrcode.java   View Source Code Vote up 5 votes
/**
 * 换取二维码
 *
 * @param qrcodeFile 二维码存储路径
 */
public void getQrcode(String qrcodeFile) {
	try {
		byte[] b = HttpUtils.getFile(SHOWQRCODE_POST_URL + URLEncoder.encode(ticket, "UTF-8"));
		File file = new File(qrcodeFile);
		FileOutputStream fStream = new FileOutputStream(file);
		fStream.write(b);
		fStream.flush();
		fStream.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 17
Project: android_camera_experiment   File: PreviewActivity.java   View Source Code Vote up 5 votes
private void saveCroppedImage(Uri croppedFileUri) {
    try {
        File saveFile = new File(previewFilePath);
        FileInputStream inStream = new FileInputStream(new File(croppedFileUri.getPath()));
        FileOutputStream outStream = new FileOutputStream(saveFile);
        FileChannel inChannel = inStream.getChannel();
        FileChannel outChannel = outStream.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        inStream.close();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 18
Project: cemu_UI   File: CloudController.java   View Source Code Vote up 5 votes
private File zipSavegames(String cemu_UIDirectory, String cemuDirectory) throws Exception {
	long unixTimestamp = Instant.now().getEpochSecond();
	FileOutputStream fos = new FileOutputStream(cemu_UIDirectory + "/" + unixTimestamp + ".zip");
	ZipOutputStream zos = new ZipOutputStream(fos);
	addDirToZipArchive(zos, new File(cemuDirectory + "/mlc01/usr/save"), null);
	zos.flush();
	fos.flush();
	zos.close();
	fos.close();
	return new File(cemu_UIDirectory + "/" + unixTimestamp + ".zip");
}
 
Example 19
Project: Cable-Android   File: PreKeyUtil.java   View Source Code Vote up 4 votes
private static synchronized void setSignedPreKeyIndex(Context context, SignedPreKeyIndex index) throws IOException {
  File             indexFile = new File(getSignedPreKeysDirectory(context), SignedPreKeyIndex.FILE_NAME);
  FileOutputStream fout     = new FileOutputStream(indexFile);
  fout.write(JsonUtils.toJson(index).getBytes());
  fout.close();
}
 
Example 20
Project: Plamber-Android   File: UploadOfflineDialog.java   View Source Code Vote up 4 votes
private void saveByteAsImage(byte[] bytes) throws IOException {
    FileOutputStream stream = new FileOutputStream(utils.getPngFileWithPath(bookId));
    stream.write(bytes);
    stream.close();

}