文件操作练习

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();

                     

               }

        }


posted @ 2018-07-03 20:25  汪迎伟  阅读(127)  评论(1编辑  收藏  举报