java--IO流

IO流,输入输出流,按传送数据分为字节流和字符流,其实字符流=字节流+字符编码。我的理解:创建一个流就是要建立通道,准备底层那些总线跟芯片的选通,通道建立好后只需往通道(线路)发送脉冲就能达到字节的传送。通道只有两种,一种是写通道,另一种是读通道,输入(input)是往内存中读入,输出(output)是往硬盘等存储设备写数据。读写完后要关闭通道,因为开启通道会占用资源。

import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
	/*
	 * 需求:将一些文字存储到硬盘一个文件中
	 * 记住:如果要操作文字数据,建议优先考虑字符流
	 * 而且要将数据从内存写到硬盘上,要使用字符流中的输出流。writer
	 * 硬盘的数据基本体现是文件。希望找到一个可以操作文件的writer。
	 * 找到了FileWriter
	 */
	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//创建一个可以往文件中写入字符数据的字符输出流对象。
		/*
		 * 既然是往一个文件中写入文字数据,那么在创建对象时就必须明确该文件(用于存储数据的目的地)。
		 * 如果文件不存在,则会自动创建。
		 * 如果文件存在,则会被覆盖
		 * 
		 * 如果构造函数中加入true,可以实现对文件进行续写!
		 */
//		FileWriter fw = new FileWriter("demo.txt");
		FileWriter fw = new FileWriter("demo.txt",true);
		
		/*
		 * 调用Writer对象的writer(string)方法,写入数据。
		 * 其实数据写入到临时存储缓冲区中
		 */
//		fw.write("abcde");
		fw.write("abcd"+LINE_SEPARATOR+"HAHAHA");
		/*
		 * 进行刷新,将数据直接写入到目的地中。
		 */
		fw.flush();
		
		/*
		 * 关闭流,关闭资源,因为较低层调用的是windows的资源,所以要关闭资源,否则会导致系统变慢。关闭流之前会调用flush()刷新缓冲中的数据到目的地
		 */
		fw.close();
	}

}

读写的过程中有可能会发生IO异常,异常处理代码

import java.io.FileWriter;
import java.io.IOException;
public class IOExceptionDemo {
	
	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args)  {
		
		FileWriter fw = null;
		try{
			fw = new FileWriter("demo.txt",true);
		
			fw.write("abcd"+LINE_SEPARATOR+"HAHAHA");
	
		
	}catch (IOException e){
		System.out.println(e.toString());
	}finally{
		if(fw != null)
		try {
			fw.close();
		} catch (IOException e) {
			throw new RuntimeException("关闭失败");
		}
			
	}

	}
}

读一次返回一个字符的正整数表示,当读到文件的结尾时,会返回-1,windows在存储文本比如txt时会在文本的开头和结尾加标志符,当读文件read()时遇到标志符则返回-1

public static void main(String[] args) throws IOException {
		//1.创建读取字符流数据的流对象。
				/*
				 * 在创建读取流对象时,必须要明确被读取的文件。一定要确定该文件是存在的。
				 * 
				 * 用读取流关联一个已存在文件。
				 */
				FileReader fr = new FileReader("demo.txt");
				
				int ch = 0;
				while((ch=fr.read())!=-1){
					System.out.println((char)ch);
				}
				//用Reader中的read方法读取字符
//				int ch = fr.read();
//				System.out.println(ch);
//				int ch2 = fr.read();
//				System.out.println(ch2);
//				int ch3 = fr.read();
//				System.out.println(ch3);
				fr.close();

	}
另外一种读方式read(buf),传入一个数组来存放读入的字符,返回的是读入的字符的个数,当数组不够大装不下时,就要读多几次,直到读完。当已经读完了,再次调用读操作则会返回-1,因为已经到结尾了。
	public static void main(String[] args) throws IOException  {
		
				FileReader fr = new FileReader("demo.txt");
				/*
				 * 使用read(char[])读取文本数据
				 * 
				 * 先创建字符数组
				 */
				char[] buf = new char[1024];
				int len = 0;
				while((len = fr.read(buf)) != -1){
					System.out.println(new String(buf,0,len) +" "+ len);//打印了ab 2 (文件中只有ab)
					
					
				}
				int num = fr.read(buf);
				System.out.println(num);//此时num = -1
				/*
				int num = fr.read(buf);//将读取到的字符存储到数组中。
				System.out.println(num+":"+new String(buf,0,num));
				int num1 = fr.read(buf);//将读取到的字符存储到数组中。
				System.out.println(num1+":"+new String(buf,0,num1));
				int num2 = fr.read(buf);//将读取到的字符存储到数组中。
				System.out.println(num2+":"+new String(buf));
				*/
				fr.close();
	}

后续


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值