Search in sources :

Example 76 with FileOutputStream

use of java.io.FileOutputStream in project KJFrameForAndroid by kymjs.

the class ImageUtils method getSmallImageFile.

/**
     * 压缩图片
     *
     * @param filePath 源图片地址
     * @param width    想要的宽度
     * @param height   想要的高度
     * @param isAdjust 是否自动调整尺寸, true图片就不会拉伸,false严格按照你的尺寸压缩
     * @return Bitmap
     */
public static File getSmallImageFile(Context cxt, String filePath, int width, int height, boolean isAdjust) {
    Bitmap bitmap = reduce(BitmapFactory.decodeFile(filePath), width, height, isAdjust);
    File file = new File(getRandomFileName(cxt.getCacheDir().getPath()));
    BufferedOutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 60, outputStream);
        outputStream.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return file;
}
Also used : Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException)

Example 77 with FileOutputStream

use of java.io.FileOutputStream in project eweb4j-framework by laiweiwei.

the class BeanXMLWriter method write.

public File write() throws Exception {
    Document doc = createDoc();
    // 读取文件
    FileOutputStream fos = new FileOutputStream(this.file);
    // 设置文件编码
    OutputFormat format = OutputFormat.createPrettyPrint();
    // 创建写文件方法
    org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(fos, format);
    // 写入文件
    xmlWriter.write(doc);
    // 关闭
    fos.close();
    xmlWriter.close();
    return this.file;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document)

Example 78 with FileOutputStream

use of java.io.FileOutputStream in project eweb4j-framework by laiweiwei.

the class FileUtil method writeFile.

public static void writeFile(File f, String content) throws Exception {
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
        writer.write(content);
    } catch (Exception e) {
        throw e;
    } finally {
        if (writer != null) {
            try {
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) HeadlessException(java.awt.HeadlessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 79 with FileOutputStream

use of java.io.FileOutputStream in project libgdx by libgdx.

the class SharedLibraryLoader method extractFile.

private File extractFile(String sourcePath, String sourceCrc, File extractedFile) throws IOException {
    String extractedCrc = null;
    if (extractedFile.exists()) {
        try {
            extractedCrc = crc(new FileInputStream(extractedFile));
        } catch (FileNotFoundException ignored) {
        }
    }
    // If file doesn't exist or the CRC doesn't match, extract it to the temp dir.
    if (extractedCrc == null || !extractedCrc.equals(sourceCrc)) {
        try {
            InputStream input = readFile(sourcePath);
            extractedFile.getParentFile().mkdirs();
            FileOutputStream output = new FileOutputStream(extractedFile);
            byte[] buffer = new byte[4096];
            while (true) {
                int length = input.read(buffer);
                if (length == -1)
                    break;
                output.write(buffer, 0, length);
            }
            input.close();
            output.close();
        } catch (IOException ex) {
            throw new GdxRuntimeException("Error extracting file: " + sourcePath + "\nTo: " + extractedFile.getAbsolutePath(), ex);
        }
    }
    return extractedFile;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 80 with FileOutputStream

use of java.io.FileOutputStream in project libgdx by libgdx.

the class SharedLibraryLoader method canWrite.

/** Returns true if the parent directories of the file can be created and the file can be written. */
private boolean canWrite(File file) {
    File parent = file.getParentFile();
    File testFile;
    if (file.exists()) {
        if (!file.canWrite() || !canExecute(file))
            return false;
        // Don't overwrite existing file just to check if we can write to directory.
        testFile = new File(parent, UUID.randomUUID().toString());
    } else {
        parent.mkdirs();
        if (!parent.isDirectory())
            return false;
        testFile = file;
    }
    try {
        new FileOutputStream(testFile).close();
        if (!canExecute(testFile))
            return false;
        return true;
    } catch (Throwable ex) {
        return false;
    } finally {
        testFile.delete();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

FileOutputStream (java.io.FileOutputStream)6586 File (java.io.File)3769 IOException (java.io.IOException)2864 FileInputStream (java.io.FileInputStream)1189 OutputStream (java.io.OutputStream)1071 BufferedOutputStream (java.io.BufferedOutputStream)907 InputStream (java.io.InputStream)864 FileNotFoundException (java.io.FileNotFoundException)682 OutputStreamWriter (java.io.OutputStreamWriter)644 Test (org.junit.Test)528 BufferedWriter (java.io.BufferedWriter)313 PrintWriter (java.io.PrintWriter)311 ZipEntry (java.util.zip.ZipEntry)311 BufferedInputStream (java.io.BufferedInputStream)287 DataOutputStream (java.io.DataOutputStream)275 ZipOutputStream (java.util.zip.ZipOutputStream)273 ByteArrayOutputStream (java.io.ByteArrayOutputStream)267 ArrayList (java.util.ArrayList)247 Writer (java.io.Writer)215 URL (java.net.URL)206