IO流1

来源:互联网 发布:淮北自然知彼 编辑:程序博客网 时间:2024/05/16 13:06

IO流用来处理设备之间的数据传输

Java对数据的操作时通过流的方式
流按操作数据分为两种:字节流和字符流
流按流向分为:输入流,输出流
unicode中无论什么都已两个字节表示
utf—8用一个如果一个不够用两个,两个不够的话用三个
字符流可以在内部融合编码表,查什么格式的码表可以自己指定
字节流的抽象基类:
InputStream 、OutputStream
字符流的抽象基类:
Reader、Writer
IO异常的处理方式,finally里面一定要对流对象进行非空的判断并
且对流要进行关掉

import java.io.FileWriter;import java.io.IOException;import javax.imageio.IIOException;public class FileWriterDemo {/** * @param args */public static void main(String[] args) {FileWriter fw = null;try { fw = new FileWriter("d:\\demo1.txt");fw.write("abcddjdj");} catch (IOException e) {System.out.println(e.toString());}finally{try {if(fw !=null)fw.close();} catch (IOException e) {e.printStackTrace();}}}}


如何在原来的文件中写入一些东西?
 fw = new FileWriter("d:\\demo.txt",true);//在原来的文章中末尾续写
如何续写的时候换行?
fw.write("\r\nhello io ....."); //加上\r\n就行了
如何读取文件?
import java.io.FileNotFoundException;import java.io.FileReader;public class FileReaderDemo {/** * @param args * @throws Exception  */public static void main(String[] args) throws Exception {// 创建一个文件读取流对象,和指定名称的文件相关联// 要保存该文件是已经存在的,不过不存在,会发生异常FileNotFoundExceptionFileReader fr = new FileReader("demo.txt");// read方法一次读一个字符,并且会自动往下读,作为整数读取的字符int ch = 0;while((ch=fr.read())!=-1){System.out.println((char)ch);}/*while(true){int ch = fr.read();if(ch==-1)break;System.out.println("ch="+(char)ch);}*/fr.close();}}


文件的读取方式二:读取的字符数
import java.io.FileNotFoundException;import java.io.FileReader;public class FileReaderDemo2 {/** * @param args * @throws Exception  */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubFileReader fr = new FileReader("d:\\demo.txt");char [] buf = new char [3];int num = 0;while((num = fr.read(buf))!= -1){System.out.println(new String(buf,0,num));}fr.close();}}


一般的数组定义为1024比较合适:
char [] buf = new char [1024];
读取一个.java文件,并在控制台打印
import java.io.FileReader;public class Test {/** * 把一个.java文件读出并打印在控制台上面 * @param args * @throws Exception  */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubFileReader fr = new FileReader("d:\\LampController.java");char [] buf = new char [1024];int num = 0;while((num = fr.read(buf))!= -1){System.out.print(new String(buf,0,num));}fr.close();}}


 将c盘的一个文本文件复制到d盘
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import javax.management.RuntimeErrorException;/** * 将c盘的一个文本文件复制到d盘 * 复制的原理: * 1、在d盘穿件一个文件,用于存储c盘文件中的数据 * 2、定义读取流和c盘文件关联 * 3、通过不断的读写完成数据存储 * 4、关闭资源 */public  class CopyText {public static void main(String[] args) throws Exception {//copy_1();copy_2();} public static void copy_1() throws Exception{FileWriter fw = new FileWriter("d:\\des.txt");FileReader fr = new FileReader("c:\\demo.txt");int ch = 0;while((ch = fr.read())!= -1){fw.write(ch);}fr.close();fw.close();} public static void copy_2(){ FileWriter fw = null; FileReader fr = null; try { fw = new FileWriter("d:\\des1.txt");     fr = new FileReader("c:\\demo.txt");     char [] buf = new char [1024]; int num = 0; while((num = fr.read(buf))!= -1){fw.write(buf);}} catch (IOException e) {throw new RuntimeErrorException(null, "读写失败");}finally{if(fr!=null){try {fr.close();} catch (IOException e) {e.printStackTrace();}}if(fw!=null){try {fw.close();} catch (IOException e) {e.printStackTrace();}}} }}





原创粉丝点击