IO流----------------------IO流理解以及流常用的读取写入文件的方法

流的本质是字节
(1)我觉得应该熟悉的流没有那么复杂,也没有那么多。
(2)字符流比字节流方便,只要是打开文件,人能看懂就可以用字符流,和字节流功能一样
(3)缓冲流是JAVA提供的API,通过缓冲流读写东西比不通过快得多
输入流:FileInputStream(字节流读文件,配合着BufferedInputStream使用)、FileReader(字符流读文件,配合着BufferedReader使用)
输出流:FileOutputStream(字节流写文件,配合着BufferOutoutStream使用)、FileWriter(字符流写文件,配合着BufferedWriter使用)


public class Demo {
public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("F:\\b.txt"); //读取文件
    int read = fis.read(); 
    int read2 = fis.read();
    int read3 = fis.read();
    System.out.println(read);  //将会输出每一个字节所代表的ascii码
    System.out.println(new String ( new byte[] {(byte)read,(byte)read2,(byte)read3}));  //将他合起来组成一个完整的字


}
}

1.使用字符流复制文件

public class Practice1 {
public static void main(String[] args) throws IOException  {


    try(BufferedReader br = new BufferedReader(new FileReader("E:\\result.txt")); //通过文件缓冲流读文件
        BufferedWriter bw   = new BufferedWriter(new FileWriter("E:\\result3.txt"))) {
        char[] ch = new char[100];
        int len;
        while((len = br.read(ch))!=-1) {//以一个字符数组为中介存储或者读写东西,也有防止数组浪费的好处
            bw.write(ch);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}
}

2.字节流复制数据

public class Practice2 {
public static void main(String[] args) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Hailong\\Desktop\\自己.jpg"));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Hailong\\Desktop\\自己3.jpg"));
    byte[] b = new byte[1024];
    int len;
    while((len = bis.read(b))!=-1) {//字符流通过字符数组,字节流当然通过字节数组
        bos.write(b);
        bos.flush();
    }
    bos.close();
    bis.close();
}

3.字符和字节的转换

public class Test1 {
    public static void main(String[] args) {
        //字符串   a  c  v 
        String str1 = 12 + "" ;
        //String str2 = 'a' + "" ;
        //将字符串转换成字节数组,可以输出一下更好理解流的本质
        byte[] bytes = "abc".getBytes();
        for (int j = 0; j < bytes.length; j++) {


            System.out.println(bytes[j]);

        }
        //有参构造  将字节数组 编程 字符串
        String str = new String(bytes);
        //重写了toString  打印字符串的内容
        System.out.println(str.toString());
}
}

4.查找后缀名是.txt的文件


public class Test1 {
public static void main(String[] args) {
    getDir("F:\\", ".txt");

}
public static void getDir(String path,String latter) {
File f = new File(path);
if(f.isFile()) { //如果是文件的话就可以输出路径
    if(f.getName().endsWith(latter)) {
        System.out.println(f.getAbsolutePath());

    }

}
else {
    File[] list = f.listFiles();//不是文件的话必须得深入文件夹去查看
    if(f.length()>0&&list!=null) {
        for (File file : list) {//遍历文件夹中的东西
            getDir(file.getAbsolutePath(), latter);
        }

    }
}
}
}

5.删除某一个文件 //和查找一样

package file;

import java.io.File;
import java.io.IOException;

public class Test2 {
public static void main(String[] args) throws IOException {
    removeDir("F:\\practice");
}
public static void removeDir(String path) throws IOException {
    File file = new File(path);
    if(file.isFile()) {

            file.delete();

    }
    else {
        File[] listFiles = file.listFiles();
        for (File file2 : listFiles) {
            removeDir(file2.getAbsolutePath());
        }
        file.delete();

    }

}
}

6.一个项目中的实例某一块运用

public static void main(String[] args) {
    List<ShoppingBean> list = new ArrayList<>();
    try(BufferedReader br = new BufferedReader(new FileReader("D:\\a.txt"));) {
        String str;
        while((str=br.readLine())!=null) { //可以一行一行读取
            String[] split = str.split(",");
             int id = Integer.parseInt(split[0]);
             String name = split[1];
             double price = Double.parseDouble(split[2]);
             int number = Integer.parseInt(split[3]);
             ShoppingBean bean = new ShoppingBean();
             bean.set(id, name, price, number);
             list.add(bean);



        }

7.解决乱码问题,看源代码是什么编码,使用包装类解决

public void downLoad() throws IllegalArgumentException, IOException {
        FSDataInputStream input = fs.open(new Path("/upload.txt"));
        BufferedReader  br = new BufferedReader(new InputStreamReader(input,"utf-8")); //使用包装类修改编码
         BufferedWriter bw = new BufferedWriter(new FileWriter("e:/upload.txt"));
         String str = null;
         while((str = br.readLine())!= null) {
             bw.write(str);
         }
         bw.close();
         br.close();

如果是直接读的话,就是默认系统的gbk编码

FileOutputStream fos = new FileOutputStream("e:/download.txt");
         byte[] b = new byte[1024];
         int len;
         while((len = input.read(b))!=-1) {
          fos.write(b,0,len);
          }
         fos.close();
         input.close();

猜你喜欢

转载自blog.csdn.net/qq_41166135/article/details/81813057