通过java读取Linux上某一个目录下的文件名,不递归查子目录,返回文件名列表List

  • Post author:
  • Post category:java


package com.test.demo.controller;

import ch.ethz.ssh2.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class QueryLinuxFileNames {

    public ArrayList<String> readFile(String directory) {
        //创建远程连接,默认连接端口为22,如果不使用默认,可以使用方法
        //new Connection(ip, port)创建对象
        String ip = "10.0.8.1";
        String usr = "root";
        String pwd = "root";
        //int port=22;
        Connection conn = null;
        String date = "";
        //所要查询的目录
        String path = directory;
        ArrayList<String> filesNameList = new ArrayList<String>();
        try {
            //连接远程服务器
            // 连接部署服务器
            conn = new Connection(ip);
            conn.connect();
            //使用用户名和密码登录
            boolean b = conn.authenticateWithPassword(usr, pwd);
            if (!b) {
                throw new IOException("Authentication failed.");
            } else {
                SFTPv3Client sft = new SFTPv3Client(conn);
                Vector<?> v = sft.ls(path);
                for (int i = 0; i < v.size(); i++) {
                    SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
                    s = (SFTPv3DirectoryEntry) v.get(i);
                    //文件名
                    String filename = s.filename;
                    filesNameList.add(filename);
                }
            }
            conn.close();
        } catch (IOException e) {
            System.err.printf("用户%s密码%s登录服务器%s失败!", usr, pwd, ip);
            e.printStackTrace();
        }

        return filesNameList;
    }

}//class