IO流 注意点
字节流InputStream/OutputStream
基本流
1.1 构造
输出流构造
public FileOutputStream(File file):创建文件输出流以写入由指定的 File对象表示的文件。public FileOutputStream(String name): 创建文件输出流以指定的名称写入文件。FileOutputStream(File file, boolean append):追加
输入流构造
FileInputStream(File file)FileInputStream(String name)
1.2 输出流 write()方法
-
write(int b) -
write(byte[] b) -
write(byte[] b, int off, int len) -
write(int b, boolean append)
1.3 输入流 read()方法
int read(byte[] b)int read()read(byte b[], int off, int len)
缓冲流
1.1构造
输入流构造
BufferedInputStream(InputStream in)BufferedInputStream(InputStream in, int size)
输出流构造
BufferedOutputStream(OutputStream out)BufferedOutputStream(OutputStream out, int size)
1.2 输出流 write()方法
write(byte b[], int off, int len)write(int b)
1.3 输入流 read()方法
int read()int read(byte[] b, int off, int len)int read(byte b[])
序列化 对象流
1.1构造
输入流构造
ObjectInputStream(InputStream in)
输出流构造
ObjectOutputStream(OutputStream out)
1.2 输出流 write()方法
writeObject(Object obj)
1.3 输入流 read()方法
Object readObject()
1.4 对象实现序列化
- 对象类 implements Serializable
随机访问流 RandomAccessFile
1.1构造
构造
RandomAccessFile(File file, String mode)RandomAccessFile(String name, String mode)
mode --> r(只读)、rw(可读写)
1.2 输出流 write()方法
write(byte b[], int off, int len)write(byte b[])write(int b)
1.3 输入流 read()方法
int read(byte b[])int read()
1.4 设置起始位置
seek(long pos)
字符流 Reader/Writer
基本流
2.1 构造
输入流构造
FileReader(File file)FileReader(String fileName)
输出流构造
FileWriter(File file)FileWriter(String fileName)
2.2 输出流 write()方法
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 输入流 read()方法
int read()int read(char[] cbuf)缓冲流没有int read(char cbuf[], int off, int len)
缓冲流
2.1构造
输入流构造
BufferedReader(Reader in)BufferedReader(Reader in, int sz)
输出流构造
BufferedWriter(Writer out)BufferedWriter(Writer out, int sz)
2.2 输出流 write()方法
write(String str)write(String str, int off, int len)write(char cbuf[])write(char cbuf[], int off, int len)write(int c)- **
newLine()** 基本流没有
2.3 输入流 read()方法
int read()read(char[] cbuf, int off, int len)String readLine(boolean ignoreLF)基本流没有
转换流
2.1构造
输入流构造
-
InputStreamReader(InputStream in) -
InputStreamReader(InputStream in, String charsetName)
输出流构造
-
OutputStreamWriter(OutputStream in) -
OutputStreamWriter(OutputStream out, String charsetName)
2.2 输出流 write()方法
write(int c)write(char[] cbuf, int off, int len)write(String str, int off, int len)
2.3 输入流 read()方法
int read()int read(char[] cbuf)缓冲流没有read(char cbuf[], int off, int len)
注意
当创建流对象的时候,输入输出流的文件对象不能是同一个文件!
例如:
BufferedReader br= new BufferedReader(new FileReader(“text.txt”));
BufferedWriter br = new BufferedWriter (new FileWriter(“text.txt”));
!!这样获取输出流对象后,文件内容会直接清空!!
若需要在同一个文件里输入输出,可以在while()中读取完数据后
再创建输出流。
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();
本文详细探讨了Java中的IO流,包括字节流InputStream和OutputStream的基本流、缓冲流、对象流和随机访问流RandomAccessFile的构造方法及write()、read()等关键方法。特别指出在使用流时,输入输出流对象不能指向同一文件,否则可能导致数据丢失。
1402

被折叠的 条评论
为什么被折叠?



