java IO流(字节流与字符流对文件的处理)

/*

* IO流:用来处理设备之间的数据传输,java对数据的操作时通过流的方式,java用于操作流的对象都在IO包中。

* 分类:* 流按操作数据分为两种:字节流和字符流。* 流按流向分为:输入流,输出流。* 字节流的抽象基类:inputStream,OutputStream* 字符流的抽象基类:Reader和Writer。* 区别:字节流处理的byte类型的数据,字符流处理的是String类型的数据。* 注意:这四个类派生出来来的子类都是以基类为后缀名:* FileinputStream,FileOutputStream

*FileReader和 FileWriter。

*IO流对文件的操作:*1.字符流对文件进行读写,通过FileReader(OutputStreamWriter的子类)和 FileWriter(IntputStreamWriter的子类)两个方法。 2.字节流对文件进行读写,通过InputStreamWriter和OuterputStream两个方法。

*/

import java.io.*;public class FileWriterDemo {public static void main(String[] args) throws IOException {//writerDemo();//FileOutputStreamDemo();}//字节流对文件进行读写public static void FileOutputStreamDemo(){//定义字节数组byte[] b1={‘b’,’c’,’a’};//定义字节流对象FileOutputStream fos=null;try{//给字节流对象创建要操作的文本//如果该文件已存在,将会将其覆盖。//如果在构造函数里传递一true参数,代表不覆盖已有的文件。并在已有的文件的末尾,续写文件。fos=new FileOutputStream("c:\\demo6.txt",true);//调用write方法将字节数组里的字符写入字节文件对象里。fos.write(b1,0,b1.length);}catch (IOException e){System.out.println(e.toString());}finally{try {//关闭流fos.close();} catch (Exception e2) {// TODO: handle exception}}}//字符文件操作流 public static void writerDemo(){//1.创建一个FileWriter对象,对象已初始化必须确定要操作的文件(创建文本),//如果该文件已存在,将会将其覆盖。//如果在构造函数里传递一true参数,代表不覆盖已有的文件。并在已有的文件的末尾,续写文件。FileWriter fw=null;try{fw=new FileWriter("c:\\demo5.txt",true);//2.调用流操作对象的write方法将字符串写入流中。//在windos文本下,不能识别\n为换行符,只能识别\r\n为换行符。fw.write("ljwer\r\noiusdjfldsklollolololol");//3.刷新流对象中缓冲的数据,将数据刷到目的文件中。fw.flush();//4.刷新流对象中缓冲的数据,将数据刷到目的文件中,并且关闭流。}catch(IOException e){System.out.println(e);}try {if(fw!=null)fw.close();} catch (IOException e1) {System.out.println(e1);}//表示流中不能再写入数据,会报出异常。//fw.write("收到两份洪武偶额我家附近");//fw.flush();}

}

/** 一.字节流与字符流对文件的读取。* 通过FileReader和FileOutputStream类进行操作。*/import java.io.*;public class FileReaderDemo {public static void main(String[] args) throws IOException {//FileOutPutStreamDemo();//FileReaderDemo()}//字节流对文件的读取。public static void FileOutPutStreamDemo() throws IOException{//定义一个字节数组。byte[] b1=new byte[1024];//定义字节流读取对象,并且确定要操作的文件,要保证该文件是已经存在的,如果不存在,会发生异常。FileInputStream fis=new FileInputStream("c:\\demo5.txt");int num=0;//调用read方法对fis进行读取,返回文件对象里字节数的总合。while((num=fis.read(b1))!=-1){//不断的对文件里的字节数进行读取,当读取完之后,将其转换成字符串进行打印,最后进行最后一轮读取//读取空字节,返回-1,跳出循环。sop(num);sop(new String(b1,0,num));}}public static void FileReaderDemo() throws IOException{//创建一个文件读取流对象,和指定名称的文件相关。//要保证该文件是已经存在的,如果不存在,会发生异常。FileReader fr=new FileReader("c:\\demo5.txt");//第二种方式:通过字符数组进行读取。//定义一个字符数组,用于存储读到字符。//该read(char[])返回的是读到的字符个数。char[] buf=new char[1024];int num=0;while((num=fr.read(buf))!=-1){sop(num);sop(new String(buf,0,num));}//第一种读取方式/*//调用read方法,一次读取一个字符,,而且会自动往下读。//读到末尾会返回-1.int ch=0;while((ch=fr.read())!=-1){sop("ch="+(char)ch);}fr.close();*/}public static<T> void sop(T t){System.out.println(t);}}

/** IO异常处理方式。* 抛出throws并不合适,所以需要对可能发生异常的代码进行try catch()处理。*/import java.io.*;public class FileWriterDemo2 {public static void main(String[] args){//定义写入字节流对象FileWriter fw=null;try{//给字节流对象创建要操作的文本//如果该文件已存在,将会将其覆盖。fw=new FileWriter("c:\\demo.txt"); fw.write("rwerwe");}catch(IOException e){System.out.println(e.toString());}finally{try {if(fw!=null)fw.close();} catch (IOException e2) {System.out.println(e2.toString());}}}}

既有美妙的风景,也会有称不上景、只有风的地方。

java IO流(字节流与字符流对文件的处理)

相关文章:

你感兴趣的文章:

标签云: