码迷,mamicode.com
首页 > 编程语言 > 详细

java FileStream文件流操作

时间:2014-05-02 23:30:56      阅读:436      评论:0      收藏:0      [点我收藏+]

标签:java   filesteam文件流操作   

直接上代码,函数使用说明详见Java API文档

import java.io.*;

public class StreamDemo
{
	public static void main(String[] args)
	{
		File f=new File("F:\\workspace\\JavaPrj\\test.txt");
		FileOutputStream out=null;
		
		try 
		{
			out=new FileOutputStream(f);
			byte[] b=new String("Hello,my name is cjc").getBytes();
			System.out.println("\"Hello,my name is cjc\"has been writeen into "+f.getName()+".");
			out.write(b);
			out.close();
		} 
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		try
		{
			FileInputStream in=new FileInputStream(f);
			byte b[]=new byte[1024];
			int cnt=0;
			cnt=in.read(b);
			//注意这里byte转化成String的方法
			System.out.println("\""+new String(b,0,cnt)+"\"has been read from "+f.getName()+".");
		}
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
bubuko.com,布布扣

java FileStream文件流操作,布布扣,bubuko.com

java FileStream文件流操作

标签:java   filesteam文件流操作   

原文地址:http://blog.csdn.net/cjc211322/article/details/24837239

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!