文件读写

来源:互联网 发布:淘宝回购率在哪里看 编辑:程序博客网 时间:2024/05/03 10:52

public class Demo1_FileInputStream {

/** * @param args * @throws IOException   * read()方法读取的是一个字节,为什么返回是int,而不是byte *  * 00010100 00100100 01000001 11111111 0000000 *  * 10000001    byte类型-1的原码 * 11111110    -1的反码 * 11111111    -1的补码 *  * 00000000 00000000 00000000 11111111 */public static void main(String[] args) throws IOException {    //demo1();    FileInputStream fis = new FileInputStream("xxx.txt");   //创建流对象    int b;    while((b = fis.read()) != -1) {        System.out.println(b);    }    fis.close();}public static void demo1() throws FileNotFoundException, IOException {    FileInputStream fis = new FileInputStream("xxx.txt");   //创建流对象    int x = fis.read();                                     //从硬盘上读取一个字节    System.out.println(x);    int y = fis.read();    System.out.println(y);    int z = fis.read();    System.out.println(z);    int a = fis.read();    System.out.println(a);    int b = fis.read();    System.out.println(b);    fis.close();                                            //关流释放资源}

}

public class Demo2_FileOutputStream {

/** * @param args * @throws IOException  * FileOutputStream在创建对象的时候是如果没有这个文件会帮我创建出来 * 如果有这个文件就会先将文件清空 */public static void main(String[] args) throws IOException {    //demo1();    FileOutputStream fos = new FileOutputStream("yyy.txt",true);    //如果想续写就在第二个参数传true    fos.write(97);    fos.write(98);    fos.close();}public static void demo1() throws FileNotFoundException, IOException {    FileOutputStream fos = new FileOutputStream("yyy.txt");     //创建字节输出流对象,如果没有就自动创建一个    //fos.write(97);                //虽然写出的是一个int数,但是到文件上的是一个字节,会自动去除前三个8位    //fos.write(98);    //fos.write(99);    fos.write(100);    fos.close();}

}

public class Demo3_Copy {

/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {    //demo1();    //demo2();    //demo3();}public static void demo3() throws FileNotFoundException, IOException {    //第二种拷贝,不推荐使用,因为有可能会导致内存溢出    FileInputStream fis = new FileInputStream("致青春.mp3");       //创建输入流对象,关联致青春.mp3    FileOutputStream fos = new FileOutputStream("copy.mp3");    //创建输出流对象,关联copy.mp3    //int len = fis.available();    //System.out.println(len);    byte[] arr = new byte[fis.available()];                     //创建与文件一样大小的字节数组    fis.read(arr);                                              //将文件上的字节读取到内存中    fos.write(arr);                                             //将字节数组中的字节数据写到文件上    fis.close();    fos.close();}public static void demo2() throws FileNotFoundException, IOException {    FileInputStream fis = new FileInputStream("致青春.mp3");       //创建输入流对象,关联致青春.mp3    FileOutputStream fos = new FileOutputStream("copy.mp3");    //创建输出流对象,关联copy.mp3    int b;    while((b = fis.read()) != -1) {                             //在不断的读取每一个字节        fos.write(b);                                           //将每一个字节写出    }    fis.close();                                                //关流释放资源    fos.close();}public static void demo1() throws FileNotFoundException, IOException {    FileInputStream fis = new FileInputStream("双元.jpg");        //创建输入流对象,关联双元.jpg    FileOutputStream fos = new FileOutputStream("copy.jpg");    //创建输出流对象,关联copy.jpg    int b;    while((b = fis.read()) != -1) {                             //在不断的读取每一个字节        fos.write(b);                                           //将每一个字节写出    }    fis.close();                                                //关流释放资源    fos.close();}

}

public class Demo4_ArrayCopy {

/** * @param args * 第三种拷贝 * 定义小数组 * @throws IOException  */public static void main(String[] args) throws IOException {    //demo1();    //demo2();    FileInputStream fis = new FileInputStream("致青春.mp3");    FileOutputStream fos = new FileOutputStream("copy.mp3");    byte[] arr = new byte[1024 * 8];    int len;    while((len = fis.read(arr)) != -1) {                //如果忘记加arr,返回的就不是读取的字节个数,而是字节的码表值        fos.write(arr,0,len);    }    fis.close();    fos.close();}public static void demo2() throws FileNotFoundException, IOException {    FileInputStream fis = new FileInputStream("xxx.txt");    FileOutputStream fos = new FileOutputStream("yyy.txt");    byte[] arr = new byte[2];    int len;    while((len = fis.read(arr)) != -1) {        fos.write(arr,0,len);    }    fis.close();    fos.close();}public static void demo1() throws FileNotFoundException, IOException {    FileInputStream fis = new FileInputStream("xxx.txt");    byte[] arr = new byte[2];    int a = fis.read(arr);                      //将文件上的字节读取到字节数组中    System.out.println(a);                      //读到的有效字节个数    for (byte b : arr) {                        //第一次获取到文件上的a和b        System.out.println(b);    }    System.out.println("-----------------------");    int c = fis.read(arr);    System.out.println(c);    for (byte b : arr) {        System.out.println(b);    }    fis.close();}

}

public class Demo5_BufferCopy {

/** * @param args * @throws IOException  * close方法 * 具备刷新的功能,在关闭流之前,就会先刷新一次缓冲区,将缓冲区的字节全都刷新到文件上,再关闭,close方法刷完之后就能写了 * flush方法? * 具备刷新的功能,刷完之后还可以继续写 */public static void main(String[] args) throws IOException {    //demo1();    //flush和close方法的区别    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("致青春.mp3"));    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.mp3"));    int b;    while((b = bis.read()) != -1) {        bos.write(b);    }    bis.close();    bos.close();}public static void demo1() throws FileNotFoundException, IOException {    FileInputStream fis = new FileInputStream("致青春.mp3");               //创建输入流对象,关联致青春.mp3    FileOutputStream fos = new FileOutputStream("copy.mp3");            //创建输出流对象,关联copy.mp3    BufferedInputStream bis = new BufferedInputStream(fis);             //创建缓冲区对象,对输入流进行包装让其变得更加强大    BufferedOutputStream bos = new BufferedOutputStream(fos);    int b;    while((b = bis.read()) != -1) {        bos.write(b);    }    bis.close();    bos.close();}

}

public class Demo6_Chinese {

/** * @param args * * 字节流读取中文的问题        * 字节流在读中文的时候有可能会读到半个中文,造成乱码     * 字节流写出中文的问题        * 字节流直接操作的字节,所以写出中文必须将字符串转换成字节数组         * 写出回车换行 write("\r\n".getBytes()); * @throws IOException  */public static void main(String[] args) throws IOException {    //demo1();    FileOutputStream fos = new FileOutputStream("zzz.txt");    fos.write("我读书少,你不要骗我".getBytes());    fos.write("\r\n".getBytes());    fos.close();}public static void demo1() throws FileNotFoundException, IOException {    FileInputStream fis = new FileInputStream("yyy.txt");    byte[] arr = new byte[4];    int len;    while((len = fis.read(arr)) != -1) {        System.out.println(new String(arr,0,len));    }    fis.close();}

}

public class Demo7_TryFinally {

/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {    //demo1();    try(        FileInputStream fis = new FileInputStream("xxx.txt");        FileOutputStream fos = new FileOutputStream("yyy.txt");        MyClose mc = new MyClose();    ){        int b;        while((b = fis.read()) != -1) {            fos.write(b);        }    }}public static void demo1() throws FileNotFoundException, IOException {    FileInputStream fis = null;    FileOutputStream fos = null;    try {        fis = new FileInputStream("xxx.txt");        fos = new FileOutputStream("yyy.txt");        int b;        while((b = fis.read()) != -1) {            fos.write(b);        }    }finally {        try{            if(fis != null)                fis.close();        }finally {                          //try fianlly的嵌套目的是能关一个尽量关一个            if(fos != null)                fos.close();        }    }}

}

class MyClose implements AutoCloseable {
public void close() {
System.out.println(“我关了”);
}
}

原创粉丝点击