美文网首页
字节流 2016.10.9

字节流 2016.10.9

作者: 大虾咪 | 来源:发表于2016-10-09 17:18 被阅读15次
    package 字节流;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    /*字符流 

    * FileWriter

    * BufferedReader

    * BufferedWriter

    *

    * 字节流

    * InputStream

    * OutputStream

    *

    * 想要操作图片数据,这时就要用到字节流

    */

    public class FileOutputStreamDemo {

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    //写字节

    // FileOutputSteamFun();

    //读字节

    FileInputSteamFun_3();

    }

    public static void FileOutputSteamFun(){

    FileOutputStream fs = null;

    try {

    fs = new FileOutputStream("src/fox.txt");

    try {

    fs.write("abcdeddddd".getBytes());

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    } catch (FileNotFoundException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }finally {

    try {

    fs.close();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    public static void FileInputSteamFun_1(){

    FileInputStream fis = null;

    try {

    fis = new FileInputStream("src/fox.txt");

    int ch = 0;

    try {

    while ((ch=fis.read())!=-1) {

    System.out.println((char)(ch));

    }

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    } catch (FileNotFoundException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }finally {

    try {

    fis.close();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    public static void FileInputSteamFun_2(){

    FileInputStream fis = null;

    try {

    fis = new FileInputStream("src/fox.txt");

    byte[] buf = new byte[1024];

    int len = 0;

    // try {

    // System.out.println(fis.available());//打印字节数的个数

    // } catch (IOException e) {

    // // TODO Auto-generated catch block

    // e.printStackTrace();

    // }

    try {

    while ((len=fis.read(buf))!=-1) {

    System.out.println(new String(buf,0,len));

    }

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    } catch (FileNotFoundException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }finally {

    try {

    fis.close();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    public static void FileInputSteamFun_3(){

    FileInputStream fis = null;

    try {

    fis = new FileInputStream("src/fox.txt");

    try {

    byte[] buf = new byte[fis.available()];

    fis.read(buf);

    System.out.println(new String(buf));

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }//打印字节数的个数

    } catch (FileNotFoundException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }finally {

    try {

    fis.close();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    }

    相关文章

      网友评论

          本文标题:字节流 2016.10.9

          本文链接:https://www.haomeiwen.com/subject/foopyttx.html