IO操作

来源:互联网 发布:下载个淘宝 编辑:程序博客网 时间:2024/05/20 10:56
一.字节流与字符流
   操作步骤:
       *1.首先通过File类对象找到操作文件路径(没有要创建)
       *2.通过字节流或字符流的子类为字节流或字符流的对象实例化(向上转型)
       *3.执行读写操作
       *4.关闭操作的资源close();
   1.字节流
    1-字节输出流:OutputStream:
       实例化:OutputStream output=new FileOutputStream(file)throws Exception
       实例化(不替换)OutputStream output=new FileOutputStream(file,true)
       进行输出:*单字节输出:public abstract void write(int b);
                           *一组字节数组:public void write(byte [] b);
                           *输出部分字节数组:public void write(byte[] b,int off,int len);
    2-字节输入流:InputStream:
       进行读取:*读取单个字节:public abstract int read()throws IOException;
                           *对区多个字节:public int read(byte[] b);

                           *读取指定多个字节:public int read(byte[] b,int off,int len); 

File file =new File("D:/hello.txt"); //字节流 //字节输入流InputStream input=new FileInputStream(file);byte[] bs=new byte[1024];int len=input.read(bs);//字节长度System.out.println(new String(bs,0,len)); input.close();字节输出流OutputStream output=new FileOutputStream(file,true);String str="十步杀一人,千里不留行。";output.write(str.getBytes());input.close();
   2.字符流:
      1-字符输出流:Writer
       输出一个字符串(重):public void write(String str)throws IOException;
      2-字符输入流:Reader:
       读取字符数组:public int read(char[] ch);
//字符流://字符输入流Reader re=new FileReader(file);char [] ch=new char[1024];int len=re.read(ch);System.out.println(new String(ch,0,len));re.close();//字符输出流:Writer wr=new FileWriter(file);String a="飞流直下三千尺,疑是银河落九天。";wr.write(a);wr.flush();wr.close();
  3.文件的复制:

public class Test3 {public static void main(String[] args) throws IOException {//复制文件:InputStream input=new FileInputStream(new File("D:/x.jpg"));OutputStream output=new FileOutputStream(new File("D:/workspace/as.jpg"));byte[] bs=new byte[1024];int len;while((len=input.read(bs))!=-1) {output.write(bs,0,len);}input.close();output.close();}}

二、转换流:
   *将字节输出流变为字符输出流:OutputStreamWriter

   *将字节输入流变为字符输入流:InputStreamReader

//转换流://1.将字节输出流转换为字符输出流OutputStream output=new FileOutputStream(file);Writer out=new OutputStreamWriter(output);out.write("天涯明月刀");out.close();//2.将字节输入流转换为字符输入流InputStream input=new FileInputStream(file);Reader in=new InputStreamReader(input);char [] ch=new char[1024];int len=in.read(ch);System.out.println(new String(ch,0,len));
三、打印流:

  *字节打印流:PrintStream

  *字符打印流:PrintWriter

PrintStream p=new PrintStream(new FileOutputStream(file));p.print("莫道黯然销魂,");p.print("何处柳暗花明。");

四、缓冲区操作:BufferedReader

缓冲区的读取在IO包中:BufferedInputStream\BufferedReader两个类:

优先使用BufferedReader:它可以读取一行数据。

readLine()方法返回String类型

File file=new File("D:/hello.txt");FileWriter writer=new FileWriter(file);BufferedWriter buff=new BufferedWriter(writer);buff.write("zhangsan");buff.newLine();buff.write("lisi");buff.close();FileReader reader =new FileReader(file);BufferedReader buffreader =new BufferedReader(reader);String str;while((str=buffreader.readLine())!=null) {System.out.println(str);}buffreader.close();reader.close();}}




原创粉丝点击