zoukankan      html  css  js  c++  java
  • 文件管理

    package 文件管理;
    import java.io.*;
    public class Filechar {
            //对字符数组进行排序的类


                   private String str;

                   private char arrayList[];

                   private BufferedReader br; //字符流

                   private File f; //读取的文件

                  

                   Filechar( String s )

                   {

                          f=new File( s );

                   }

                  

                   public void start()

                   {

                         

                          if( inputData()==-1 )

                          {

                                 return;

                          }

                         

                          //对字符数组进行冒泡排序

                          sortChar(  );

                  

                          //把排序后的数组变成字符串,并通过流将字符串写入b.txt文件中。

                          outputString();

                         

                   }

                  

                   //通过BufferedReader读取文本中的字符串

                   //读取文件失败返回-1,成功返回1

                   private int  inputData()

                   {

                          try

                          {

                                 br=new BufferedReader( new FileReader(f));

                                 //读取文本的内容

                                 while( (str=br.readLine())!=null )

                                 {

                                        //把所有字母转换为小写

                                        str.toLowerCase();

                                        arrayList=str.toCharArray(); 

                                 }

                          }

                          catch( IOException e )

                          {

                                 System.out.println( "读取文件出错!" );

                                 e.printStackTrace();

                                 return -1;              

                          }

                          finally

                          {

                                 //关闭输入流

                                 if( br!=null )

                                 {

                                        try

                                        {

                                               br.close();

                                        }

                                        catch( IOException e )

                                        {

                                               e.printStackTrace();

                                        }

                                 }

                          }

                          return 1;

                   }

                  

                   //对字符数组进行冒泡排序

                   private void sortChar(  )

                   {

                          char temp=0;

                          for( int i=0; i<arrayList.length-1; i++ )

                          {

                                 for( int j=0; j<arrayList.length-i-1; j++ )

                                 {

                                        if( arrayList[j]>arrayList[j+1] )

                                        {

                                               temp=arrayList[j];

                                               arrayList[j]=arrayList[j+1];

                                               arrayList[j+1]=temp;            

                                        }

                                 }

                          }

                   }

                  

                   /*

                   把排序后的数组变成字符串,并通过流将字符串写入b.txt文件中。

                   a.通过String类的构造函数将字符数组变成字符串。

                   b.通过FileWriter把字符串写入到b.txt文件中。

                   */

                   private void outputString()

                   {

                          BufferedWriter bw=null;

                          try

                          {

                                 FileWriter fw = new FileWriter("b.txt");

                                 bw = new BufferedWriter(fw);

                                

                                 //把排序完成后的字符串重新写入到文件中

                                 bw.write( new String( arrayList,0,arrayList.length ));

                                 bw.flush();

                          }

                          catch( IOException e )

                          {

                                 e.printStackTrace();

                          }

                          finally

                          {

                                 //关闭输入流

                                 if( bw!=null )

                                 {

                                        try

                                        {

                                               bw.close();

                                        }

                                        catch( IOException e )

                                        {

                                               e.printStackTrace();

                                        }

                                 }

                          }

                   }

            }

             

            class Demo

            {

                   public static void main(String arsg[])

                   {

                          new Filechar( "a.txt" ).start();

                         

                   }

            }

  • 相关阅读:
    mysql报错总结
    mysql半同步开启
    BZOJ3675 [Apio2014]序列分割 动态规划 斜率优化
    BZOJ1096 [ZJOI2007]仓库建设 动态规划 斜率优化
    BZOJ3437 小P的牧场 动态规划 斜率优化
    BZOJ3156 防御准备 动态规划 斜率优化
    BZOJ1010 [HNOI2008]玩具装箱toy 动态规划 斜率优化
    BZOJ1001 [BeiJing2006]狼抓兔子 最小割 对偶图 最短路
    BZOJ2527 [Poi2011]Meteors 整体二分 树状数组
    树状数组的一些区间修改与区间询问的实现
  • 原文地址:https://www.cnblogs.com/zw98916/p/9260305.html
Copyright © 2011-2022 走看看