Java IOException

package io.filewriter;

import java.io.FileWriter;
import java.io.IOException;

public class IOExceptionDemo {

	private static final String LINE_SRPATATOR = System.getProperty("line.separator");

	public static void main(String[] args)  {

		FileWriter fw=null;
		
	
		try {
			fw=new FileWriter("k:\\demo.txt");
	
		fw.write("xixi");
		fw.write(LINE_SRPATATOR);
		fw.write("aaaa");
		} catch (IOException e) {
			System.out.println(e.toString());
		}
		
		
		/*
		 * 关闭流 关闭资源 在关闭前会调用flush刷新缓冲中的数据到目的地
		 */
		finally {
			if(fw!=null)//防止空指针异常
			 try {
				fw.close();
			} catch (IOException e) {
				throw new RuntimeException("关闭失败");
			}
		}
		
		
	
	}

}

你可能感兴趣的:(Java学习)