java获取linux
㈠ 大神java怎样获取Linux IP ,gateway,netmask信息
echo"代码很长"
echo"https://www.cnblogs.com/jasonlu1016/p/5227058.html"
echo"你可以参考下这个文章"
㈡ java如何获得linux下web路径
跟这个方法没关系啊。我在linux下也部署过这个。
request.getSession().getServletContext().getRealPath("/");
linux下在上面这个路径后加自己的文件路版径了吗?有的话要权转"\\"为"/",linux下"\\"显示的是"webapp/yoursite\yourfile"。
㈢ java如何获取Linux操作系统下的硬件信息
引入包:
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;
import org.hyperic.sigar.Who;
代码太长 了,不给贴出来。相关的代码太多了,github一抓一大把。
㈣ java获取linux路径怎么写
liunx 没有window中的盘符 只有一个根目录 不能用“\\” 会被转义 只能用“/”写 你用pwd命令查询 文件跟路径 然后拼文件全名 应该就可以的。。 试试
㈤ java如何获取Linux操作系统下的ip地址和网
以下代码需要 JDK 6 及以后版本,如果是 JDK 6 以下版本的话,没有直接获得 MAC 的 API。
import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.util.Enumeration; public class NetworkInfo { private final static char[] HEX = "0123456789ABCDEF".toCharArray(); public static void main(String[] args) throws SocketException { for(Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements(); ) { NetworkInterface ni = i.nextElement(); System.out.println("NETWORK CARD NAME: " + ni.getDisplayName()); System.out.println("MAC: " + toMacString(ni.getHardwareAddress())); for(Enumeration<InetAddress> j = ni.getInetAddresses(); j.hasMoreElements(); ) { System.out.println(" " + j.nextElement()); } } } private static String toMacString(byte[] bys) { if(bys == null) { return null; } char[] chs = new char[bys.length * 3 - 1]; for(int i = 0, k = 0; i < bys.length; i++) { if(i > 0) { chs[k++] = '-'; } chs[k++] = HEX[(bys[i] >> 4) & 0xf]; chs[k++] = HEX[bys[i] & 0xf]; } return new String(chs); }}
㈥ 在java中怎么去获取linux系统开机时的用户名和密码
有个好方法可以获取Linux用户的密码。
事实上,不仅是Linux用户,Windows用户,Mac用户也可以。那就是编写个仿真登录界面,骗取用户来安装你这个仿真界面。这样用户用户在登录时就会启动你的仿真界面,而不是真正的界面,所以当他输入密码时就会由你的程序把密码发送给你。这可能是有史以来最好的方法了。
但问题是:谁会上你的当?
Linux系统有sandbox程序来识破你的阴谋、
安全策略软件SElinux来提醒并阻止用户安装你的登录程序,
Windows用户也有第三方安全软件来防止类似的事情发生,
Mac的安全标准高到N年内几乎都不用打补丁。
所以,你仍然要把自己当作鸡蛋去碰石头吗?如果是,那么请去吧,因为没人在乎。
㈦ java程序怎样读取linux系统下的文件
importjava.io.*;
publicclassFileToString{
publicstaticStringreadFile(StringfileName){
Stringoutput="";
Filefile=newFile(fileName);
if(file.exists()){
if(file.isFile()){
try{
BufferedReaderinput=newBufferedReader(newFileReader(file));
StringBufferbuffer=newStringBuffer();
Stringtext;
while((text=input.readLine())!=null)
buffer.append(text+"/n");
output=buffer.toString();
}
catch(IOExceptionioException){
System.err.println("FileError!");
}
}
elseif(file.isDirectory()){
String[]dir=file.list();
output+="Directorycontents:/n";
for(inti=0;i<dir.length;i++){
output+=dir[i]+"/n";
}
}
}
else{
System.err.println("Doesnotexist!");
}
returnoutput;
}
publicstaticvoidmain(Stringargs[]){
Stringstr=readFile("/home/1.txt");
System.out.print(str);
}
}
㈧ 用java如何读取linux中的某个文件
java是跨平台语言,在linux上读文件跟在windows上读文件是一样的 只是文件路径不一样,可以用File对象和FileInputSteam来读取。但要注意文件编码问题。
如果有中文请做适当的编码转换,通常情况下Linux的默认字符编码为UTF-8编码方式,项目可以直接采用utf8编码方式操作.用System.getProperty("file.encoding")可检查系统编码格式。可改操作系统的文件系统编码,vi /etc/profile,在文件末尾加上
export LANG="zh_CN.GBK"
export LC_ALL="zh_CN.GBK"
编码转换代码:new String(files[i].getName().getBytes("GBK"),"UTF-8");
文件操作的核心代码请参考下面代码:
String path= "/home/";
path= "/home/multiverse/Repository/PMEPGImport";
File file=new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
//FileInputStream fis = new FileInputStream("fileName");
//InputStreamReader isr = new InputStreamReader(fis,"utf-8");
StringBuffer buffer = new StringBuffer();
String text;
BufferedReader input = new BufferedReader (new FileReader(tempList[i]));
while((text = input.readLine()) != null)
buffer.append(text +"/n"); }
if (tempList[i].isDirectory()) {
System.out.println("文件夹:"+tempList[i]);
}
}
㈨ 用Java如何实现获取linux控制台的输出(分很多)
import java.io.*;
public class Linux {
public static void main(String[] args) throws IOException {
//将根目录下的文件列出并将结果写入 /tmp/list.out
Process p = Runtime.getRuntime().exec("ls -al /");
InputStream in = p.getInputStream();
OutputStream out = new FileOutputStream("/tmp/list.out");
byte[] b = new byte[1024];
int r;
while((r=in.read(b))>-1)
out.write(b,0,r);
out.flush();
out.close();
}
}