6.Java基础:常见IO流----->字节流:FileInputStream、FileOutputStream

OutputStream:字节输出流它做写入操作

FileOutputStream ----完成文件的写入操作

1.构造

new FileOutputStream(File file);

new FileOutputstream(File file,boolean append);

new FIleOutputStream(String name);

new FileOutputStream(String name,boolean append);


2.写入操作

1.write(int code)

它代表的是ASCII码.

2.write(byte[] b);

它代表一次可以写入多个字节

3.write(byte[] b,int off,int len)

向数组中从off开始的len个字节写入.

public class FileOutputStram<span style="font-family: Arial, Helvetica, sans-serif;">Demo</span><span style="font-family: Arial, Helvetica, sans-serif;"> {</span>
	public static void main(String[] args) throws IOException {
		write(); //写入操作
		
		fun1(); //追加操作
		
		fun2(); //追加多个
	}

	private static void write() throws IOException {
		//创建一个FileOutoutStream
		FileOutputStream fos = new FileOutputStream("e:/a.txt");
		//上面执行,相当于程序与文件建立管道,向文件写入信息
		
		fos.write(97); //向文件写入97的ASCII编码
	}
	
	//追加操作
	public static void fun1() throws IOException {
		FileOutputStream fos = new FileOutputStream("e:/a.txt",true);
		
		fos.write(98);//向文件写入98的ASCII编码
		//构造:new FileOutputstream(File file,boolean append);
	}
	
	//追加多个
	public static void fun2() throws IOException {
		FileOutputStream fos = new FileOutputStream("e:/a.txt",true);
		
		String s = "helloworld"; //把字符串转化成byte数组
		//构造:new FileOutputStream(String name,boolean append);
		
		fos.write(s.getBytes()); //write(byte[] b)
	}
}

3.关闭

close();

public class FileOutputStream<span style="font-family: Arial, Helvetica, sans-serif;">Demo</span><span style="font-family: Arial, Helvetica, sans-serif;"> {</span>
	//FileOutputStream异常处理
	public static void main(String[] args) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream("e:/a.txt"); //创建流
			fos.write(97); //使用流
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("文件不存在");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("写入失败");
		} finally {
			try {
				if (fos != null) {
					fos.close(); //关闭流
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
	}
}

注意:

1.对于输出流,如果它指向的文件不存在,会创建它.

2.如果输出流的指向的文件存在,当管道创建时,会将其覆盖。(使用没有boolean类型参数的构造)

3.如果想要向文件中追加信息,需要指定  new FileOutputStream(String filename,boolean flag)

flag值如果为true,代表追加。

4.流在使用完成后要关闭。  关闭操作要写在finally中.

InputStream  字节输入流.它做读取操作

FileInputStream ----完成文件的读取操作.

1.构造

new FileInputStream(String filename)

new FileInputStream(File file);


2.读取操作

1.read()

public int read();

作用:读取信息,一次读取一个字节.

返回值:读取的字节的码值.如果值为-1,代表读取到了末尾。

public class FileInputStream<span style="font-family: Arial, Helvetica, sans-serif;">Demo</span><span style="font-family: Arial, Helvetica, sans-serif;"> {</span>
	public static void main(String[] args) throws IOException {
		fun1();
	}
	
	//read():无参
	public static void fun1() throws IOException {
		//1.创建一个字节输入流
		FileInputStream fis = new FileInputStream("e:/a.txt");
	
		//2.从文件读取信息
		int code = -1;
		while ((code = fis.read()) != -1) {
			System.out.print((char)code+" ");
		}
		
		System.out.println();
		
		//3.关闭流
		fis.close();
	}
}

2.read(byte[] b)

public int read(byte[] b);

作用:一次最多可以装入b.length字节信息.

返回值:代表装入到数组中有效字节的个数。如果返回-1,代表读取到文件末尾

public class FileInputStream<span style="font-family: Arial, Helvetica, sans-serif;">Demo</span><span style="font-family: Arial, Helvetica, sans-serif;"> {</span>
	public static void main(String[] args) throws IOException {
		fun2();
	}
	
	//read(byte[] b)
	public static void fun2() throws IOException {
		//1.创建一个字节输入流
		FileInputStream fis = new FileInputStream("e:/a.txt");
		
		//读取信息
		int length = -1;
		while ((length = fis.read(by)) != -1) {
			System.out.print(new String(by, 0, length));
		}
		
		//3.关闭流
		fis.close();
	}
}

3.read(byte[] b ,int off,int len)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值