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

Java代码收集

时间:2014-08-25 20:49:04      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   os   io   文件   div   代码   

一、文件读取和写入

 1 try {
 2             FileInputStream fis = new FileInputStream(fileName);
 3             InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
 4             BufferedReader br = new BufferedReader(isr);
 5             String line;
 6             StringBuilder sb = new StringBuilder();
 7             while ((line = br.readLine()) != null)
 8                 sb = sb.append(line+"\n");
 9             br.close();
10             isr.close();
11             fis.close();
12             return sb.toString();
13         } catch (FileNotFoundException e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16         } catch (IOException e) {
17             // TODO Auto-generated catch block
18             e.printStackTrace();
19         }
 1 try {
 2             FileOutputStream fos = new FileOutputStream(newFile);
 3             OutputStreamWriter osw = new OutputStreamWriter(fos);
 4             BufferedWriter bw = new BufferedWriter(osw);
 5             bw.write(string);
 6             System.out.println("文件写入成功");
 7             bw.close();
 8             osw.close();
 9             fos.close();
10         } catch (FileNotFoundException e) {
11             // TODO Auto-generated catch block
12             e.printStackTrace();
13         } catch (IOException e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16         }

 

Java代码收集

标签:style   blog   color   java   os   io   文件   div   代码   

原文地址:http://www.cnblogs.com/hellovenus/p/3935705.html

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