读写文件


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->  1 
 2  //   
 3  //   程序:ReadWiteFile.java
 4  //
 5  //   描述:对文件读写操作
 6  //
 7  //   作者:JAVA-HE
 8  //
 9  //   时间:2006-12-20
10  //
11  //
12 
13  package  com.readwritefile;
14 
15  import  java.io. * ;
16  public   class  ReadWiteFile
17  {
18       public  ReadWiteFile ()
19      {
20      }
21       public   static   void  main(String args [])
22      {
23          ReadWiteFile managerFile  =   new  ReadWiteFile();
24          File readfile  =   new  File( " read.txt " );
25          File writefile  =   new  File( " write.txt " );
26          String content  =   "" ;
27           try
28          {
29              content  =  managerFile.readFile(readfile);
30              
31          } catch (Exception ex)
32          {
33              System.out.println ( " 读文件发生异常: " + ex.toString ());
34          }
35           try
36          {
37              managerFile.writeFile(writefile,content);
38          } catch (Exception e)
39          {
40              System.out.println ( " 写文件发生异常: " + e.toString ());
41          }
42      }
43       private   String readFile (File readfile)  throws  Exception 
44      {
45          FileReader read  =   new  FileReader(readfile);
46          BufferedReader in  =   new  BufferedReader(read);
47          String readResult = "" ;
48          String str  =   "" ;
49           while ((str = in.readLine ()) !=   null )
50          {
51              String tem []  =  str.split ( " = " );
52              readResult  +=   " m_objCitys.Add \ "" +tem[1]+ " \ " , \ "" +tem[0]+ " \ "" + " \n " ;
53          }
54          read.close ();
55          in.close ();
56           return  readResult;
57      }
58 
59       private   void  writeFile (File writefile, String content)  throws  Exception
60      {
61          FileWriter write  =   new  FileWriter(writefile);
62          BufferedWriter wr  =   new  BufferedWriter(write);
63          wr.write (content);
64          wr.flush ();
65          wr.close ();
66          write.close ();
67      }
68      
69  }
70 
71 
72 
73 
74 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值