Java学习路程之File

FileInputStream和FileOutPutStream

一个流可以理解为一个数据的序列, 输入流表示从源中读取数据, 输出流表示向一个目标中写入数据

FileInputStream:是用来从文件中读取数据,他的对象可以用new关键字创建,也有多种构造方法创建对象。

InputStream f = new FileInputStream("c:/java.txt");

或者

File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);


FileOutputStream:是用来向一个目标中写入数据的。如果该流在打开文件进行输出前,目标文件不存在,那么该流会创建该文件。有两个构造方法可以用来创建 

OutputStream out=new FileOutputStream("c:/java.txt");
或者
File file=new File("c:/java.txt");
OutputStream out=new FileOutputStream(file); 


有了上面的对象,就可以对文件进行读写了!

public static void main(String[] args) throws IOException {
		File file=new File("c:/java.txt");
		file.createNewFile();
		FileWriter writer=new FileWriter(file);
		writer.append("hello world");
		writer.flush();
		writer.close();
		
		FileReader reader=new FileReader(file);
		char[] cha=new char[30];
		reader.read(cha);
		for (char c : cha) {
			System.out.print(c);
		}
		reader.close();
	}
输出结果为:hello world


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值