字符流
?
1????? 字符輸入流Reader
l? read():讀取單個字符并返回
l? read(char[]):將數據讀取到數組中,并返回讀取的個數。
public class Demo01 {public static void main(String[] args) throws IOException {//明確數據源FileReader fr=new FileReader("D:\\test\\s.txt");//一個字符一個字符的讀/*int len=0;while((len=fr.read())!=-1){System.out.print((char)len+" ");}*/ //一個字符數組的讀char[]ch=new char[2];int len=0;while((len=fr.read(ch))!=-1){System.out.println(new String(ch,0,len));}//釋放資源 fr.close();} }?2 字符輸出流Writer
flush():將流中的緩沖區緩沖的數據刷新到目的地中,刷新后,流還可以繼續使用。
close():關閉資源,但在關閉前會將緩沖區中的數據先刷新到目的地,否則丟失數據,然后在關閉流。流不可以使用。如果寫入數據多,一定要一邊寫一邊刷新,最后一次可以不刷新,由close完成刷新并關閉。
public class Demo02 {public static void main(String[] args) throws IOException {// 明確目的地FileWriter fw = new FileWriter("D:\\test\\b.txt");// 寫一個字符fw.write(100);fw.flush();//寫字符數組char[]ch={'你','是','啥','a'};fw.write(ch,2,2);fw.write("你好啊".toCharArray());fw.write("你好啊");fw.flush();// 釋放資源 fw.close();} }3? 字符流復制文本
?
public class Demo03 {public static void main(String[] args) throws IOException {//文件復制//明確數據源FileReader fr=new FileReader("D:\\test\\b.txt");//明確目的地FileWriter fw=new FileWriter("D:\\test\\e\\b.txt");//開始復制char []ch=new char[1024];int len=0;while((len=fr.read(ch))!=-1){fw.write(ch,0,len);fw.flush();}fr.close();fw.close();} }?
轉載于:https://www.cnblogs.com/quanjunkang/p/10655390.html
總結
- 上一篇: UML-2-迭代、进化和敏捷
- 下一篇: 物理、线性、虚拟、逻辑、有效地址