JAVA-input and output stream summary
- Description: Input and output for programs, not files.
Program->file(output)
program<-file(input)
First things first: files
构造方法:
File(String Filename文件名)
File(String DirectoryPath路径名,String Filename文件名)
File(String Dirobj指定目录,String Filename文件名)
eg:
File f1=new File("a.txt"); (在当前类的目录下,创建一个a.txt的文件)
JAVA streams are divided into:
- file byte stream
- file character stream
- buffered stream
- data flow
- object stream
file byte stream
(One byte is read one byte, so the Chinese characters are garbled (Chinese characters occupy two bytes) )
(data stream application)
* 输入流FileInputStream
构造方法: FileInputStream(String name)
读操作: int read();
int read(byte a[]);
* 输出流FileOutputStream
构造方法: FileOutputStream(String name)
读操作: void write();
void write(byte b[]);
file character stream
(Can handle Chinese characters, handle as Unicode characters)
(buffer stream application)
* 输入流FileReader
构造方法: 同上
读操作:
int read()
int read(char b[])
* 输出流FileWriter
构造方法: 同上
写操作:
write(char b[]) 建字符数组-字符串变字符数组-写入文件
write(String str)
buffered stream
(file character stream as parameter)
* 输入流BufferedReader
构造方法:BufferedReader(Reader in)
读文本行: readerLine()
* 输出流BufferedWriter
构造方法:BufferedWriter(Writer)
写操作:write(String str)
eg:
FileReader in=new FileReader("a.txt");
BufferedReader inin=new BufferedReader(in);
data flow
(read of numeric data, read long double boolean)
(not byte/character storage, open as garbled)
* 输入流DataInputStream
构造方法:DataInputStream(InputStream in)
读:
readInt()
readDouble()
* 输出流DataOutputStream
构造方法:DataOutputStream(OutputStream out)
写:
writeInt()
writeDouble()
writeLong()
object stream
("name + password" is an object, in object units; the class must be serialized)
(byte stream as parameter)
* 输入流ObjectInputStream
构造方法: ObjectInputStream(InputStream in)
读: readObject(Object obj)
* 输出流ObjectOutputSteam
构造方法: ObjectOutputStream(OutputStream out)
写: writeObject(Object obj)