IO flow Notes
Byte stream InputStream / OutputStream
The basic flow
1.1 Constructs
Output stream configuration
public FileOutputStream(File file)
: Create a file output stream to write to the file represented by the specified File object.public FileOutputStream(String name)
: Create a file output stream to write to the file name specified.FileOutputStream(File file, boolean append)
:add to
Configuration input stream
FileInputStream(File file)
FileInputStream(String name)
1.2 the output stream write () method
-
write(int b)
-
write(byte[] b)
-
write(byte[] b, int off, int len)
-
write(int b, boolean append)
1.3 input stream read () method
int read(byte[] b)
int read()
read(byte b[], int off, int len)
Buffer flow
1.1 Constructs
Configuration input stream
BufferedInputStream(InputStream in)
BufferedInputStream(InputStream in, int size)
Output stream configuration
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out, int size)
1.2 the output stream write () method
write(byte b[], int off, int len)
write(int b)
1.3 input stream read () method
int read()
int read(byte[] b, int off, int len)
int read(byte b[])
Serialized object stream
1.1 Constructs
Configuration input stream
ObjectInputStream(InputStream in)
Output stream configuration
ObjectOutputStream(OutputStream out)
1.2 the output stream write () method
writeObject(Object obj)
1.3 input stream read () method
Object readObject()
1.4 serialized object implementation
- Object class implements Serializable
Random access flow RandomAccessFile
1.1 Constructs
structure
RandomAccessFile(File file, String mode)
RandomAccessFile(String name, String mode)
mode -> r (read-only), rw (read-write)
1.2 the output stream write () method
write(byte b[], int off, int len)
write(byte b[])
write(int b)
1.3 input stream read () method
int read(byte b[])
int read()
1.4 Set the start position
seek(long pos)
Character stream Reader / Writer
The basic flow
2.1 construction
Configuration input stream
FileReader(File file)
FileReader(String fileName)
Output stream configuration
FileWriter(File file)
FileWriter(String fileName)
2.2 the output stream write () method
write(int c)
write(char[] cbuf)
write(char[] cbuf, int off, int len)
write(String str)
write(String str, int off, int len)
2.3 input stream read () method
int read()
int read(char[] cbuf)
No buffer flowint read(char cbuf[], int off, int len)
Buffer flow
2.1 construction
Configuration input stream
BufferedReader(Reader in)
BufferedReader(Reader in, int sz)
Output stream configuration
BufferedWriter(Writer out)
BufferedWriter(Writer out, int sz)
2.2 the output stream write () method
write(String str)
write(String str, int off, int len)
write(char cbuf[])
write(char cbuf[], int off, int len)
write(int c)
- **
newLine()
** The basic flow no
2.3 input stream read () method
int read()
read(char[] cbuf, int off, int len)
String readLine(boolean ignoreLF)
The basic flow no
Conversion flow
2.1 construction
Configuration input stream
-
InputStreamReader(InputStream in)
-
InputStreamReader(InputStream in, String charsetName)
Output stream configuration
-
OutputStreamWriter(OutputStream in)
-
OutputStreamWriter(OutputStream out, String charsetName)
2.2 the output stream write () method
write(int c)
write(char[] cbuf, int off, int len)
write(String str, int off, int len)
2.3 input stream read () method
int read()
int read(char[] cbuf)
No buffer flowread(char cbuf[], int off, int len)
note
When you create a stream object, the object file input and output streams can not be the same file!
E.g:
BufferedReader br= new BufferedReader(new FileReader(“text.txt”));
BufferedWriter br = new BufferedWriter (new FileWriter(“text.txt”));
! ! After obtaining such an output stream objects, files, empty the contents directly! !
If the required input and output in the same file, read data can be finished in a while () in
Re-creating the output stream.
BufferedReader br = new BufferedReader(new FileReader("test5.txt"));
BufferedWriter bw = null;
String str;
StringBuffer sb = new StringBuffer();
while((str = br.readLine()) != null){
sb.append(str.substring(0,10).toUpperCase());
sb.append(str.substring(10,str.length()-10));
for(int i = 0;i < 10;i++){
char[] chars = str.substring(str.length() - 10).toCharArray();
int num = chars[i];
sb.append(num);
}
}
bw = new BufferedWriter(new FileWriter("test5.txt"));
bw.write(sb.toString());
bw.close();