1.字符流Reader
Reader方法
方法 | 说明 |
int read() | 从输入流中读取单个字符,返回所读取的字符数据 |
int read(byte[] c) | 从输入流中读取多个c.length长度的字符,保存在字符数组c中,返回实际读取的字符数 |
read(char[] c,int off,int len) | 从输入流中读取最多len长度的字符,保存到字符数组c中,保存的位置从off开始,返回实际读取的字符数 |
void close() | 关闭流 |
boolean ready() | 如果要读的流已经准备好,返回true,否则返回false |
skip(long n) | 从输入流跳过参数n指定数目的字符 |
mark(int readlimit) | 标记输入流中的当前位置,以便使用reset()方法复位到该标记的位置 |
void reset() | 将当前位置复位为上次调用mark()方法标记的位置 |
读取数据两种方法:
1.用
BufferedReader:
public class Ch07 {
public static void main(String[] args) {
File file = new File("D:/iodemo/ch04.txt");
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String str = null;
int len = 0;
// 逐行读取
while ((str = br.readLine()) != null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2.通过read方法来读取:
public class Ch08 {
public static void main(String[] args) {
// 读取文件
File f = new File("D:/iodemo/ch04.txt");
FileReader fr = null;
try {
fr = new FileReader(f);
// 以字符为单位读取数据,字节流以字节为单位
char[] c = new char[1024];
int len = 0;
StringBuffer sb = new StringBuffer();
while ((len = fr.read(c, 0, 10)) != -1) {
// 把读取到的内容存放到sb中
sb.append(c, 0, len);
}
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
字节流装换成字符流:
public class Ch09 {
public static void main(String[] args) {
// 字节流转字符流
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("D:/iodemo/isr.txt")));
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str + "\r\n");
}
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2.字符流Writer
Writer方法:
方法 | 说明 |
write(String str) | 将str字符串包含的字符输出到指定的输出流中 |
write(String str,int off,int len) | 将str字符串里从off位置开始长度为len的字符输出到输出流中 |
void close() | 关闭流 |
void flush() | 清空流 |
public class Ch10 {
public static void main(String[] args) {
FileWriter fw=null;
try {
fw=new FileWriter("D:/iodemo/fw.doc");
fw.write("理发师的发布的");
fw.newLine();
fw.write("啊啊啊啊啊");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if (fw!=null) {
fw.flush();
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}