android: 文件操作

以前感觉 io系统的接口、类等比较乱,这次因为要解 zip压缩文件,不得不研究了下,感觉略有收获,先记录下。

File类表示是什么?


//文件名、文件夹名的抽象表示[消除了系统相关性如 windows/linux表示文件名的方式是不同的有"\" "/"之分]
An abstract representation of file and directory pathnames.
public File(String pathname) {}

是User interfaces,文件的创建、删除等都用 File的成员函数 to文件路径名的转换关系是:

file.toString()或者file.getPath(); File file = new File("file name")

写文件 FileOutputStream

input/output和读写的关系有点乱,记住一句话:从输入流中读数据,写数据到输出流里。

OutputStream是有关输出流的抽象类,不止是文件、显示器等都可以用

FileOutputStream //是 字节的方式不是char
class FileOutputStream extends OutputStream {}
A file output stream is an output stream for writing data to a File or to a FileDescriptor.

读文件 FileInputStream?


//FileInputStream 读raw bytes,如果要读字符流有使用FileReader
A FileInputStream obtains input bytes from a file in a file system.
FileInputStream is meant for reading streams of raw bytes such as image data. 
For reading streams of characters, consider using FileReader
InputStream in = new FileInputStream(new File(filePath));

Reader
/* Abstract class for reading character streams.*/
public abstract class Reader implements Readable, Closeable {}

BufferReader
/**
 * Reads text from a character-input stream, buffering characters so as to
 * provide for the efficient reading of characters, arrays, and lines.
 */
public class BufferedReader extends Reader {}

StringBuffer
/**
 * A thread-safe, mutable sequence of characters.[可变的字符序列]
 * A string buffer is like a {@link String}, but can be modified. At any
 * point in time it contains some particular sequence of characters, but
 * the length and content of the sequence can be changed through certain
 * method calls.
 */
public final class StringBuffer extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence
{}

//5.1 获得conn的InputStream 和要写入文件的outStream
                inStream = conn.getInputStream();
                File aimFile = new File(folder, fileNameString);
                outStream = new FileOutputStream(aimFile);

//5.2 循环从InputStream 读到数据写入到 outStream
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = inStream.read(buffer)) != -1) {// inputStream的读函数
                    outStream.write(buffer, 0, len);// 向outputStream里写入
                }
                Log.i(TAG, "please check file of tmp.config exit or not");
                return aimFile;

猜你喜欢

转载自blog.csdn.net/u011279649/article/details/88354156