当前位置: 首页>>代码示例>>Java>>正文


Java FileOutputStream类代码示例

本文整理汇总了Java中java.io.FileOutputStream的典型用法代码示例。如果您正苦于以下问题:Java FileOutputStream类的具体用法?Java FileOutputStream怎么用?Java FileOutputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


FileOutputStream类属于java.io包,在下文中一共展示了FileOutputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: exportResource

import java.io.FileOutputStream; //导入依赖的package包/类
public static String exportResource(Context context, int resourceId, String dirname) {
    String fullname = context.getResources().getString(resourceId);
    String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
    try {
        InputStream is = context.getResources().openRawResource(resourceId);
        File resDir = context.getDir(dirname, Context.MODE_PRIVATE);
        File resFile = new File(resDir, resName);

        FileOutputStream os = new FileOutputStream(resFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        return resFile.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CvException("Failed to export resource " + resName
                + ". Exception thrown: " + e);
    }
}
 
开发者ID:jorenham,项目名称:fingerblox,代码行数:26,代码来源:Utils.java

示例2: setUp

import java.io.FileOutputStream; //导入依赖的package包/类
static void setUp() throws Exception {
    testarray = new float[1024];
    for (int i = 0; i < 1024; i++) {
        double ii = i / 1024.0;
        ii = ii * ii;
        testarray[i] = (float)Math.sin(10*ii*2*Math.PI);
        testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);
        testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);
        testarray[i] *= 0.3;
    }
    test_byte_array = new byte[testarray.length*2];
    AudioFloatConverter.getConverter(format).toByteArray(testarray, test_byte_array);
    test_file = File.createTempFile("test", ".raw");
    FileOutputStream fos = new FileOutputStream(test_file);
    fos.write(test_byte_array);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:17,代码来源:GetRoot.java

示例3: copyFile

import java.io.FileOutputStream; //导入依赖的package包/类
public static long copyFile(File f1, File f2) throws Exception {
	long time = new Date().getTime();
	int length = 2097152;
	FileInputStream in = new FileInputStream(f1);
	FileOutputStream out = new FileOutputStream(f2);
	byte[] buffer = new byte[length];
	while (true) {
		int ins = in.read(buffer);
		if (ins == -1) {
			in.close();
			out.flush();
			out.close();
			return new Date().getTime() - time;
		} else
			out.write(buffer, 0, ins);
	}
}
 
开发者ID:cnldw,项目名称:APITools,代码行数:18,代码来源:PubUtils.java

示例4: createBlobFile

import java.io.FileOutputStream; //导入依赖的package包/类
private void createBlobFile(int size) throws Exception {
    if (testBlobFile != null && testBlobFile.length() != size) {
        testBlobFile.delete();
    }

    testBlobFile = File.createTempFile(TEST_BLOB_FILE_PREFIX, ".dat");
    testBlobFile.deleteOnExit();

    // TODO: following cleanup doesn't work correctly during concurrent execution of testsuite 
    // cleanupTempFiles(testBlobFile, TEST_BLOB_FILE_PREFIX);

    BufferedOutputStream bOut = new BufferedOutputStream(new FileOutputStream(testBlobFile));

    int dataRange = Byte.MAX_VALUE - Byte.MIN_VALUE;

    for (int i = 0; i < size; i++) {
        bOut.write((byte) ((Math.random() * dataRange) + Byte.MIN_VALUE));
    }

    bOut.flush();
    bOut.close();
}
 
开发者ID:rafallis,项目名称:BibliotecaPS,代码行数:23,代码来源:BlobTest.java

示例5: copyTestImageToSdCard

import java.io.FileOutputStream; //导入依赖的package包/类
private void copyTestImageToSdCard(final File testImageOnSdCard) {
	new Thread(new Runnable() {
		@Override
		public void run() {
			try {
				InputStream is = getAssets().open(TEST_FILE_NAME);
				FileOutputStream fos = new FileOutputStream(testImageOnSdCard);
				byte[] buffer = new byte[8192];
				int read;
				try {
					while ((read = is.read(buffer)) != -1) {
						fos.write(buffer, 0, read);
					}
				} finally {
					fos.flush();
					fos.close();
					is.close();
				}
			} catch (IOException e) {
				L.w("Can't copy test image onto SD card");
			}
		}
	}).start();
}
 
开发者ID:siwangqishiq,项目名称:ImageLoaderSupportGif,代码行数:25,代码来源:HomeActivity.java

示例6: saveDocument

import java.io.FileOutputStream; //导入依赖的package包/类
public static void saveDocument(Document doc, String filePath) {
    /**
     * @todo: Configurable parameters
     */
    try {
        /*The XOM bug: reserved characters are not escaped*/
        //Serializer serializer = new Serializer(new FileOutputStream(filePath), "UTF-8");
        //serializer.write(doc);
        OutputStreamWriter fw =
            new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
        fw.write(doc.toXML());
        fw.flush();
        fw.close();
    }
    catch (IOException ex) {
        new ExceptionDialog(
            ex,
            "Failed to write a document to " + filePath,
            "");
    }
}
 
开发者ID:ser316asu,项目名称:SER316-Ingolstadt,代码行数:22,代码来源:FileStorage.java

示例7: put

import java.io.FileOutputStream; //导入依赖的package包/类
public synchronized void put(String key, Entry entry) {
    pruneIfNeeded(entry.data.length);
    File file = getFileForKey(key);
    try {
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
        CacheHeader e = new CacheHeader(key, entry);
        if (e.writeHeader(fos)) {
            fos.write(entry.data);
            fos.close();
            putEntry(key, e);
        } else {
            fos.close();
            VolleyLog.d("Failed to write header for %s", file.getAbsolutePath());
            throw new IOException();
        }
    } catch (IOException e2) {
        if (!file.delete()) {
            VolleyLog.d("Could not clean up file %s", file.getAbsolutePath());
        }
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:22,代码来源:DiskBasedCache.java

示例8: main

import java.io.FileOutputStream; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
    URLConnection conn = B7050028.class.getResource("B7050028.class").openConnection();
    int len = conn.getContentLength();
    byte[] data = new byte[len];
    InputStream is = conn.getInputStream();
    is.read(data);
    is.close();
    conn.setDefaultUseCaches(false);
    File jar = File.createTempFile("B7050028", ".jar");
    jar.deleteOnExit();
    OutputStream os = new FileOutputStream(jar);
    ZipOutputStream zos = new ZipOutputStream(os);
    ZipEntry ze = new ZipEntry("B7050028.class");
    ze.setMethod(ZipEntry.STORED);
    ze.setSize(len);
    CRC32 crc = new CRC32();
    crc.update(data);
    ze.setCrc(crc.getValue());
    zos.putNextEntry(ze);
    zos.write(data, 0, len);
    zos.closeEntry();
    zos.finish();
    zos.close();
    os.close();
    System.out.println(new URLClassLoader(new URL[] {new URL("jar:" + jar.toURI() + "!/")}, ClassLoader.getSystemClassLoader().getParent()).loadClass(B7050028.class.getName()));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:B7050028.java

示例9: save

import java.io.FileOutputStream; //导入依赖的package包/类
private void save(Context context) {
    byte[] byteBuffer = new byte[Double.SIZE / Byte.SIZE];

    try {
        FileOutputStream stream = context.openFileOutput(ScoresFileName, Context.MODE_PRIVATE);

        ByteBuffer.wrap(byteBuffer).putDouble(BuildConfig.VERSION_CODE);
        stream.write(byteBuffer);

        for (Map.Entry<Integer, List<ScoreEntry>> group : this.scores.entrySet()) {
            stream.write(group.getKey());
            stream.write(group.getValue().size());

            for (ScoreEntry score : group.getValue()) {
                double[] data = score.saveData();

                for (double val : data) {
                    ByteBuffer.wrap(byteBuffer).putDouble(val);
                    stream.write(byteBuffer);
                }
            }
        }

        stream.close();
        Log.i("HighScores", "Saved");
    } catch (Exception e) {
        Log.e("HighScores", "Autosave failed", e);
    }
}
 
开发者ID:subchannel13,项目名称:EnchantedFortress,代码行数:30,代码来源:HighScores.java

示例10: setUp

import java.io.FileOutputStream; //导入依赖的package包/类
@Override
protected void setUp() throws Exception {
    super.setUp();
    mOldVmPolicy = StrictMode.getVmPolicy();
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                                   .detectLeakedClosableObjects()
                                   .penaltyLog()
                                   .penaltyDeath()
                                   .build());
    mTestFramework = startCronetTestFramework();
    assertTrue(NativeTestServer.startNativeTestServer(getContext()));
    // Add url interceptors after native application context is initialized.
    MockUrlRequestJobFactory.setUp();
    mFile = new File(getContext().getCacheDir().getPath() + "/tmpfile");
    FileOutputStream fileOutputStream = new FileOutputStream(mFile);
    try {
        fileOutputStream.write(LOREM.getBytes("UTF-8"));
    } finally {
        fileOutputStream.close();
    }
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:22,代码来源:UploadDataProvidersTest.java

示例11: writeBinaryCheckpoints

import java.io.FileOutputStream; //导入依赖的package包/类
private static void writeBinaryCheckpoints(TreeMap<Integer, StoredBlock> checkpoints, File file) throws Exception {
    final FileOutputStream fileOutputStream = new FileOutputStream(file, false);
    MessageDigest digest = Sha256Hash.newDigest();
    final DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest);
    digestOutputStream.on(false);
    final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream);
    dataOutputStream.writeBytes("CHECKPOINTS 1");
    dataOutputStream.writeInt(0);  // Number of signatures to read. Do this later.
    digestOutputStream.on(true);
    dataOutputStream.writeInt(checkpoints.size());
    ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
    for (StoredBlock block : checkpoints.values()) {
        block.serializeCompact(buffer);
        dataOutputStream.write(buffer.array());
        buffer.position(0);
    }
    dataOutputStream.close();
    Sha256Hash checkpointsHash = Sha256Hash.wrap(digest.digest());
    System.out.println("Hash of checkpoints data is " + checkpointsHash);
    digestOutputStream.close();
    fileOutputStream.close();
    System.out.println("Checkpoints written to '" + file.getCanonicalPath() + "'.");
}
 
开发者ID:creativechain,项目名称:creacoinj,代码行数:24,代码来源:BuildCheckpoints.java

示例12: onCreate

import java.io.FileOutputStream; //导入依赖的package包/类
@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    momentCheckBox = (CheckBox) findViewById(R.id.shareToMomentCheckBox);
    qzoneCheckBox = (CheckBox) findViewById(R.id.shareToQzoneCheckBox);

    icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    if (!ICON_FILE.exists()) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(ICON_FILE);
            icon.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
开发者ID:szitguy,项目名称:Allshare,代码行数:18,代码来源:MainActivity.java

示例13: getResponseData

import java.io.FileOutputStream; //导入依赖的package包/类
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        long contentLength = entity.getContentLength() + current;
        FileOutputStream buffer = new FileOutputStream(getTargetFile(), append);
        if (instream != null) {
            try {
                byte[] tmp = new byte[BUFFER_SIZE];
                int l;
                while (current < contentLength && (l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
                    current += l;
                    buffer.write(tmp, 0, l);
                    sendProgressMessage((int) current, (int) contentLength);
                }
            } finally {
                instream.close();
                buffer.flush();
                buffer.close();
            }
        }
    }
    return null;
}
 
开发者ID:benniaobuguai,项目名称:android-project-gallery,代码行数:25,代码来源:RangeFileAsyncHttpResponseHandler.java

示例14: saveToSDCard

import java.io.FileOutputStream; //导入依赖的package包/类
public static String saveToSDCard(byte[] data,Context context,String path) throws IOException {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String filename = "IMG_" + format.format(date) + ".jpg";
        File fileFolder = new File(path);
        if (!fileFolder.exists()) {
            fileFolder.mkdirs();
        }
        File jpgFile = new File(fileFolder, filename);
        FileOutputStream outputStream = new FileOutputStream(jpgFile); //
        //刷新相册
        MediaScannerConnection.scanFile(context,
                new String[]{jpgFile.getAbsolutePath()}, null, null);

        outputStream.write(data);
        outputStream.close();
//        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//        Uri uri = Uri.fromFile(new File(Environment
//                .getExternalStorageDirectory() + "/DeepbayPicture/" + filename));
//        intent.setData(uri);
//        mContext.sendBroadcast(intent);
        return jpgFile.getAbsolutePath();
    }
 
开发者ID:lwd1815,项目名称:Selector,代码行数:24,代码来源:ImageCacheUtils.java

示例15: finalizeCnf

import java.io.FileOutputStream; //导入依赖的package包/类
private void finalizeCnf() {
    try {
        free();
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(cnfFile));
        File tmp = new File(cnfFile + ".tmp"); 
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmp)); 
        byte[] header = String.format("p cnf %s %s\n", vars, clauses).getBytes();
        out.write(header);
        int ch;
        while ((ch = in.read()) != -1) {
            out.write(ch);
        }
        in.close();
        out.close();
        tmp.renameTo(cnfFile);
    } catch (IOException e) {
        throw new RuntimeException("could not preprend header to cnf file: " + cnfFile.getAbsolutePath(), e);
    }
}
 
开发者ID:ModelWriter,项目名称:Tarski,代码行数:20,代码来源:MiniSatExternal.java


注:本文中的java.io.FileOutputStream类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。