How can i get and create access permission ,group and user of a file on linux pc in java.
Asked
Active
Viewed 339 times
2 Answers
0
If you're using Java 7, you should be able to use the POSIX functionality to do this. Specifically, the getPosixFilePermissions
method should be what you're looking for. See this question for more information.
-
thank for your answer.but i want to use this getPosixFilePermissions method in java 6 – Jar Yit Feb 20 '13 at 16:35
-
-
It`s not available to use java 7 in our project because of our development framework is base on java 6. – Jar Yit Feb 20 '13 at 16:40
0
package Test.Dir.Onlinux;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.lang.System;
public class TestDirOnLinux {
public static void main(String[] args) {
String linuxCommands[] = { "stat -c %U ", "stat -c %G ", "stat -c %A ", "stat -c %a " };
String fileName = "/home/zmt/UberSVN";
excuteLinuxCommand("192.168.0.26", "root", "ace@dir", linuxCommands[0] + fileName);
excuteLinuxCommand("192.168.0.26", "root", "ace@dir", linuxCommands[1] + fileName);
excuteLinuxCommand("192.168.0.26", "root", "ace@dir", linuxCommands[2] + fileName);
excuteLinuxCommand("192.168.0.26", "root", "ace@dir", linuxCommands[3] + fileName);
}
public static void excuteLinuxCommand(String ipAddress, String userName, String password, String linuxCommands) {
boolean isAuthenticated = false;
try {
// Connection conn = new Connection(hostname);
Connection conn = new Connection(ipAddress);
conn.connect();
// isAuthenticated = conn.authenticateWithPassword(username,
// password);
isAuthenticated = conn.authenticateWithPassword(userName, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
Session sess = conn.openSession();
// sess.execCommand("shutdown -h now");
sess.execCommand(linuxCommands);
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
InputStreamReader insrout = new InputStreamReader(stdout);
InputStreamReader insrerr = new InputStreamReader(stderr);
BufferedReader stdoutReader = new BufferedReader(insrout);
BufferedReader stderrReader = new BufferedReader(insrerr);
while (true) {
String line = stdoutReader.readLine();`enter code here`
if (line == null) {
break;
}
System.out.println(line);
}
while (true) {
String line = stderrReader.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
sess.close();
conn.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
}
}
enter code here

Jar Yit
- 955
- 11
- 22