在Android中写入文件,但无法获取输出流

在Android中写入文件,但无法获取输出流,android,android-resources,Android,Android Resources,我需要写入资产文件夹中的文件。我必须使用 context.getResources().getAssets() 但是当我使用openpath_来_my_txt时,我得到了一个InputStream,而我发现将它转换为OutputStream没有什么好处 如何使用getResources方法写入此文件?下面的类取自Unviersal imageloader import java.io.Closeable; import java.io.IOException; import java.io.I

我需要写入资产文件夹中的文件。我必须使用

context.getResources().getAssets()
但是当我使用openpath_来_my_txt时,我得到了一个InputStream,而我发现将它转换为OutputStream没有什么好处
如何使用getResources方法写入此文件?

下面的类取自Unviersal imageloader

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Provides I/O operations
 * 
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 * @since 1.0.0
 */
public final class IoUtils {

    /** {@value} */
    public static final int DEFAULT_BUFFER_SIZE = 32 * 1024; // 32 KB
    /** {@value} */
    public static final int DEFAULT_IMAGE_TOTAL_SIZE = 500 * 1024; // 500 Kb
    /** {@value} */
    public static final int CONTINUE_LOADING_PERCENTAGE = 75;

    private IoUtils() {
    }

    /**
     * Copies stream, fires progress events by listener, can be interrupted by
     * listener. Uses buffer size = {@value #DEFAULT_BUFFER_SIZE} bytes.
     * 
     * @param is
     *            Input stream
     * @param os
     *            Output stream
     * @param listener
     *            null-ok; Listener of copying progress and controller of
     *            copying interrupting
     * @return <b>true</b> - if stream copied successfully; <b>false</b> - if
     *         copying was interrupted by listener
     * @throws IOException
     */
    public static boolean copyStream(InputStream is, OutputStream os,
            CopyListener listener) throws IOException {
        return copyStream(is, os, listener, DEFAULT_BUFFER_SIZE);
    }

    /**
     * Copies stream, fires progress events by listener, can be interrupted by
     * listener.
     * 
     * @param is
     *            Input stream
     * @param os
     *            Output stream
     * @param listener
     *            null-ok; Listener of copying progress and controller of
     *            copying interrupting
     * @param bufferSize
     *            Buffer size for copying, also represents a step for firing
     *            progress listener callback, i.e. progress event will be fired
     *            after every copied <b>bufferSize</b> bytes
     * @return <b>true</b> - if stream copied successfully; <b>false</b> - if
     *         copying was interrupted by listener
     * @throws IOException
     */
    public static boolean copyStream(InputStream is, OutputStream os,
            CopyListener listener, int bufferSize) throws IOException {
        int current = 0;
        int total = is.available();
        if (total <= 0) {
            total = DEFAULT_IMAGE_TOTAL_SIZE;
        }

        final byte[] bytes = new byte[bufferSize];
        int count;
        if (shouldStopLoading(listener, current, total))
            return false;
        while ((count = is.read(bytes, 0, bufferSize)) != -1) {
            os.write(bytes, 0, count);
            current += count;
            if (shouldStopLoading(listener, current, total))
                return false;
        }
        os.flush();
        return true;
    }

    private static boolean shouldStopLoading(CopyListener listener,
            int current, int total) {
        if (listener != null) {
            boolean shouldContinue = listener.onBytesCopied(current, total);
            if (!shouldContinue) {
                if (100 * current / total < CONTINUE_LOADING_PERCENTAGE) {
                    return true; // if loaded more than 75% then continue
                                    // loading anyway
                }
            }
        }
        return false;
    }

    /**
     * Reads all data from stream and close it silently
     * 
     * @param is
     *            Input stream
     */
    public static void readAndCloseStream(InputStream is) {
        final byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
        try {
            while (is.read(bytes, 0, DEFAULT_BUFFER_SIZE) != -1)
                ;
        } catch (IOException e) {
            // Do nothing
        } finally {
            closeSilently(is);
        }
    }

    public static void closeSilently(Closeable closeable) {
        try {
            closeable.close();
        } catch (Exception e) {
            // Do nothing
        }
    }

    /** Listener and controller for copy process */
    public static interface CopyListener {
        /**
         * @param current
         *            Loaded bytes
         * @param total
         *            Total bytes for loading
         * @return <b>true</b> - if copying should be continued; <b>false</b> -
         *         if copying should be interrupted
         */
        boolean onBytesCopied(int current, int total);
    }
}

您不能在文件夹中写入文件,但可以将其写入文件系统

try {
                is = assetManager.open("filename");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                OutputStream os = null;
                IoUtils.copyStream(is, os);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }