FileReader
准备工作
准备一个E:\news.txt,内容如下,字符编码为UTF-8
常用构造器
-
public FileReader(String fileName) throws FileNotFoundException
传入文件的路径字符串
-
public FileReader(File file) throws FileNotFoundException
传入文件的file对象
1 2
| FileReader fileReader = new FileReader("E://news.txt");
|
1 2
| FileReader fileReader = new FileReader(new File("E://news.txt"));
|
read
可以将FileReader中的内容读取出来
有以下常用重载形式
-
public int read() throws IOException
一次读取一个字符,返回范围在0到65535字符对应的整数,读取到文件末尾时返回-1
-
public int read(char cbuf[]) throws IOException
返回读取的字符数,将读取到的字符存入到cbuf字符数组中,读取到文件末尾时返回-1,一次读取的字符数不超过字符数组的大小
-
public int read(char cbuf[], int offset, int length) throws IOException
返回读取的字符数,将读取到的字符存入到cbuf字符数组中的offset位置开始的长度为length的子数组中,读取到文件末尾时返回-1
1 2 3 4 5 6 7 8
| FileReader fileReader = new FileReader("E:\\news.txt"); int data = -1; while ((data = fileReader.read()) != -1) { System.out.print((char)data); }
|
1 2 3 4 5 6 7 8 9 10 11
| FileReader fileReader = new FileReader("E:\\news.txt"); int readLine = -1; char[] cbuf = new char[2]; while ((readLine = fileReader.read(cbuf)) != -1) { System.out.println("读取到的字符数:" + readLine + "," + "读取到的元素为:" + new String(cbuf, 0, readLine)); }
|
1 2 3 4 5 6 7 8
| FileReader fileReader = new FileReader("E:\\news.txt"); int readLine = -1; char[] cbuf = new char[2];
while ((readLine = fileReader.read(cbuf, 0, 2)) != -1) { System.out.println("读取到的字符数:" + readLine + "," + "读取到的元素为:" + new String(cbuf, 0, readLine)); }
|
getEncoding
获得FileReader绑定的文件的字符编码
public String getEncoding()
1 2 3 4
| FileReader fileReader = new FileReader("E:\\news.txt"); System.out.println(fileReader.getEncoding());
|
ready
用于验证缓冲区流是否已准备好被读取。在两种情况下,如果缓冲区不为空或主流已准备就绪,则表示缓冲区流已准备就绪
返回true说明已经准备好,返回false说明还没有准备好
1 2 3 4
| FileReader fileReader = new FileReader("E:\\news.txt"); System.out.println(fileReader.ready());
|
markSupported
查看FileReader是否支持mark操作,值得一提的是,FileReader确实不支持mark和reset操作
public boolean markSupported()
1 2 3 4
| FileReader fileReader = new FileReader("E:\\news.txt"); System.out.println(fileReader.markSupported());
|
skip
这个方法可以跳过指定长度的字符读取
public long skip(long n) throws IOException
传入要跳过的字符数,返回实际跳过的字符数
1 2 3 4 5 6 7 8 9 10 11 12 13
| FileReader fileReader = new FileReader("E:\\news.txt"); int data = -1; while ((data = fileReader.read()) != -1) { System.out.println((char)data); System.out.println("跳过字符数:" + fileReader.skip(1)); }
|
close
将FileReader流进行关闭
public void close() throws IOException
FileReader要做到不用则关的好习惯
1 2
| FileReader fileReader = new FileReader("E:\\news.txt"); fileReader.close();
|
FileWriter
准备工作
准备一个空的文件news.txt,字符编码为UTF-8
常用构造器
-
public FileWriter(String fileName) throws IOException
传入文件的路径字符串,默认方式为覆盖
-
public FileWriter(String fileName, boolean append) throws IOException
传入文件的路径字符串,append为true表示追加,append为false表示覆盖
-
public FileWriter(File file) throws IOException
传入文件的file对象,默认方式为覆盖
-
public FileWriter(File file, boolean append) throws IOException
传入文件的file对象,append为true表示追加,append为false表示覆盖
close
关闭FileWriter流对象
public void close() throws IOException
1 2
| FileWriter fileWriter = new FileWriter("E:\\news.txt"); fileWriter.close();
|
flush
将缓冲区的数据强制一次性存到文件中,注意文件写入完毕后一定要flush一下,不然可能存在部分内容没有写入到文件的情况
public void flush() throws IOException
1 2 3
| FileWriter fileWriter = new FileWriter("E:\\news.txt"); fileWriter.flush(); fileWriter.close();
|
write
将内容写入到对应的文件中
有以下几种常用的重载方法
-
public void write(int c) throws IOException
将c对应的字符写入到文件中
-
public void write(char cbuf[]) throws IOException
将cbuf字符数组写入到文件中
-
public void write(String str) throws IOException
将str字符串写入到字符数组中
-
public void write(String str, int off, int len) throws IOException
将str字符串在[off, off + len - 1] 的内容写入到文件中
1 2 3 4 5
| FileWriter fileWriter = new FileWriter("E:\\news.txt"); fileWriter.write((int)('c')); fileWriter.flush(); fileWriter.close();
|
1 2 3 4 5 6
| FileWriter fileWriter = new FileWriter("E:\\news.txt"); char[] cbuf = new char[]{'h', 'o', 'l', 'l', 'o'}; fileWriter.write(cbuf); fileWriter.flush(); fileWriter.close();
|
1 2 3 4 5 6
| FileWriter fileWriter = new FileWriter("E:\\news.txt"); String str = "hello"; fileWriter.write(str); fileWriter.flush(); fileWriter.close();
|
1 2 3 4 5 6
| FileWriter fileWriter = new FileWriter("E:\\news.txt"); char[] cbuf = new char[]{'h', 'e', 'l', 'l', 'o'}; fileWriter.write(cbuf, 0, 3); fileWriter.flush(); fileWriter.close();
|
getEncoding
获得FileWriter绑定的文件的字符编码
public String getEncoding()
1 2 3 4
| FileWriter fileWriter = new FileWriter("E:\\news.txt"); System.out.println(fileWriter.getEncoding());
|