java访问Linux服务器读取文件
2016-01-11 15:02
603 查看
所需jar包:j2ssh-core-0.2.2.jar
java代码:
SshClient client=new SshClient();
try{
client.connect("此处是Linux服务器IP");
//设置用户名和密码
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername("root");
pwd.setPassword("123456");
int result=client.authenticate(pwd);
if(result==AuthenticationProtocolState.COMPLETE){//如果连接完成
System.out.println("==============="+result);
List<SftpFile> list = client.openSftpClient().ls("/etc/mail/");
for (SftpFile f : list) {
System.out.println(f.getFilename());
System.out.println(f.getAbsolutePath());
if(f.getFilename().equals("aliases")){
OutputStream os = new FileOutputStream("d:/mail/"+f.getFilename());
client.openSftpClient().get("/etc/mail/aliases", os);
//以行为单位读取文件start
File file = new File("d:/mail/aliases");
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;//行号
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
//显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
//以行为单位读取文件end
}
}
}
}catch(IOException e){
e.printStackTrace();
}
java代码:
SshClient client=new SshClient();
try{
client.connect("此处是Linux服务器IP");
//设置用户名和密码
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername("root");
pwd.setPassword("123456");
int result=client.authenticate(pwd);
if(result==AuthenticationProtocolState.COMPLETE){//如果连接完成
System.out.println("==============="+result);
List<SftpFile> list = client.openSftpClient().ls("/etc/mail/");
for (SftpFile f : list) {
System.out.println(f.getFilename());
System.out.println(f.getAbsolutePath());
if(f.getFilename().equals("aliases")){
OutputStream os = new FileOutputStream("d:/mail/"+f.getFilename());
client.openSftpClient().get("/etc/mail/aliases", os);
//以行为单位读取文件start
File file = new File("d:/mail/aliases");
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;//行号
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
//显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
//以行为单位读取文件end
}
}
}
}catch(IOException e){
e.printStackTrace();
}
相关文章推荐
- 【Linux命令】杀死僵尸程序
- linux服务器 磁盘和文件系统管理(二) LVM逻辑卷管理的基本操作
- R语言—Linux下R语言的下载与安装
- 阿里云的centos 6.5下安装open***服务器
- Linux软连接和硬链接
- linux常用命令系列—cp 复制文件与文件夹
- linux tail命令的使用方法详解
- centos 7 安装和配置vncserver
- linux常用指令
- VirtualBox 安装CentOs7后无法联网问题
- CentOS 6 rsync 文件同步
- CentOS6/5下vsftpd(虚拟用户)的配置
- Use multiple CPU Cores with your Linux commands — awk, sed, bzip2, grep, wc, etc.
- gdbserver调试工具移植到ARM Linux教程
- redhat/centos修改yum源
- 那年,一步一步学linux c
- Android和Linux版本对应
- linux下配置ODBC连接MySql
- Writing a Linux Keylogger in C
- Linux系统如何查看版本信息