Java基础——I/O流

字节输出流

步骤:
1.创建对象
   参数是字符串表示的路径或者是File对象都是可以的
   文件不存在会创建一个,但要保证父级是存在的
   如果文件已经存在,会清空文件
2.写出数据
   write方法的参数是整数,但实际上写到本地文件是 ACSCII 上对应的字符
3.释放资源
   每次使用完流之后都要释放资源

public static void main(String[] args)throws IOException {
		/*
		   字节输入流FileOutputStream
		   实现需求:写出一段文字到本地文件中(除了中文)
		  
		 */
		
		
		//1.创建对象
		//写出 输出流 OutputStream
		//本地文件 File
		FileOutputStream fos = new FileOutputStream("D:\\test.txt");
		//2.写出数据
		fos.write(97);
		//3.释放资源
		fos.close();
	}

字节输出流写出数据的三种方式

void write(int b) —— 一次写一个字节数据

void write(byte[] b) —— 一次写一个字节数组数据

void write(byte[] b, int off, int len) —— 一次写一个字节数组的部分数据

                ↑(数组, 起始索引0开始,个数)

//1.创建对象
FileOutputStream fos = new FileOutputStream("D:\\test.txt");
//2.写数据

//①
fos.write(97);
fos.write(98);

//②		
byte[] bytes = {97,98,99,100,101};
fos.write(bytes);

//③		
byte[] bytes = {97,98,99,100,101};
fos.write(bytes, 1, 2);
		
		
//3.释放资源
fos.close();

(执行其中一种方法时把其他方法注释掉)

换行和续写

换行写:
写出换行符:Windows: \r\n
                      Linux:    \n
                      Mac:      \r
windows系统中,java对回车进行了优化。
虽然完整是的\r\n,但写其中一个也可以达到目的
因为java在底层会补全。

续写:
如果想要续写,打开续写开关
开关位置,创建对象的第二个参数
默认是false,表示关闭续写,此时创建对象会清空文件
手动传递true:表示打开续写,此时创建对象不会清空文件

public static void main(String[] args)throws IOException {
		
	//1.创建对象
	FileOutputStream fos = new FileOutputStream("D:\\test.txt", true); //打开续写开关
	
	//2.写出数据
	//nihao 
	String str1 = "nihao";
	byte[] bytes1 = str1.getBytes();//getBytes()返回字节也就是byte类型数组
	fos.write(bytes1);
	
	String str2 = "\r\n";
	byte[] bytes2 = str2.getBytes();
	fos.write(bytes2);
	
	String str3 = "666";
	byte[] bytes3 = str3.getBytes();
	fos.write(bytes3);
	
	//3.释放资源
	fos.close();
	
	

	}

字节输入流

实现需求:读取文件中的数据

实现步骤:创建对象

                  读取数据

                  释放资源

read():读取一次数据,移动一次指针

public static void main(String[] args)throws IOException {
		
		//1.创建对象//文件内容为abcde
		FileInputStream fis = new FileInputStream("D:\\test.txt");
		//2.读取数据
		int b1 = fis.read();
		System.out.println(b1);
		
		int b2 = fis.read();
		System.out.println(b2);
		
		int b3 = fis.read();
		System.out.println(b3);
		
		int b4 = fis.read();
		System.out.println(b4);
		
		int b5 = fis.read();
		System.out.println((char)b5); //强转,强转成char类型
		
		int b6 = fis.read();
		System.out.println(b6); //读不到了,就会返回-1
		
		//3.释放资源
		fis.close();

	}

提示:

1.创建字节输入对象

如果文件不存在,直接报错,Java为什么会这么设计呢?

        输出流:不存在,创建

        输入流:不存在,报错

因为如果输入流也创建的话就会创建出一个空文件,这样没有意义,所以文件不存在就会报错。

因为最重要的是数据

2.写数据

一次读一个字节,读出来的数据在ASCII上对应的数字。读到文件末尾了,read方法返回-1

3.释放资源

每次使用完流之后都要释放资源

字节输入流循环读取

public static void main(String[] args)throws IOException {
		
		//1.创建对象
        //vbwoevbeowabgvwavo
        //(随便写的)
		FileInputStream fis = new FileInputStream("D:\\test.txt");
		
		//2.循环对象
		int b ;
		while ((b = fis.read()) != -1) { //因为读不到内容,会返回-1
			System.out.print((char)b);
		}
		
		
		//3.释放资源
		fis.close();
}

字符输出流

public static void main(String[] args)throws IOException {
		/*
		 * 第一步:创建对象
		 * public FileWriter(File file)
		 * public FileWriter(String pathname)
		 * public FileWriter(File file,boolean append)
		 * public FileWriter(String pathname, boolean append)
		 
		 * 第二步:读取数据
		 * void write(int c)
		 * void write(String str)
		 * void write(String str, int off ,int len)
		 * void write(char[] cbuf)
		 * void write(char[] cbuf, int off ,int len)
		 * 
		 * 第三步:释放资源
		 * public void close()
		 */
		
		FileWriter fw = new FileWriter("D:\\test.txt", true); //打开了续写开关
		
		fw.write(25105);//根据字符流的编码方式进行编码,把编码之后的数据写到文件中去,UTF-8
							
		fw.write("你好呀???");
		char[] chars = {'a','b','c','我'};
		fw.write(chars);
		
		fw.close();
}

字符输入流

public static void main(String[] args)throws IOException {
		/*
		 * 第一步:创建对象
		 * public FliReader(File file) 		创建字符输入流关联到本地文件
		 * public FliReader(String pathname)创建字符输入流关联到本地文件
		 * 
		 * 第二步:读取数据
		 * pubic int read()					读取数据,读到末尾返回-1
		 * pubic int read(char[] buffer)	读取多个数据,读到末尾返回-1
		 * 
		 * 第三步:释放资源
		 * public void close()				释放资源
		 */
		
		//1.创建对象
        //文件内容为:字节输入流循环读取
		FileReader fr = new FileReader("D:\\duwenjian.txt");
		//2.读取数据
		int len;
		//①
		while ((len = fr.read()) != -1) { //空参read()读取数据
			System.out.print((char)len);
		}
		//②
		char[] chars = new char[2];
		while ((len = fr.read(chars)) != -1) { //有参read()读取数据
			//把数组中的数据变成字符串自行打印
			System.out.print(new String(chars, 0, len)); //可能char数组装不满
		}
		//3.释放资源
		fr.close();

	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值