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

java io操作

时间:2016-03-26 08:10:37      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:java   程序   io流   

//IO:输入输出流    (读入写出)
/*
 * 使用文件字节流将数据读取到程序
 */
public class TestFileInputStream {

	public static void main(String[] args) throws IOException {
		//先准备文件对象    f就是将要被读取的源文件
		File f = new File("file/abc.txt");
		//准备字节输入流对象
		FileInputStream fis = null;
		try {
			//将源文件f转化成字节输入流
			fis = new FileInputStream(f);
			//从流中获取数据     读取一个字节
//			int read = fis.read();
			//一次性读取一个字节数组
			//先准备一个空杯子
			byte[]bs = new byte[(int)f.length()];
			//将流中的数据存储到bs中
			fis.read(bs);
			//将字节数组中的数据展示   (将字节数组转字符串)
			String str = new String(bs);
			System.out.println(str);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			fis.close();
		}
	}
}
//--------------------------------------------------------------------------
/*
 * 将程序中的数据写入到文件中
 */
public class TestFileOutputStream {

	public static void main(String[] args) {
		//创建一个目标文件。   一会将数据写到f中
		File f = new File("file/def.txt");
		FileOutputStream fos = null;
		//准备文件输出流
		try {
			//创建一个文件输出流对象,并指定将要把数据写到哪个文件中
			//FileOutputStream(f,true)//f代表目标文件,true代表是否追加
			fos = new FileOutputStream(f,true);
			//将数据写入到文件中
			String str = "小明,今天天气\n怎么样?";
			//先将字符串转字节数组
			byte[] bs = str.getBytes();
			//将字节数组中的数据写入到文件中
			fos.write(bs);
			//刷缓存
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(null!=fos){
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
//-------------------------------------------------------------------------
/**
	 * 使用缓冲流读取数据
	 */
	public static void main(String[] args) {
		BufferedReader br = null;
		try {
			//创建一个缓冲流对象,指定要读取的文件
			br = new BufferedReader(
					new FileReader(
							new File("file/abc.txt")));
			//对文件进行操作
			String str =  br.readLine();//直接读取一行数据
			while(str!=null){//不为空就循环读取
				System.out.println(str);
				str = br.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(null!=br){
				try {
					//只需要关闭缓冲流对象,FileReader对象一同被关闭,不需要再手动关闭
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
//----------------------------------------------------------------------------------
/**
	 * 使用字符缓冲流,对文件进行读写(复制)
	 */
	public static void main(String[] args) {
		//先将文件file/def.txt中的数据读取出来
		BufferedReader br = null;
		//缓冲输出流对象
		BufferedWriter bw = null;
		try {
/*			File file = new File("file/def.txt");
			//要有FileReader对象,就需要先有文件对象
			FileReader fr = new FileReader(file);
			//要创建缓冲流对象,必须先有Reader对象
			br = new BufferedReader(fr);*/
			//构建文件输入流对象
			br = new BufferedReader(
					new FileReader(
							new File("file/def.txt")));
			//构建文件输出流对象,指定将数据写到d盘中aaa.txt中
			bw = new BufferedWriter(
					new FileWriter(
							new File("D:/aaa.txt")));
			//使用缓冲流读取一行数据
			String str = br.readLine();
			while(str!=null){
				//直接将上面代码读取的字符串写到文件中
				bw.write(str);
				bw.newLine();//换行
				bw.flush();//刷流
				//继续读取数据
				str = br.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
				try {
					//先进后出
					if(bw!=null){
						bw.close();
					}
					if(br!=null){
						br.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

}
//-------------------------------------------------------------------------------
/**
 * 
 * @author Administrator
 字符输入流:
        以reader结束的流的类一定是字符输入流
    
    FileReader:文件字符流
    BufferedReader:缓冲字符流
 */
public class TestReader {

	public static void main(String[] args) {
		//创建字符输入流对象
		FileReader fr = null;
		try {
			//创建字符输入流对象,并指定操作的是哪一个文件对象
			fr = new FileReader(new File("file/abc.txt"));
			//使用字符输入流读取数据
//			int read = fr.read();//读取一个字符
			//准备一个字符数组
			char[]chs = new char[10];
			//将数据读取到字符数组中,len代表读取到数组中字符的个数
			int len = fr.read(chs);
			//循环将数据都读取出来
			while(len!=-1){//len=-1代表读取到了文件的末尾
				//将每一次读取到字符数组中的数据转成字符串
				//    从数组的第一个元素开始,len代表数组中字符的个数
				String str = new String(chs,0,len);
				System.out.println(str);
				//继续循环读取
				len = fr.read(chs);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(null!=fr){
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
//----------------------------------------------------------------------------------
/*
 * 配置文件的读取和使用
 * properties文件中key和value都是String类型
 * 
 * */
public class TestProper {
	public static void main(String[] args) {
		FileInputStream fis = null;
		Properties pp = null;
		try {
			//构建一个输入流对象
			fis = new FileInputStream("file/abc.properties");
			//创建properties对象
			pp = new Properties();
			//使用配置文件对象,将流加载进行
			pp.load(fis);
			//从pp中获取数据
			String name = pp.getProperty("name");
			String age = pp.getProperty("age");
			String hobbys = pp.getProperty("hobby");
			//将爱好hobbys分成几个独立的部分  “跳舞,吃饭,唱歌”
			String[] hobs = hobbys.split(",");
			System.out.println(name+age+hobbys);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
回顾以前学的
     (每天坚持写博客)


java io操作

标签:java   程序   io流   

原文地址:http://flyblog.blog.51cto.com/10081495/1755389

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