------------- android培训、java培训、期待与您交流! ---------------
IO流四个顶层父类
字节流的抽象基类:
1. InputStream 2. OutputStream
字符流的抽象基类:
1. Reader 2. Writer
命名:这些体系的子类几乎都以父类名为后缀。 子类名的前缀为该对象的功能。
记住:如果要操作文字数据,优先考虑字符流。要将数据从内存写到硬盘中,
使用字符流中的输出流:Writer.FileWriter对象:
创建一个文件:FileWriter fw = new FileWriter("demo.doc");
调用writer对象中的writer(String)
方法写入数据。fw.writer("demo")
使用刷新,将数据写到目的地。fw.flush();
关闭流,再用writer()和flush()抛出异常fw.close
public class FileWriterDemo {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");//换行符
public static void main(String[] args) {
// 创建一个可以往文件中写入字符流的字符输出流对象。
FileWriter fw = null;
try {
fw = new FileWriter("d:\\demo.txt", true);//如果构造函数中加入true,可以实现对文件的续写
fw.write("abc" + LINE_SEPARATOR + "HAHA");
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (fw != null)
;
try {
fw.close();//关闭流
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}
}
}
}
FileWriter对象:
一。
public static void main(String[] args) throws IOException {
// 在创建读取流时,必须要明确被读的文件并且存在
FileReader fr = new FileReader("demo.txt");
int ch = 0;
//一个一个读。
while((ch=fr.read())!=-1) {
System.out.println((char)ch);
}
}
二。
public static void main(String[] args) throws IOException {
// 在创建读取流时,必须要明确被读的文件并且存在
FileReader fr = new FileReader("demo.txt");
//使用read(char[])读取文本中的数据。
char[] buf=new char[4];//首先创建字符数组,数组最好使用1024的倍数。
int len=0;
while((len=fr.read(buf))!=-1) {
System.out.println(new String(buf,0,len));
}
}
//复制文本文件
public class CopyTextDemo {
public static void main(String[] args) throws IOException {
// 1,读取一个已有的文本文件,使用字符读取流。
FileReader fr=new FileReader("Demo.txt");
// 2,创建目的,储存读取到得数据。
FileWriter fw=new FileWriter("Write.txt");
// 3,执行读写操作。
int ch=0;
while((ch=fr.read())!=-1) {
fw.write(ch);
}
// 4,关闭流。
fr.close();
fw.close();
}
}
第二种方法:
public class CopyTestDemo2 {
private static final int BUFFER_SIZE = 1024;
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("demo.txt");
fw = new FileWriter("Writer2.txt");
char[] buffer = new char[BUFFER_SIZE];
int len = 0;
while ((len = fr.read(buffer)) != -1) {
fw.write(buffer, 0, len);
}
} catch (Exception e) {
throw new RuntimeException("复制失败");
} finally {
if (fr != null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
if (fw != null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/* 分析缓冲区:
缓冲区无非就是封装了一个数组,并对外提供一个更多方法对数组进行访问,其实这些方法最终操作的都是数组的角标。*/
/* 缓冲原理:
就是从源中获取一批数据装进缓冲区中。再从缓冲区中不断的取出一个一个数据。
此次取完后,再从源中继续取一批数据进缓冲区,当源中的数据取完时,用-1作为结束标记。*/
public class MyBufferedReader {
private FileReader r;
// 定义一个数组做缓冲区
private char[] buffer = new char[1024];
// 定义一个指针用于操作这个数组的元素,当操作到最后一个元素后指针归为零
private int pos = 0;
// 定义一个计数器记录缓冲区的数据个数,数据减到0,从源中获取数据。
private int count = 0;
MyBufferedReader(FileReader r) {
this.r = r;
}
public int myRead() throws IOException {
if (count == 0) {
count = r.read(buffer);
pos = 0;
}
if (count < 0)
return -1;
char ch = buffer[pos];
pos++;
count--;
return ch;
}
public String myReadLine() throws IOException {
StringBuilder sb = new StringBuilder();
int ch = 0;
while ((ch - myRead()) != -1) {
if (ch == '\r')
continue;
if (ch == '\n')
return sb.toString();
sb.append((char) ch);
}
return null;
}
}
------------- android培训、java培训、期待与您交流! ---------------