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

java io读写操作

时间:2015-02-06 18:30:29      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

文件的读取:

public static void duqu(String path) throws IOException
	{
	
		File file=new File(path);
		FileInputStream fis=new FileInputStream(file);
		InputStreamReader isr=new InputStreamReader(fis,"gb2312");
		BufferedReader br=new BufferedReader(isr);
		StringBuffer buf=new StringBuffer();
		String str=null;
		while((str=br.readLine())!=null){
			buf.append(str);
			buf.append("\r\n");
		}
		System.out.println(buf.toString());
		br.close();
	}

文件的写入:
 public static void write(String path, String content) {  
	        String s = new String();  
	        String s1 = new String();  
	          
	        try {  
	            File f = new File(path);  
	              
	            if (f.exists()) {  
	                System.out.println("文件存在");  
	            } else {  
	                System.out.println("文件不存在,正在创建...");  
	                if (f.createNewFile()) {  
	                    System.out.println("文件创建成功!");  
	                } else {  
	                    System.out.println("文件创建失败!");  
	                }  
	            }  
	              
	            BufferedReader input = new BufferedReader(new FileReader(f));  
	  
	            while ((s = input.readLine()) != null) {  
	                s1 += s + "/n";  
	            }  
	          
	            input.close();  
	            s1 += content;  
	            BufferedWriter output = new BufferedWriter(new FileWriter(f));  
	            System.out.println("文件内容:" + s1);  
	            output.write(s1);  
	            output.close();  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	    }

  

java io读写操作

标签:

原文地址:http://www.cnblogs.com/wwxblog/p/4277519.html

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