hpfileio.java

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

JAVA
2,123
字号
			{
				detf.delete();
				detf = new File(detname);
			}

			detraf = new RandomAccessFile(detf,"rw");
			char c[] = new char[b.length];

			for ( int i=0;i<b.length;i++)
			{
				c[i] = (char)b[i];
			}

			detraf.writeBytes(new String(c));

			detraf.close();
			srcfis.close();

		}catch(SecurityException  e){
			   throw new HpException(17,"Can't perform requested operation");
		}catch(Exception i){
            throw new HpException(53 , "File not found");
		}


	}

  /**
   * Renames a disk file or directory.
   *
   * Syntax:
   *
   *   Name oldpathname As newpathname .
   *
   * oldpathname	: String expression that specifies the existing filename and
   *              location - may include directory and drive. 
   * newpathname	: String expression that specifies the new filename and 
   *              location - may include directory and drive.  The filename 
   *              specified by newpathname can't already exist.  
   */
   public static void name(String oldpathname,String newpathname) throws HpException
   {
      File oldf;
      File newf;
      String oldname = oldpathname.replace('\\',fseparator);
      String newname = newpathname.replace('\\',fseparator);

      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(oldname))
					   throw new HpException(55, "File already open.");
			   }else
			   {
				   if ((finfo.name).equals(oldname))
					   throw new HpException(55, "File already open.");
			   }
		   }
      }

      oldf = new File(oldname);
      if (!oldf.exists() && (!oldf.isFile() || !oldf.isDirectory()))
         throw new HpException(53 , "File not found");

      newf = new File(newname);
      if (newf.exists())
         throw new HpException(77, "File already exists.");

      if (!oldf.renameTo(newf))
         throw new HpException(53,"File not found");
   }

  /**
   * Returns file mode or operating system file handle information 
   * for files opened using the Open statement.
   *
   * Syntax :
   *
   *     FileAttr(filenumber, returntype) .
   *
   * filenumber	: Any valid file number.
   * returntype	: Number indicating the type of information to return.
   *                Specify 1 to return a value indicating the file mode.  
   *                Specify 2 to return the operating system file handle.
   *
   */
   public static int fileattr(short filenumber,short returntype) throws HpException
   {
      int mode = 0;

      if (returntype != 1 && returntype != 2)
         throw new HpException(8, "Illegal function call");

      finfo = getFileInfo( filenumber ) ;

      if (finfo == null)
         throw new HpException( 52, "Bad file name or number.");

      if(returntype == 1)
         mode = finfo.mode;
      else    
         throw new HpException( 5, "Invalid procedure call");

      return mode;
   }

  /**
   * Returns a number representing the attributes of a file or directory.
   *
   * Syntax :
   *
   *   GetAttr(pathname) .
   *
   * The pathname argument is a string expression that specifies a filename.
   * The pathname may include the directory and the drive.
   */
   public static int getattr(String pathname) throws HpException
   {
      int attr = 0;
      String fname = pathname.replace('\\',fseparator);

      File attr_file = new File(fname);

      if (attr_file.exists() || attr_file.isDirectory())
      {
         if(attr_file.isFile())
            attr = ATTR_NORMAL;

         if(attr_file.exists())
            attr = ATTR_ARCHIVE;

         if (!(attr_file.canWrite()) && attr_file.canRead())
            attr = attr + ATTR_READONLY;

         if (attr_file.isDirectory() ||
            fname.charAt(fname.length()-1)==':')  //include the drive
            //attr = attr + FCONST.ATTR_DIRECTORY;
            attr = ATTR_DIRECTORY;        

        // other attribute.....ATTR_HIDDEN ATTR_SYSTEM 
      }
      else
        throw new HpException(53, "File not found");

      return attr;
   }

  /**
   * Sets attribute information for a file.
   *
   * Syntax :
   *   SetAttr pathname, attributes .
   *
   * pathname	: String expression that specifies a filenamemay include 
   *             directory and drive. 
   * attributes : Constant or numeric expression, the sum of which specifies 
   *              file attributes.  
   */
   public static void setattr(String pathname,int attributes) throws HpException
   {
      String fname = pathname.replace('\\',fseparator);

      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))
					   throw new HpException(55, "File already open.");
			   }else
			   {
				   if ((finfo.name).equals(fname))
					   throw new HpException(55, "File already open.");
			   }
         }
      }
      if(attributes > 32767|| attributes < -32768)
         throw new HpException(6, "Overflow");

      File attr_f = new File(fname);
      if (attr_f.exists() || attr_f.isDirectory() )
      {
         if(finfo.mode != A_READ)
           ;// class File can't reset the attribute ???
         else
           throw new HpException(8, "Illegal function call");
      }
      else
         throw new HpException(53, "File not found");
   }

  /**
   * Returns the length of a file in bytes.
   *
   * Syntax :
   *
   *   FileLen(pathname).
   *
   * The pathname argument is a string expression that specifies a file.  
   * The pathname may include the directory and the drive.   
   */
   public static int filelen(String pathname) throws HpException
   {
      String fname = pathname.replace('\\',fseparator);
      int i = 0;

      File file = new File(fname) ;

      if(file.exists() && (file.isFile() || file.isDirectory()))
      i = (int)file.length();
      else
         throw new HpException(53,"File not found");

      return i;
   }


  /**
   * Returns the size, in bytes, of a file opened using the Open statement.
   *
   * Syntax :
   *
   *   LOF(filenumber).
   *
   * The filenumber argument is any valid file number.
   */
   public static int lof(short filenumber) throws HpException
   {
      fileinfo finfo=null;
      finfo = getFileInfo( filenumber ) ;

      if (finfo == null)
         throw new HpException(52, "Bad file name or number.");
      if (finfo.ishttp)
         return (int)(finfo.length);
      else
         return (int)(finfo.Length());
   }

  /**
   * Returns a date (VarType 7) that indicates the date and time when a 
   * file was created or last modified.
   *
   * Syntax :
   *
   *   FileDateTime(pathname).
   *
   * The pathname argument is a string expression that specifies a filename.
   * The pathname may include the directory and the drive.   
   */
   /*public static HDate filedatetime(String pathname) throws HpException
   {
      String fname = pathname.replace('\\',fseparator);
      File file;
      long dt = 0;
      int hour;
      HDate time = new HDate(0);

      HPCore.stdfunc.UdtDatetime     Datetime = new HPCore.stdfunc.UdtDatetime();
      Calendar  calendar = Calendar.getInstance();

      file = new File(fname);

      if (file.exists() || file.isDirectory() )
      {
        dt = file.lastModified();

        Date date = new Date();
        date.setTime(dt);
        calendar.setTime(date);

        int am_pm = calendar.get( Calendar.AM_PM);
        if( System.getProperty("os.name").toLowerCase().compareTo("windows nt" )!=0 )
            hour = am_pm == 1 ? (calendar.get(Calendar.HOUR)+20) : (calendar.get(Calendar.HOUR)+8);
        else
            hour = am_pm == 1 ? (calendar.get(Calendar.HOUR)+12) : (calendar.get(Calendar.HOUR));

        int[]   m_VBDate = {calendar.get(Calendar.MONTH)+1,
                            calendar.get(Calendar.DAY_OF_MONTH),
                            calendar.get(Calendar.YEAR),
                            hour,//calendar.get(Calendar.HOUR)
                            calendar.get(Calendar.MINUTE),
                            calendar.get(Calendar.SECOND)
                            };
        GGRESULT  ddate = Datetime.dtcommon(m_VBDate);

        time = new HDate(ddate.valued+ddate.valuet);
      }
      else
         throw new HpException(53, "File not found");

      return time;
   }*/

  /**
   * Returns a value that indicates whether the end of a file has been 
   * reached.
   *
   * Syntax :
   *
   *   EOF(filenumber).
   *
   * The filenumber argument is any valid file number.
   *
   */

   public static int eof(short filenumber) throws HpException
   {
		fileinfo finfo = getFileInfo(filenumber);

		if(finfo == null)
			throw new HpException(52, "Bad file name or number.");
		else
		{
			long pf = 0;

			pf = finfo.GetFilePointer();

			if (finfo.Length() == 0)
            return -1;
         else if (pf >= finfo.Length())
				return -1;
			else
				return 0;
		}

   }

  /**
   * Reads a line from an open sequential file and assigns it to a string 
   * variable.
   *
   * Syntax :
   *
   *   Line Input #filenumber, varname.
   *
   * filenumber : Any valid file number.
   * varname    : Valid string variable name.
   */
   public static void lineinput(short filenumber,Variant varname) throws HpException
   {
      long p = 0,plen = 0;

      int type = varname.getType();

      if (type == Variant.V_VAR || type == Variant.V_STR || type == Variant.V_EMPTY || type == Variant.V_NULL)
        ;
      else
        throw new HpException(13,"Type mismatch");

      finfo = getFileInfo(filenumber);

      if(finfo == null)
      throw new HpException(52, "Bad file name or number.");

      if(finfo.mode != INPUT && finfo.mode != BINARY)
         throw new HpException(54,"Bad file mode.");

      p = finfo.GetFilePointer();
      plen = finfo.Length();

      if (p >= plen)
         throw new HpException(62,"Input past end of file." );

      if (finfo.ishttp)
      {
         Vector vch = new Vector();
         int by = finfo.ReadUnsignedByte();
         char ch = (char)by;
         int pf = 1;
         while ( ch != '\r' && ch != '\n' && plen > p + pf)
         {
            vch.addElement(new Character(ch));
            by = finfo.Read();
            ch = (char)by;
            pf++;
         }
         char[] cc = new char[vch.size()];
         for (int j=0;j<vch.size();j++)
            cc[j] = ((Character)(vch.elementAt(j))).charValue();
         varname = new VString(new String(cc));
         //v.HcAssign(new String(cc));
      }
      else
         varname = new VString(finfo.ReadLine());
         //v.HcAssign(finfo.ReadLine());
   }

  /**
   * Reads data from an open sequential file and assigns the data to 
   * variables.
   *
   * Syntax :
   *
   *   Input #filenumber, varlist.
   *
   * The Input # statement syntax has these parts:
   *
   * filenumber : Any valid file number.
   * varlist :    Comma-delimited list of variables that are assigned values 
   *              read from the filecan't be an array or object variable.  
   *              However, variables that describe an element of an array or 
   *              user-defined type may be used.
   */
   public static void input_start(short filenumber) throws HpException
   {
       finfo = getFileInfo(filenumber);

       if (finfo == null)
          throw new HpException(52, "Bad file name or number.");

       if(finfo.mode != BINARY && finfo.mode != INPUT)
          throw new HpException( 54, "Bad file mode.");
   }

/*   public static void input(Variant varname) throws HpException
   {

⌨️ 快捷键说明

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