hpfileio.java

来自「一个简单的visio程序。」· Java 代码 · 共 2,123 行 · 第 1/5 页

JAVA
2,123
字号

/**
 * @(#)fileio
 * Baiwei 1997.4
 */

package HPCore.stdstmt;

import java.io.*;
import java.util.*;
import HPCore.Exception.*;
import HECore.stddata.*;

public class hpfileio
{

   final static short INPUT  = 0x0001;
   final static short OUTPUT = 0x0002;
   final static short RANDOM = 0x0004;
   final static short APPEND = 0x0008;
   final static short BINARY = 0x0020;

   final static short L_WRITE      = 0x1000 ;
   final static short L_READ       = 0x2000 ;
   final static short L_SHARE      = 0x3000 ;
   final static short L_READ_WRITE = 0x4000 ;

   final static short A_READ       = 0x0100 ;
   final static short A_WRITE      = 0x0200 ;
   final static short A_READ_WRITE = 0x0300 ;

   final static int ATTR_NORMAL    = 0x0000 ;
   final static int ATTR_READONLY  = 0x0001 ;
   final static int ATTR_HIDDEN    = 0x0002 ;
   final static int ATTR_SYSTEM    = 0x0004 ;
   final static int ATTR_VOLUMN    = 0x0008 ;
   final static int ATTR_DIRECTORY = 0x0010 ;
   final static int ATTR_ARCHIVE   = 0x0020 ;


   private static  Vector  vFInfo = null;

   static  final  char fseparator = System.getProperty("file.separator").charAt(0);
   private static  int Col = 0 ;
   private static boolean tab = true;

   private static boolean isTab   = false;
   private static boolean isPtTab = false;
   private static boolean isPrint = false;

   static  fileinfo  finfo=null ;

  /**
   * Enables input/output (I/O) to a file.
   *
   * pathname   : String expression that specifies a filenamemay include 
   *              directory and drive.
   * mode       : Keyword specifying the file mode: Append, Binary, Input,
   *              Output, or Random.
   * access	    : Keyword specifying the operations permitted on the open 
   *              file: Read, Write, or Read Write.
   * lock       : Keyword specifying the operations permitted on the open 
   *              file by other processes: Shared, Lock Read, Lock Write, 
   *              and Lock Read Write.
   * filenumber : A valid file number in the range 1 to 511, inclusive.  
   *              Use the FreeFile function to obtain the next available 
   *              file number.
   * reclength	 : Number less than or equal to 32,767 (bytes).  For files 
   *              opened for random access, this value is the record length.
   *              For sequential files, this value is the number of characters 
   *              buffered.
   */

   public static void open(String pathname,  short filenumber, int mode_access_lock, short reclength) throws HpException
   {
       int mode =0, access=0, lock=0;

       lock   = mode_access_lock & 0x0000F000 ;
       access  = mode_access_lock & 0x00000F00 ;
       mode  = mode_access_lock & 0x000000FF ;

       OPEN(pathname,mode,access,lock,filenumber,reclength);
   }

  /**
   * Enables input/output (I/O) to a file.
   *
   * pathname   : String expression that specifies a filenamemay include 
   *              directory and drive.
   * mode       : Keyword specifying the file mode: Append, Binary, Input,
   *              Output, or Random.
   * access	    : Keyword specifying the operations permitted on the open 
   *              file: Read, Write, or Read Write.
   * lock       : Keyword specifying the operations permitted on the open 
   *              file by other processes: Shared, Lock Read, Lock Write, 
   *              and Lock Read Write.
   * filenumber : A valid file number in the range 1 to 511, inclusive.  
   *              Use the FreeFile function to obtain the next available 
   *              file number.
   */
   public static void open(String pathname, short filenumber, int mode_access_lock ) throws HpException
   {
       int mode =0, access=0, lock=0;

       lock   = mode_access_lock & 0x0000F000 ;
       access  = mode_access_lock & 0x00000F00 ;
       mode = INPUT;
       int reclength = 0;

       OPEN(pathname,mode,access,lock,filenumber,reclength);
   }

   /**
    * @Syntax:
    *  Open file [For mode] [Access access] [lock] As [#]filenumber
    *            [Len = reclen]
    *  char *fname ,int fnum,int phmode ,int access, int mode ,int reclen
    */
   static void OPEN(String fname,int mode,int access,int lock,short fnum,int reclen) throws HpException
   {
       File f0;
       RandomAccessFile  af0;

       int i = 0 ;
       int phmode;
       phmode=lock;
       String fmode = null;

       if(vFInfo==null)
       vFInfo = new Vector();

       if(fnum < 1 || fnum > 512)
		   throw new HpException(52,"Bad file name or number");

       fname = fname.replace('\\',fseparator);

       if (fname.indexOf("*") != -1)
		    throw new HpException(52,"Bad file name or number");

       boolean ishttp = false;
    
       if ((fname.toLowerCase()).indexOf("file:///") != -1)
       {
          fname = fname.substring(fname.indexOf("file:///")+8,fname.length());
       }

       if (fname.length() > 4 && fname.charAt(4) == ':' && (fname.toLowerCase()).indexOf("http:") != -1)
          ishttp = true;        

       if (ishttp)
       {
          if (mode == OUTPUT || mode == APPEND)
				 throw new HpException(75,"Path/File access error");

          int len = 0;
          try{
             java.net.URL url = new java.net.URL(fname);
             InputStream in = url.openStream();
             if (mode == INPUT)
             {
                while (in.read() != -1)
                   len++;
                in = url.openStream();
             }
             else
             {
               DataInputStream ins = new DataInputStream(in);
               len = ins.available();
             }
             DataInputStream dins = new DataInputStream(in);
		       finfo = new fileinfo(dins);
          }catch(Exception e){
			    throw new HpException (76,"Path not found");
          }
          finfo.name = fname;
          finfo.mode = mode;
          finfo.fnum = fnum;
          finfo.flock = lock;  //phmode  ;
          finfo.reclen = reclen;
          finfo.access = access;
          finfo.length = len;

          vFInfo.addElement(finfo);

       }
       else 
       {
	       if (fname.indexOf(fseparator) != -1 && fname.lastIndexOf(fseparator) != 0)
	       {
		       String path = fname.substring(0,fname.lastIndexOf(fseparator));
		       f0 = new File(path);
		       if (!f0.exists())
			       throw new HpException (76,"Path not found");
		       else f0 = null;
	       }

          if(mode == OUTPUT || mode == APPEND || mode == INPUT)
		       if (checkout(fname,fnum))
			      throw new HpException(55,"File already open.");

          if (checkappout(fname,fnum))
             throw new HpException(55,"File already open.");

          if(findindex(fnum))
		      throw new HpException(52,"Bad file name or number.");

          switch(lock)
          {
          case L_SHARE:
          break;

          case L_READ:
          break;

          case L_WRITE:
          break;

          case L_READ_WRITE:
          break;

          default :
          break;
          }

          if((access == 0 || access == A_READ_WRITE) && (mode == RANDOM || mode == BINARY ))	  /* if mode is binary or random ,deflaut access*/
              fmode="rw";
          else {
          if((access == 0) && ((mode == OUTPUT) || (mode == APPEND)))
              fmode="rw";

          else if((access == 0)&&(mode == INPUT))
              fmode="r";
          }

          if(access == A_READ )
          fmode ="r";

          if(access == A_WRITE )
          fmode="rw";

          if (access == A_READ_WRITE )
          fmode ="rw";


          if (mode == INPUT)
	       { //read
              f0 = new File(fname);

              if (f0.exists() && f0.isFile())
		        {
				      try{
				      af0 = new RandomAccessFile(f0,fmode);
				      finfo = new fileinfo(af0,f0);
				      } catch(IOException io) {
					      throw new HpException( 5, io.getMessage());
				      }
              }
              else
			         throw new HpException(53,"File not found");
          }
          else
	       {
		      f0 = new File(fname);
		      if (f0.exists() && f0.isFile())
		      {
			      if (f0.canWrite())
			      {
				      try{
					      af0 = new RandomAccessFile(f0,fmode);
				      } catch(IOException io) {
					      throw new HpException( 5, io.getMessage());
				      }
			      }else
			      {
				      if (mode == OUTPUT || mode == APPEND)
					      throw new HpException(75,"Path/File access error");
				      try{
					      af0 = new RandomAccessFile(f0,"r");
				      } catch(IOException io) {
					      throw new HpException( 5, io.getMessage());
				      }
			      }
		      }else
		      {
			      try{
				      af0 = new RandomAccessFile(f0,fmode);
			      } catch(IOException io) {
				      throw new HpException( 5, io.getMessage());
			      }
		      }

		      finfo = new fileinfo(af0, f0);
          }

          finfo.name = fname;
          finfo.mode = mode;
          finfo.fnum = fnum;
          finfo.flock = lock;  //phmode  ;
          finfo.reclen = reclen;
          finfo.access = access;

          vFInfo.addElement(finfo);
       }

   }

   static boolean checkout(String fname ,short fnum)
   {
		if (vFInfo != null && vFInfo.size() != 0)
		{
        for (int i = 0; i < vFInfo.size(); i++)
        {
            finfo = (fileinfo)vFInfo.elementAt(i);

            if(System.getProperty("os.name").equalsIgnoreCase("Windows 95")
			   ||System.getProperty("os.name").equalsIgnoreCase("Windows NT"))
				{
					if (finfo.name.equalsIgnoreCase(fname))
						return true;
				}else
				{
					if (finfo.name.equals(fname))
						return true;
				}
        }
		}
		return false;
   }

   static boolean checkappout(String fname ,short fnum)
   {
      if (vFInfo != null && vFInfo.size() != 0)
      {
        for (int i = 0; i < vFInfo.size(); i++)
        {
            finfo = (fileinfo)vFInfo.elementAt(i);

            if(System.getProperty("os.name").equalsIgnoreCase("Windows 95")
			   ||System.getProperty("os.name").equalsIgnoreCase("Windows NT"))
			{
				if (finfo.name.equalsIgnoreCase(fname) && (finfo.fnum == fnum ||
					(finfo.mode == OUTPUT || finfo.mode == APPEND ||
					finfo.mode == INPUT)))
					return true;
			}else
			{
				if (finfo.name.equals(fname) && (finfo.fnum == fnum ||
					(finfo.mode == OUTPUT || finfo.mode == APPEND ||
					finfo.mode == INPUT)))
					return true;
			}
        }
      }
      return false;
   }

   static boolean findindex(short fnum)
   {
       if (vFInfo.size() != 0)
       {
           for (int i=0;i<vFInfo.size();i++)
           {
              finfo = (fileinfo)vFInfo.elementAt(i);

              if (finfo.fnum == fnum)
              return true;
           }
       }
       return false;
   }

  /**
   *  Copies a file.
   *  
   *  Syntax:
   *     FileCopy source, destination.
   *  
   *  source :	String expression that specifies the name of the file to be 
   *           copied.  The source may include directory and drive.
   *  destination :	String expression that specifies the target filename.
   *           The destination may include directory and drive.
   *  
   */
   public static void filecopy(String source,String destination) throws HpException
   {
      File srcf;
      File detf;
      FileInputStream srcfis;
      RandomAccessFile detraf;
      String srcname = source.replace('\\',fseparator);
      String detname = destination.replace('\\',fseparator);
		byte b[]=null ;

      if (vFInfo != null && vFInfo.size() != 0)
      {
         for (int i=0;i<vFInfo.size();i++)
         {
            finfo = (fileinfo)vFInfo.elementAt(i);
				if(System.getProperty("os.name").equalsIgnoreCase("Windows 95")
				   ||System.getProperty("os.name").equalsIgnoreCase("Windows NT"))
				{
					if (((finfo.name).equalsIgnoreCase(srcname) && finfo.mode != INPUT)
						|| (finfo.name).equalsIgnoreCase(detname))
						throw new HpException(55, "File already open.");
				}else
				{
					if (((finfo.name).equals(srcname) && finfo.mode != INPUT)
						|| (finfo.name).equals(detname))
						throw new HpException(55, "File already open.");
				}
         }
      }

      srcf = new File(srcname);

      if (!srcf.exists() && !srcf.isFile())
          throw new HpException(53 , "File not found");

		try{
			srcfis = new FileInputStream(srcf);
			b = new byte[(int)srcf.length()];
			srcfis.read(b,0,(int)srcf.length());
			detf = new File(detname);

			if (detf.exists() && detf.isFile())

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?