6、流与文件
2016-07-24 22:43
330 查看
1. java.io包中的stream类根据它们操作对象的类型是字符还是字节可分为两大类:字节流(InputStream,OutputStream)和字符流(Reader, Writer)。另外,根据实现功能不同可以分为两类:一类流直接从指定的位置(如磁盘文件或内存区域)读或写,这类流称为结点流(node
stream),其它的流则称为过滤器(filters)。
1) 对文件进行操作:FileInputStream(字节输入流),FileOutputStream(字节输出流),FileReader(字符输入流),FileWriter(字符输出流)
2) 对管道进行操作:PipedInputStream(字节输入流),PipedOutStream(字节输出流),PipedReader(字符输入流),PipedWriter(字符输出流)
3) 字节/字符数组:ByteArrayInputStream,ByteArrayOutputStream,CharArrayReader,CharArrayWriter是在内存中开辟了一个字节或字符数组。
4) Buffered缓冲:BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter,是带缓冲区的处理流,缓冲区的作用的主要目的是:避免每次和硬盘打交道,提高数据访问的效率。
5) 转化流:InputStreamReader/OutputStreamWriter,把字节转化成字符。
6) 数据流:DataInputStream,DataOutputStream。
7) 打印流:printStream,printWriter,一般是打印到控制台,可以进行控制打印的地方。
8) 对象流:ObjectInputStream,ObjectOutputStream,把封装的对象直接输出,而不是一个个在转换成字符串再输出。
9) 序列化流:SequenceInputStream
2. java.io.InputStream:
1) abstract int read(): 读取一个字节的数据并返回,当读到流结尾时,返回-1
2) int read(bytep[] b): 将数据读取到一个字节数组中,返回读取实际字节长度,当读到流结尾时,返回-1,最多读取b.length个字节
3) int read(byte[] b, int off, intlen): 将数据读到字节数组并返回读取实际字节长度,当读到流结尾时,返回-1,off表示在b中的偏移
4) long skip(long n): 在输入流中跳过n个字节,返回实际跳过的字节数(如果到了流的结尾处,返回值可能小于n)
5) int available(): 返回可用的未阻塞的字节数
6) void close(): 关闭输入流
7) void mark(int readlimit): 在输入流的当前位置放置一个标记,(不是所有流都支持标记),如果从输入流中读取的字节数多于readlimit字节,那么允许忽略这个标记
8) void reset(): 返回最后的标记,之后调用read方法将会重读那些字节,如果没有当前没有标记,那么流不会被重置
9) boolean markSupported(): 如果流支持标记则返回true
3. java.io.OutputStream:
1) abstract void write(int n): 写入一个字节的数据
2) void write(byte[] b): 写入数组b的所有字节
3) void write(byte[] b, int off,int len): 写入数组b某范围中的数据
4) void close(): 刷新并关闭输出流
5) void flush(): 刷新输出流,即将缓冲区所有数据发送到目的地
4. 将已经存在的流传递给另一个流的构造器方法,将这两种流结合起来,结合后的流被称为过滤流
1) Java.io.FileInputStream:FileInputStream(String name), FileInputStream(File f)新建文件输入流
2) Java.io.FileOutputStream:FileOutputStream(String name)新建文件输出流,FileOutputStream (File f)新建文件输出流,FileOutputStream(String name, boolean append)append为true时则数据被追加到文件的结尾
3) Java.io.BufferedInputStream:BufferedInputStream(InputStream in)新建一个默认大小的缓冲流,BufferedInputStream(InputStreamin, int n)指定大小的缓冲流
4) Java.io.BufferedOutputStream:BufferedOutputStream(OutputStream in)新建一个默认大小的缓冲流,BufferedOutputStream(OutputStreamin, int n)指定大小的缓冲流
5) Java.io.PushbackStream:PushbackStream(InputStream in)构造一个预查看一个字节的流,PushbackStream(InputStream in, int size)指定大小的流, void unread(intb)返回一个字节,下次调用读取时仍会读取该字节,一次只能返回一个字节
5. 数据流的读取,接口java.io.input:
6. 数据流的写入,接口java.io.Output:
7. 随机存取文件流java.io.RandomAccessFile:
3) public long getFilePointer()返回文件指针当前位置
5) long length()返回文件的长度
8. 需要将一个读取器或写入器同文件联系在一起时,java提供了便捷的类FileReader和FileWriter
9. Java.util.StrinfgTokenizer:
1) StringTokenizer(String str, String delim)使用给定的分隔符集合构造一个字符串的记号处理器
2) boolean hasMoreTokens() 如果存在更多地记号,返回true
3) String nextToken() 返回下一个记号,如果没有,抛出NoSuchElementException异常
10. StringBuilder类:类似于ArrayList,管理着一个可以根据需求增长或缩短的字符数组char[],然后使用toString方法将内容转换成一个真正的String对象
stream),其它的流则称为过滤器(filters)。
1) 对文件进行操作:FileInputStream(字节输入流),FileOutputStream(字节输出流),FileReader(字符输入流),FileWriter(字符输出流)
2) 对管道进行操作:PipedInputStream(字节输入流),PipedOutStream(字节输出流),PipedReader(字符输入流),PipedWriter(字符输出流)
3) 字节/字符数组:ByteArrayInputStream,ByteArrayOutputStream,CharArrayReader,CharArrayWriter是在内存中开辟了一个字节或字符数组。
4) Buffered缓冲:BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter,是带缓冲区的处理流,缓冲区的作用的主要目的是:避免每次和硬盘打交道,提高数据访问的效率。
5) 转化流:InputStreamReader/OutputStreamWriter,把字节转化成字符。
6) 数据流:DataInputStream,DataOutputStream。
7) 打印流:printStream,printWriter,一般是打印到控制台,可以进行控制打印的地方。
8) 对象流:ObjectInputStream,ObjectOutputStream,把封装的对象直接输出,而不是一个个在转换成字符串再输出。
9) 序列化流:SequenceInputStream
2. java.io.InputStream:
1) abstract int read(): 读取一个字节的数据并返回,当读到流结尾时,返回-1
2) int read(bytep[] b): 将数据读取到一个字节数组中,返回读取实际字节长度,当读到流结尾时,返回-1,最多读取b.length个字节
3) int read(byte[] b, int off, intlen): 将数据读到字节数组并返回读取实际字节长度,当读到流结尾时,返回-1,off表示在b中的偏移
4) long skip(long n): 在输入流中跳过n个字节,返回实际跳过的字节数(如果到了流的结尾处,返回值可能小于n)
5) int available(): 返回可用的未阻塞的字节数
6) void close(): 关闭输入流
7) void mark(int readlimit): 在输入流的当前位置放置一个标记,(不是所有流都支持标记),如果从输入流中读取的字节数多于readlimit字节,那么允许忽略这个标记
8) void reset(): 返回最后的标记,之后调用read方法将会重读那些字节,如果没有当前没有标记,那么流不会被重置
9) boolean markSupported(): 如果流支持标记则返回true
3. java.io.OutputStream:
1) abstract void write(int n): 写入一个字节的数据
2) void write(byte[] b): 写入数组b的所有字节
3) void write(byte[] b, int off,int len): 写入数组b某范围中的数据
4) void close(): 刷新并关闭输出流
5) void flush(): 刷新输出流,即将缓冲区所有数据发送到目的地
4. 将已经存在的流传递给另一个流的构造器方法,将这两种流结合起来,结合后的流被称为过滤流
1) Java.io.FileInputStream:FileInputStream(String name), FileInputStream(File f)新建文件输入流
2) Java.io.FileOutputStream:FileOutputStream(String name)新建文件输出流,FileOutputStream (File f)新建文件输出流,FileOutputStream(String name, boolean append)append为true时则数据被追加到文件的结尾
3) Java.io.BufferedInputStream:BufferedInputStream(InputStream in)新建一个默认大小的缓冲流,BufferedInputStream(InputStreamin, int n)指定大小的缓冲流
4) Java.io.BufferedOutputStream:BufferedOutputStream(OutputStream in)新建一个默认大小的缓冲流,BufferedOutputStream(OutputStreamin, int n)指定大小的缓冲流
5) Java.io.PushbackStream:PushbackStream(InputStream in)构造一个预查看一个字节的流,PushbackStream(InputStream in, int size)指定大小的流, void unread(intb)返回一个字节,下次调用读取时仍会读取该字节,一次只能返回一个字节
5. 数据流的读取,接口java.io.input:
boolean | readBoolean()Reads one input byte and returns trueif that byte is nonzero, falseif that byte is zero. |
byte | readByte()Reads and returns one input byte. |
char | readChar()Reads two input bytes and returns a charvalue. |
double | readDouble()Reads eight input bytes and returns a doublevalue. |
float | readFloat()Reads four input bytes and returns a floatvalue. |
void | readFully(byte[] b)Reads some bytes from an input stream and stores them into the buffer array b. |
void | readFully(byte[] b, int off, int len)Reads lenbytes from an input stream. |
int | readInt()Reads four input bytes and returns an intvalue. |
String | readLine()Reads the next line of text from the input stream. |
long | readLong()Reads eight input bytes and returns a longvalue. |
short | readShort()Reads two input bytes and returns a shortvalue. |
int | readUnsignedByte()Reads one input byte, zero-extends it to type int, and returns the result, which is therefore in the range 0through 255. |
int | readUnsignedShort()Reads two input bytes and returns an intvalue in the range 0through 65535. |
String | readUTF()Reads in a string that has been encoded using amodified UTF-8 format. |
int | skipBytes(int n)Makes an attempt to skip over nbytes of data from the input stream, discarding the skipped bytes. |
void | write(byte[] b) Writes to the output stream all the bytes in array b. |
void | write(byte[] b, int off, int len) Writes len bytes from array b, in order, to the output stream. |
void | write(int b) Writes to the output stream the eight low-order bits of the argument b. |
void | writeBoolean(boolean v) Writes a boolean value to this output stream. |
void | writeByte(int v) Writes to the output stream the eight low- order bits of the argument v. |
void | writeBytes(String s) Writes a string to the output stream. |
void | writeChar(int v) Writes a char value, which is comprised of two bytes, to the output stream. |
void | writeChars(String s) Writes every character in the string s, to the output stream, in order, two bytes per character. |
void | writeDouble(double v) Writes a double value, which is comprised of eight bytes, to the output stream. |
void | writeFloat(float v) Writes a float value, which is comprised of four bytes, to the output stream. |
void | writeInt(int v) Writes an int value, which is comprised of four bytes, to the output stream. |
void | writeLong(long v) Writes a long value, which is comprised of eight bytes, to the output stream. |
void | writeShort(int v) Writes two bytes to the output stream to represent the value of the argument. |
void | writeUTF(String s) Writes two bytes of length information to the output stream, followed by themodified UTF-8 representation of every character in the string s. |
1)RandomAccessFile
(File
file,String
mode)
2)RandomAccessFile
(String
name,String
mode)“r”
表示只读方式,
”rw”
读写模式,
”rws”
同步内容和元数据,
”rwd”
同步内容
3) public long getFilePointer()返回文件指针当前位置
4) public void seek(long pos)设置文件指针的位置
5) long length()返回文件的长度
8. 需要将一个读取器或写入器同文件联系在一起时,java提供了便捷的类FileReader和FileWriter
9. Java.util.StrinfgTokenizer:
1) StringTokenizer(String str, String delim)使用给定的分隔符集合构造一个字符串的记号处理器
2) boolean hasMoreTokens() 如果存在更多地记号,返回true
3) String nextToken() 返回下一个记号,如果没有,抛出NoSuchElementException异常
10. StringBuilder类:类似于ArrayList,管理着一个可以根据需求增长或缩短的字符数组char[],然后使用toString方法将内容转换成一个真正的String对象
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树