简体   繁体   中英

From where two extra files named ( '.' and '..' ) came by getting file names from Vector in jSch SFTP

I made a simple program using jSch to print the name of total files in a remote directory. In the directory, there are only 7 files but, on my console, I am getting two extra files with name . and .. .

From where these files came, or its a garbage???

My Java code:

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.util.Vector;

public class DownloadFileSFTP {

public static void main(String[] s){
    String user = "user";
    String password = "1234";
    String host = "remoteIP";
    int port = 22;
    String sourcePath = "/home/remoteSFTP_Files/";
    String destinationPath = "/home/user/SFTP_files/";
    Session session = null;
    ChannelSftp sftp = null;

    try {
        JSch jsch = new JSch();
        session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        System.out.println("Session created");
        sftp = (ChannelSftp) session.openChannel("sftp");
        sftp.connect();
        System.out.println("SFTP Channel connected");
        Vector totalFiles = sftp.ls(sourcePath);
        for(int i = 0; i < totalFiles.size(); i++){
            ChannelSftp.LsEntry ls = (ChannelSftp.LsEntry) totalFiles.get(i);
            System.out.println("File Name: " + ls.getFilename());
        }
    } catch (Exception e){
        e.printStackTrace();
    } finally {
        sftp.exit();
        sftp.disconnect();
        session.disconnect();
    }
}
}

O/P in my console:

Session created
SFTP Channel connected
File Name: campMonitorHome.jsp
File Name: asm-3.1.jar
File Name: didLatest_dump.sql
File Name: PieChartJson.java
File Name: demoFile.txt
File Name: .
File Name: jquery.jqplot.min.css
File Name: showgraphicalMonitor.jsp
File Name: ..

It's simply showing the "." current directory and ".." parent directory.

I guess people who haven't used terminals never come accross them :)

In your execution folder, along with all your files there are two entities called "." and ".." which represent the present folder and the parent folder respectively. Those are not real files, but keywords reserved by a Unix system to have a relative reference to call them.

This way you can make scripts to relate to those folders without writing a whole address.

You can simply ignore these keywords by using a remove method from totalFiles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM