java文件读写类[备忘]

 java文件读写类[备忘]

package FileAccess;

import java.io.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/**
 * Title:
 * Description: 这是对文本文件的读写操作的Bean
 * Copyright:    Copyright (c) 2001 2001/11/24
 * Company:  海南在线技术部
 * @author  程式猎人
 * @version 1.0
 */

public class FileAccess
{
    private String fileName,errorMessage;
    private int columns,rowCount;

    /* TextFileReader 的结构函数 */
    public FileAccess()
    {
      reset();
    }

    public void reset()
    {
      fileName=new String("");
      errorMessage=new String("");
      columns=0;
      rowCount=0;
    }

    public void setErrorMessage(String errorMessage)
    {
      this.errorMessage=errorMessage;
    }

    public String getErrorMessage()
    {
       return errorMessage;
    }

    public String getFileName()
    {
       return fileName;
    }

    public int getRows()
    {
      return rowCount;
    }

    public int getColumns()
    {
      return columns;
    }

    /*
    将文件内容返回到一个字符串里去
    如果有错误返回 null
    */
    public String getContent()
    {
      String content="";
      File file=new File(fileName);

      if(!file.exists())
      {
        setErrorMessage("Error: The file '"+fileName+"' does not exists.");
        return content;
      }
      else if(file!=null)
      {
        try
        {
          BufferedReader reader=new BufferedReader(new FileReader(file));
          String inLine=reader.readLine();

          while(inLine !=null)
          {
            if(inLine.length()+1>columns)
              columns=inLine.length()+1;

            content+=(inLine+System.getProperty("line.separator"));
            inLine=reader.readLine();
            rowCount++;
          }

          return content;

        }
        catch(IOException e)
        {
          setErrorMessage("Error reading the file: "+e.getMessage());
          return content;
        }
      }
      else
      {
        setErrorMessage("UnKnown Error!");
        return content;
      }

   }

    /* TextFileWriter 部分开始 */

    public void setFileName(String fileName)
    {
       this.fileName=fileName;
    }

    /*

    对文件写入新的内容writemod的方式有2种
    a:如果文件存在,新的内容追加到文件。否则创建一个新文件
    o: 如果文件存在,覆盖旧文件的内容。否则创建一个新文件
    writer: 王海波 2001/11/30

    */

    public boolean Write(String strInputContent,char writemod)
    {
      try
      {
         File fso=new File(fileName);

         String strContent=new String("");

         BufferedWriter writer=new BufferedWriter(new FileWriter(fso));

         switch (writemod)
         {
           case 'a':    //最加新的内容到文件

               FileAccess objFRead=new FileAccess();
               String oldContent=new String("");

               objFRead.setFileName(fileName);
               oldContent=objFRead.getContent();

               strContent=oldContent+strInputContent;

               break;

           case 'o':    //新内容覆盖旧文件的内容

               strContent=strInputContent;
               break;

           default:

               strContent=strInputContent;
               break;
         }

         writer.write(strContent);

         writer.close();

         return true;

      }
      catch(IOException e)
      {
         setErrorMessage("Error writting the file: "+e.getMessage());
         return false;
      }

    }

}

实现Client端功能的ClientApp.java原文件:

import java.net.*;
import java.io.*;
import java.lang.*;

public class ClientApp
{
  public static void main(String args[])
  {
    try
    {
       //创建通讯并且和主机Rock连接
       Socket cSocket=new Socket("192.168.100.188",8018);
       //打开这个Socket的输入/输出流
       OutputStream os=cSocket.getOutputStream();
       DataInputStream is=new DataInputStream(cSocket.getInputStream());

       int c;
       boolean flag=true;

       String responseline;

       while(flag)
       {
           //从标准输入输出接受字符并且写如系统
           while((c=System.in.read())!=-1)
           {
              os.write((byte)c);
              if(c=='/n')
              {
                 os.flush();
                 //将程序阻塞,直到回答信息被收到后将他们在标准输出上显示出来
                 responseline=is.readLine();
                 System.out.println("Message is:"+responseline);
              }
           }
       }
       os.close();
       is.close();
       cSocket.close();

    }
    catch(Exception e)
    {
      System.out.println("Exception :"+ e.getMessage());
    }
  }
}

实现Server端功能的ServerApp.java原文件:

import java.net.*;
import java.io.*;

public class ServerApp
{
  public static void main(String args[])
  {
     try
     {
        boolean flag=true;
        Socket clientSocket=null;
        String inputLine;
        int c;

        ServerSocket sSocket=new ServerSocket(8018);
        System.out.println("Server listen on:"+sSocket.getLocalPort());

        while(flag)
        {
          clientSocket=sSocket.accept();
          DataInputStream is= new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
          OutputStream os=clientSocket.getOutputStream();

          while((inputLine=is.readLine())!=null)
          {
             //当客户端输入stop的时候服务器程序运行终止!
             if(inputLine.equals("stop"))
             {
                flag=false;
                break;
             }
             else
             {
                System.out.println(inputLine);

                while((c=System.in.read())!=-1)
                {
                  os.write((byte)c);
                  if(c=='/n')
                  {
                    os.flush();   //将信息发送到客户端
                    break;
                  }
                }
             }


          }
          is.close();
          os.close();
          clientSocket.close();

        }
        sSocket.close();
     }
     catch(Exception e)
     {
       System.out.println("Exception :"+ e.getMessage());
     }
  }
}


展开阅读全文

没有更多推荐了,返回首页