java基础之io流

file类

file类只能用于获取文件的名字,创建文件,目录等等的操作,不能对文件内容进行读写。


输入流和输出流

输入流是读的操作,输出流是写的操作。

输入流父类: InputStream Reader  (以这两个结尾的都是输入流的子类)

输出流父类: OutputStream Writer  (以这两个结尾的都是输出流的子类)


字节流和字符流

字节流常用于音乐 ,视频等操作,比如类: FileInputStream ,FileOutputStream。

字符流长用于文件等操作。比如类: FileReader  ,FileWriter  。

前面加BUffered 来缓冲 可以加快处理速度
读代码:
public class Read {

	private static FileReader fr = null;
	private static BufferedReader br = null;

	/**
	 * @param args 
	 * 在这里用到buffer 缓冲流 来包装 节点流 具体见01_IO之 file的filecreat类
	 * 
	 * 此类做的方法是读文件文本内容
	 */
	public static void main(String[] args) {
		File fileRead = new File("D:\\xuexi\\javaio\\test.txt");
		try {
			fr = new FileReader(fileRead);
			br = new BufferedReader(fr);
			
			try {
				String readLine = br.readLine();
				while (readLine != null) {
					System.out.println(readLine);
					readLine = br.readLine();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			
			try {
				br.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
写代码:
public class Write {

	private static BufferedWriter bw = null;

	/**
	 * @param args 
	 * 在这里用到buffer 缓冲流 来包装 节点流 具体见01_IO之 file的filecreat类
	 * 
	 * 此类做的方法是写文件文本内容
	 */
	public static void main(String[] args) {
		File fileWrite = new File("D:\\xuexi\\javaio\\test.txt");
        try {
			bw  = new BufferedWriter(new FileWriter(fileWrite,true));
			bw.write(new Date()+"我是插入的数据");
			bw.flush();//写操作先flush一遍
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翅膀君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值