美文网首页
java I/O读写

java I/O读写

作者: Zebraaa | 来源:发表于2018-08-18 21:42 被阅读0次

    简单的Java 文件进行读写:一种是stream 一种是reader
    第一种是面向字节形式的I/O
    第二种就是提供Unicode与面向字符的I/O

    //字符流 Reader Writer
    //文件字符流 FileReader FileWriter
    //缓存字符流 BufferedReader BufferedWiter

    //桥接流
    //InputStreamReader
    //OutputStreamWriter

    //字节流InputStream OutputStream
    //文件字节流 FileInputStrean FileOutputStream
    //缓存字节流 BufferedInputStream BufferedOutputStream
    //数据字节流 DataInputStream DataOutputStream
    //对象输入流 ObjectInputStream ObjectOutputStream

    读:根据InputStream

    File file = new File("C:/Users/chen/Desktop/123.txt");
    InputStream iStream = new FileInputStream(file);
                byte[] b = new byte[iStream.available()];
                int len = 0;
                while((len = iStream.read(b))!=-1){
                    System.out.println(new String(b,0,len));
                }
    iStream.close();
    

    读:BufferedReader

    BufferedReader br = new BufferedReader(new FileReader(file));
                String str = null;
                StringBuffer sb = new StringBuffer();
                while((str = br.readLine())!=null){
                    sb.append(str+"\n");
                }
                System.err.println(sb.toString());
                br.close();
    

    写文件:OutputStream

    File file2 = new File("C:/Users/chen/Desktop/test.txt");
    OutputStream oStream = new FileOutputStream(file2);
                oStream.write(b);
                oStream.close();
    

    相关文章

      网友评论

          本文标题:java I/O读写

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