星空网 > 软件开发 > Java

java中IO流相关知识点

(一) 下边使用outputStream字节输出流进行写操作

package zdbIO;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutputStreamDemo1 {

    /**
     * @throws IOException
     * @throws IOException
     *
     */
    public static void main(String[] args) throws IOException{
        
        /**
         * 使用IO流的具体步骤:
         *             1.使用file找到要操作的文件
         *             2.(使用字节流或字符流的子类来实例化inputStream、outStream、reader、writer)
         *             3.进行读写操作
         *             4.关闭流,除BufferedReader例外
         */
        
        File file = new File("f:"+File.separator+"zdb1.txt");//使用file找到要操作的文件
        OutputStream out = null;
        out = new FileOutputStream(file,true);//使用OutputStream的子类进行实例化
        String str = "XXX的十年人生规划,一定要有个计划这样你的人生才会有明确的方向 不至于迷失。";//要输出的信息
        byte b[] = str.getBytes();//将str变为byte数组
        out.write(b);//写入数据
        out.close();//关闭流
        
    }
    
}

(二)下边使用inputStream字节流进行读操作的第一种方法:


package zdbIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 通过inputStream字节流来进行读操作
 *
 */
public class InputStreamDemo {
    public static void main(String[] args) throws Exception {
        //注意此文件必须存在否则会发生java.io.FileNotFoundException异常
        //File.separator表示分隔符,其中在Windows中表示\,在unix表示/,这样可以跨平台
        File file = new File("f:"+File.separator+"zdb1.txt");
        InputStream input = null;
        input = new FileInputStream(file);
        byte b[] = new byte[1024];//开辟一块内存用来存储读取的内容
        int len = input.read(b);//将文件读到字符数组中
        //其中new String(byte[]bytes,int offset,int length),
        //表示将创建一个字符串,从offset为开始,长度为length
        System.out.println(new String(b,0,len));
        input.close();
        
    }

}


/**
     * 这种方法中开辟空间的大小受到限制,因此可以根据文件的大小来开辟空间的大小,
     * 即可以使用byte b[] = new byte[(int)file.length()],来创建开辟的空间,
     * 然后可以通过read()方法来一个个读取。
     *
     */

(二)下边使用inputStream字节流进行读操作的第二种方法

package zdbIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo1 {
    public static void main(String[] args) throws Exception {
        File file = new File("f:"+File.separator+"zdb1.txt");
        InputStream input = null;
        input = new FileInputStream(file);
        byte b[] = new byte[(int)file.length()];
        for(int i=0;i<b.length;i++){
            b[i] = (byte)input.read();
        }
        System.out.println(new String(b));    
        input.close();
    }

}


(三)下边使用Reader字节流进行读操作的第一种方法:
package zdbIO;

import java.io.File;

import java.io.FileReader;
import java.io.Reader;

public class ReaderDemo {
    public static void main(String[] args) throws Exception {
        File file = new File("f:"+File.separator+"zdb1.txt");
        Reader in = null;
        in = new FileReader(file);
        char c[] = new char[1024];
        int len = in.read(c);
        System.out.println(new String(c,0,len));
        in.close();
    }

}

(三)下边使用Reader字节流进行读操作的第二种方法:
package zdbIO;

import java.io.File;

import java.io.FileReader;
import java.io.Reader;

public class ReaderDemo {
    public static void main(String[] args) throws Exception {
        File file = new File("f:"+File.separator+"zdb1.txt");
        Reader in = null;
        in = new FileReader(file);
        char c[] = new char[1024];
        int len = in.read(c);
        System.out.println(new String(c,0,len));
        in.close();
    }

}
(四)下边使用Writer字节流进行写操作的:
package zdbIO;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriterDemo {
    /**
     * 可以通过字符流writer来进行写操作
     * @throws IOException
     *
     */
    
    public static void main(String[] args) throws IOException {
        File file = new File("f:"+File.separator+"zdb1.txt");
        Writer writer = null;
        writer = new FileWriter(file, true);
        String str = "你今天好好学习了吗?你今天进步了吗学会每天一番思";
        writer.write(str);
        writer.close();

    }

}
(五) 字符流和字节流的区别:
    1.字符流中写操作Writer中,操作完成后需要关闭流或者刷新,否则写入不成功。
    2.字节流直接与文件进行交互的,不需要使用缓存;而字符流是通过缓存与文件进行交互的;
    3.在传输和硬盘上保存的内容都是字节形式,所以字节形式操作较多;而操作文件的时候使用字符流较多;
















































 




原标题:java中IO流相关知识点

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

旅行结婚大概多少钱价格:https://www.vstour.cn/t/旅行结婚大概多少钱价格.html
旅行结婚大概费用多少钱:https://www.vstour.cn/t/旅行结婚大概费用多少钱.html
旅行结婚大概费用价格:https://www.vstour.cn/t/旅行结婚大概费用价格.html
旅行结婚大概要多少钱多少钱:https://www.vstour.cn/t/旅行结婚大概要多少钱多少钱.html
旅行结婚大概要多少钱价格:https://www.vstour.cn/t/旅行结婚大概要多少钱价格.html
旅行结婚到哪里好多少钱:https://www.vstour.cn/t/旅行结婚到哪里好多少钱.html
珠海长隆横琴剧院在哪里?附禁带物品:https://www.vstour.cn/a/334173.html
珠海长隆横琴剧院坐公交怎么去:https://www.vstour.cn/a/334174.html
相关文章
我的浏览记录
最新相关资讯
跨境电商服务平台 | 深圳旅行社 | 东南亚物流