一.字符流:
尽管字节流提供了处理任何类型的输入/输出操作的足够功能,它们不能直接操作Unicode字符,所以java提供字符输入输出流。字符流的顶层是Reader和Writer抽象类。
Reader和Writer类也有较多的子类,与字节流类似,它们用来创建具体的字符流对象进行I/O操作,字符流的读写等方法与字节流的方法类似,但读写的对象使用的是字符。
Reader层次(输入流)部分:
Writer层次(输出流)部分:
二.I/O操作:
1).简单的输出流:
public class StreamTest {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("file.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("我是第一行!");
bw.write("\n");
bw.write("我是第二行!");
bw.close();
}
}
在项目目录下就可以看到file.txt文件。从例子中可以看出字符流和字节流的操作基本类似。
2).输入流:
<span style="font-size:18px;">public class StreamTest {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("file.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
//读一行数据
System.out.println(br.readLine());
System.out.println(br.readLine());
br.close();
}
}</span>
打印:
我是第一行!
我是第二行!
3).手工输入流:
public class StreamTest2 {
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str ;
while(null != (str = br.readLine())){
System.out.println(str);
}
br.close();
}
}
打印你输入的数据。
4).字符数组输入流:
public class StreamTest3 {
public static void main(String[] args) throws Exception {
String str = "WA HA HA";
char[] ch = new char[str.length()];
str.getChars(0,str.length(),ch,0);
CharArrayReader input = new CharArrayReader(ch);
int i ;
while(-1 != (i = input.read())){
System.out.println((char)i);
}
input.close();
}
}
打印:
WA HA HA
5).随即文件访问类(RandomAccessFile.class):
RandomAccessFile包装了一个随机访问的文件,它不是派生于InputStreadm和OutputStream,而是实现定义了基本输入输出方法的DataInput和DataOutput接口,它支持定位要求,也就是说,可以在文件内部放置文件指针。
它的两个构造方法:
| 构造方法摘要 | |
|---|---|
RandomAccessFile(File file,String mode)
创建从中读取和向其中写入(可选)的随机访问文件流,该文件由 File 参数指定。 |
|
RandomAccessFile(String name,String mode)
创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。 |
|
其中第一个参数file指定文件,第二个参数mode决定如何访问文件,如果是“r”那么文件可读不可写,如果是“rw”文件可读可写。
实例:
public class RandomAccessFileTest {
public static void main(String[] args) throws Exception {
Student s1 = new Student("赵三",13,9.99);
RandomAccessFile raf = new RandomAccessFile("student.txt","rw");
s1.write(raf);
Student s2 = new Student();
raf.seek(0);//要将读取的位置重新回到开头
s2.read(raf);
System.out.println(s2.getName()+" "+s2.getAge()+" "+s2.getId());
}
}
class Student{
String name ;
int age ;
double id ;
public Student(){}
public Student(String name ,int age,double id){
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public void write(RandomAccessFile raf) throws Exception{
raf.writeUTF(name);
raf.writeInt(age);
raf.writeDouble(id);
}
public void read(RandomAccessFile raf) throws Exception{
this.name = raf.readUTF();
this.age = raf.readInt();
this.id = raf.readDouble();
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getId() {
return id;
}
public void setId(double id) {
this.id = id;
}
}打印:赵三 13 9.99
原文:http://blog.csdn.net/u010708662/article/details/44495797