Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.commons.net.ftp;
The FTPFile class is used to represent information about files stored on an FTP server.

 
 
 public class FTPFile implements Serializable
 {
     private static final long serialVersionUID = 9010790363003271996L;

    
A constant indicating an FTPFile is a file. *
 
     public static final int FILE_TYPE = 0;
    
A constant indicating an FTPFile is a directory. *
 
     public static final int DIRECTORY_TYPE = 1;
    
A constant indicating an FTPFile is a symbolic link. *
 
     public static final int SYMBOLIC_LINK_TYPE = 2;
    
A constant indicating an FTPFile is of unknown type. *
 
     public static final int UNKNOWN_TYPE = 3;

    
A constant indicating user access permissions. *
 
     public static final int USER_ACCESS = 0;
    
A constant indicating group access permissions. *
 
     public static final int GROUP_ACCESS = 1;
    
A constant indicating world access permissions. *
 
     public static final int WORLD_ACCESS = 2;

    
A constant indicating file/directory read permission. *
 
     public static final int READ_PERMISSION = 0;
    
A constant indicating file/directory write permission. *
 
     public static final int WRITE_PERMISSION = 1;
    
A constant indicating file execute permission or directory listing permission.
 
     public static final int EXECUTE_PERMISSION = 2;
 
     private int _type_hardLinkCount;
     private long _size;
     private String _rawListing_user_group_name_link;
     private Calendar _date;
     private final boolean[] _permissions[]; // e.g. _permissions[USER_ACCESS][READ_PERMISSION]
 
    
Creates an empty FTPFile. *
 
     public FTPFile()
     {
          = new boolean[3][3];
          = null;
          = ;
         // init these to values that do not occur in listings
         // so can distinguish which fields are unset
          = 0; // 0 is invalid as a link count
          = -1; // 0 is valid, so use -1
          = "";
          = "";
          = null;
          = null;
     }


    
Set the original FTP server raw listing from which the FTPFile was created.

Parameters:
rawListing The raw FTP server listing.
 
     public void setRawListing(String rawListing)
     {
          = rawListing;
     }

    
Get the original FTP server raw listing used to initialize the FTPFile.

Returns:
The original FTP server raw listing used to initialize the FTPFile.
    public String getRawListing()
    {
        return ;
    }


    
Determine if the file is a directory.

Returns:
True if the file is of type DIRECTORY_TYPE, false if not.
    public boolean isDirectory()
    {
        return ( == );
    }

    
Determine if the file is a regular file.

Returns:
True if the file is of type FILE_TYPE, false if not.
    public boolean isFile()
    {
        return ( == );
    }

    
Determine if the file is a symbolic link.

Returns:
True if the file is of type UNKNOWN_TYPE, false if not.
    public boolean isSymbolicLink()
    {
        return ( == );
    }

    
Determine if the type of the file is unknown.

Returns:
True if the file is of type UNKNOWN_TYPE, false if not.
    public boolean isUnknown()
    {
        return ( == );
    }


    
Set the type of the file (DIRECTORY_TYPE, FILE_TYPE, etc.).

Parameters:
type The integer code representing the type of the file.
    public void setType(int type)
    {
         = type;
    }


    
Return the type of the file (one of the _TYPE constants), e.g., if it is a directory, a regular file, or a symbolic link.

Returns:
The type of the file.
    public int getType()
    {
        return ;
    }


    
Set the name of the file.

Parameters:
name The name of the file.
    public void setName(String name)
    {
         = name;
    }

    
Return the name of the file.

Returns:
The name of the file.
    public String getName()
    {
        return ;
    }


    
Set the file size in bytes.

Parameters:
size The file size in bytes.
    public void setSize(long size)
    {
         = size;
    }


    
Return the file size in bytes.

Returns:
The file size in bytes.
    public long getSize()
    {
        return ;
    }


    
Set the number of hard links to this file. This is not to be confused with symbolic links.

Parameters:
links The number of hard links to this file.
    public void setHardLinkCount(int links)
    {
         = links;
    }


    
Return the number of hard links to this file. This is not to be confused with symbolic links.

Returns:
The number of hard links to this file.
    public int getHardLinkCount()
    {
        return ;
    }


    
Set the name of the group owning the file. This may be a string representation of the group number.

Parameters:
group The name of the group owning the file.
    public void setGroup(String group)
    {
         = group;
    }


    
Returns the name of the group owning the file. Sometimes this will be a string representation of the group number.

Returns:
The name of the group owning the file.
    public String getGroup()
    {
        return ;
    }


    
Set the name of the user owning the file. This may be a string representation of the user number;

Parameters:
user The name of the user owning the file.
    public void setUser(String user)
    {
         = user;
    }

    
Returns the name of the user owning the file. Sometimes this will be a string representation of the user number.

Returns:
The name of the user owning the file.
    public String getUser()
    {
        return ;
    }


    
If the FTPFile is a symbolic link, use this method to set the name of the file being pointed to by the symbolic link.

Parameters:
link The file pointed to by the symbolic link.
    public void setLink(String link)
    {
         = link;
    }


    
If the FTPFile is a symbolic link, this method returns the name of the file being pointed to by the symbolic link. Otherwise it returns null.

Returns:
The file pointed to by the symbolic link (null if the FTPFile is not a symbolic link).
    public String getLink()
    {
        return ;
    }


    
Set the file timestamp. This usually the last modification time. The parameter is not cloned, so do not alter its value after calling this method.

Parameters:
date A Calendar instance representing the file timestamp.
    public void setTimestamp(Calendar date)
    {
         = date;
    }


    
Returns the file timestamp. This usually the last modification time.

Returns:
A Calendar instance representing the file timestamp.
    public Calendar getTimestamp()
    {
        return ;
    }


    
Set if the given access group (one of the _ACCESS constants) has the given access permission (one of the _PERMISSION constants) to the file.

Parameters:
access The access group (one of the _ACCESS constants)
permission The access permission (one of the _PERMISSION constants)
value True if permission is allowed, false if not.
    public void setPermission(int accessint permissionboolean value)
    {
        [access][permission] = value;
    }


    
Determines if the given access group (one of the _ACCESS constants) has the given access permission (one of the _PERMISSION constants) to the file.

Parameters:
access The access group (one of the _ACCESS constants)
permission The access permission (one of the _PERMISSION constants)
    public boolean hasPermission(int accessint permission)
    {
        return [access][permission];
    }

    
Returns a string representation of the FTPFile information.

Returns:
A string representation of the FTPFile information.
    @Override
    public String toString()
    {
        return getRawListing();
    }

    
Returns a string representation of the FTPFile information. This currently mimics the Unix listing format.

Returns:
A string representation of the FTPFile information.
Since:
3.0
    public String toFormattedString()
    {
        StringBuilder sb = new StringBuilder();
        Formatter fmt = new Formatter(sb);
        sb.append(formatType());
        fmt.format(" %4d", Integer.valueOf(getHardLinkCount()));
        fmt.format(" %-8s %-8s"getGroup(), getUser());
        fmt.format(" %8d", Long.valueOf(getSize()));
        Calendar timestamp = getTimestamp();
        if (timestamp != null) {
            fmt.format(" %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS"timestamp);
            fmt.format(" %1$tZ"timestamp);
            sb.append(' ');
        }
        sb.append(' ');
        sb.append(getName());
        fmt.close();
        return sb.toString();
    }
    private char formatType(){
        switch() {
            case :
                return '-';
            case :
                return 'd';
            case :
                return 'l';
        }
        return '?';
    }
    private String permissionToString(int access ){
        StringBuilder sb = new StringBuilder();
        if (hasPermission(access)) {
            sb.append('r');
        } else {
            sb.append('-');
        }
        if (hasPermission(access)) {
            sb.append('w');
        } else {
            sb.append('-');
        }
        if (hasPermission(access)) {
            sb.append('x');
        } else {
            sb.append('-');
        }
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X