
Linux是一种基于Unix的操作系统,其命令行界面具有强大的功能,可以轻松地读取和编辑文件。对于Java开发人员来说,是一项非常必要的技能。在本文中,我们将介绍一些常用的Linux命令以及如何使用它们来查看和编辑Java代码。
查看文件
在Linux中,我们可以使用多个命令来查看文件,其中最常用的是cat、less和more命令。下面是它们的基本用法。
cat命令:将文件的全部内容打印到控制台上。
“`shell
cat file.java
“`
less命令:分页显示文件内容,可以逐页向下滚动查看。
“`shell
less file.java
“`
more命令:分页显示文件内容,可以逐页向下翻页查看。
“`shell
more file.java
“`
以上这些命令可以让我们在控制台上查看文件的内容,但是对于较大的文件,显示可能会变得杂乱无章,为了更好地查看文件内容,我们需要使用一些其他命令。
grep命令
grep命令是一种强大的文本搜索工具,它可以在文件中查找特定的字符串或模式。对于Java开发人员来说,我们可以使用grep命令来查找关键字或特定的代码行。下面是一些示例命令。
查找关键字
“`shell
grep “keyword” file.java
“`
查找特定的代码行
“`shell
grep “public class” file.java
“`
以上示例命令可以让我们快速查找文件中的特定内容。
sed命令
sed命令是一种基于行的文本编辑器,在Linux中使用非常广泛。对于Java开发人员来说,我们可以使用sed命令来替换文件中的特定字符串或删除行。下面是一些示例命令。
替换字符串
“`shell
sed ‘s/old_string/new_string/g’ file.java
“`
删除行
“`shell
sed ‘/delete_line/d’ file.java
“`
以上命令可以让我们更方便地编辑和修改Java代码。
vi和vim命令
vi和vim命令是Linux中最常用的文本编辑器之一。它们拥有许多强大的编辑功能,非常适用于Java开发者。下面是一些示例命令。
打开文件
“`shell
vi file.java
“`
在vi或vim编辑器中,您可以使用箭头键或h、j、k、l键来移动光标。使用i、o或a键进入编辑模式,可以编辑文件内容。使用:wq命令保存并退出编辑器。
在本文中,我们介绍了一些常用的Linux命令,以及如何使用它们来查看和编辑Java代码。这些命令涵盖了最基本的功能,但是对于大部分情况来说已经足够。对于一些更高级的需求,您可以通过熟练掌握这些命令,进一步探索和学习更高级的Linux命令和技巧。通过学习这些命令,您可以更方便地阅读和修改Java代码,并且加快开发速度。
相关问题拓展阅读:
- 怎么用java代码读取linux主机的磁盘使用信息,同时截取出文件系统和已使用情况 放在map中可以得到keyvalu
- linux环境下,在java中用sun.net.ftp.FtpClient类去读取文件名含有“点号”的文件时报错找不到文件
- java window和linux FileInputStream读文件路径问题
怎么用java代码读取linux主机的磁盘使用信息,同时截取出文件系统和已使用情况 放在map中可以得到keyvalu
package com.cmmb.util;
import java.io.*;
/**
* linux 下cpu 内存 磁盘 jvm的使用监控
* @author avery_leo
*
*/
public class DiskSpace {
/**
* 获取cpu使用情况
* @return
* @throws Exception
*/
public double getcpuUsage() throws Exception {
double cpuUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“top -b -n 1”);// 调用系统的“top”命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(” R “) != -1) {// 只分析正在运行空型的进程,top进程本身镇扒除外 &&
strArray = str.split(” “);
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 9) {// 第9列为cpu的使用百分比(RedHat
cpuUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuUsed;
}
/**
* 内存监控
* @return
* @throws Exception
*/
public double getMemUsage() throws Exception {
double menUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“top -b -n 1”);// 调用系统的御亏昌“top”命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(” R “) != -1) {// 只分析正在运行的进程,top进程本身除外 &&
//
// System.out.println(“”);
strArray = str.split(” “);
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 10) {
// 9)–第10列为mem的使用百分比(RedHat 9)
menUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return menUsed;
}
/**
* 获取磁盘空间大小
*
* @return
* @throws Exception
*/
public double getDeskUsage() throws Exception {
double totalhd = 0;
double usedhd = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“df -hl /home”);//df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(” “);
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
++m;
System.out.println(“—-tmp—-” + tmp);
if (tmp.indexOf(“G”) != -1) {
if (m == 2) {
System.out.println(“—G—-” + tmp);
if (!tmp.equals(“”) && !tmp.equals(“0”))
totalhd += Double.parseDouble(tmp
.substring(0, tmp.length() – 1)) * 1024;
}
if (m == 3) {
System.out.println(“—G—-” + tmp);
if (!tmp.equals(“none”) && !tmp.equals(“0”))
usedhd += Double.parseDouble(tmp.substring(
0, tmp.length() – 1)) * 1024;
}
}
if (tmp.indexOf(“M”) != -1) {
if (m == 2) {
System.out.println(“—M—” + tmp);
if (!tmp.equals(“”) && !tmp.equals(“0”))
totalhd += Double.parseDouble(tmp
.substring(0, tmp.length() – 1));
}
if (m == 3) {
System.out.println(“—M—” + tmp);
if (!tmp.equals(“none”) && !tmp.equals(“0”))
usedhd += Double.parseDouble(tmp.substring(
0, tmp.length() – 1));
System.out.println(“” + usedhd);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
//上面写在userd和total写反了,懒得改了,就反着用了
System.out.println(“—-totalhd—-” + usedhd);
System.out.println(“—-usedhd—-” + totalhd);
return (totalhd / usedhd) * 100;
}
public static void main(String args) throws Exception {
DiskSpace cpu = new DiskSpace();
System.out.println(“-cpu used:” + cpu.getcpuUsage() + “%”);
System.out.println(“-mem used:” + cpu.getMemUsage() + “%”);
System.out.println(“-HD used:” + cpu.getDeskUsage() + “%”);
System.out.println(“jvm监控”);
Runtime lRuntime = Runtime.getRuntime();
System.out.println(“Free Momery:” + lRuntime.freeMemory()+”K”);
System.out.println(“Max Momery:” + lRuntime.maxMemory()+”K”);
System.out.println(“Total Momery:” + lRuntime.totalMemory()+”K”);
System.out.println(“-Available Processors :”
+ lRuntime.availableProcessors());
}
}
package com.cmmb.util;
import java.io.*;
/**
* linux 下cpu 内存 磁盘 jvm的使用监控
* @author avery_leo
*
*/
public class DiskSpace {
/**
* 获取cpu使用情况
* @return
* @throws Exception
*/
public double getCpuUsage() throws Exception {
double cpuUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“top -b -n 1”);// 调用系统的“top”命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(” R “) != -1) {// 只分析正在运行空型的进程,top进程本身镇扒除外 &&
strArray = str.split(” “);
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 9) {// 第9列为CPU的使用百分比(RedHat
cpuUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuUsed;
}
/**
* 内存监控
* @return
* @throws Exception
*/
public double getMemUsage() throws Exception {
double menUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“top -b -n 1”);// 调用系统的御亏昌“top”命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(” R “) != -1) {// 只分析正在运行的进程,top进程本身除外 &&
//
// System.out.println(“”);
strArray = str.split(” “);
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 10) {
// 9)–第10列为mem的使用百分比(RedHat 9)
menUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return menUsed;
}
/**
* 获取磁盘空间大小
*
* @return
* @throws Exception
*/
public double getDeskUsage() throws Exception {
double totalHD = 0;
double usedHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“df -hl /home”);//df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(” “);
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
++m;
System.out.println(“—-tmp—-” + tmp);
if (tmp.indexOf(“G”) != -1) {
if (m == 2) {
System.out.println(“—G—-” + tmp);
if (!tmp.equals(“”) && !tmp.equals(“0”))
totalHD += Double.parseDouble(tmp
.substring(0, tmp.length() – 1)) * 1024;
}
if (m == 3) {
System.out.println(“—G—-” + tmp);
if (!tmp.equals(“none”) && !tmp.equals(“0”))
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() – 1)) * 1024;
}
}
if (tmp.indexOf(“M”) != -1) {
if (m == 2) {
System.out.println(“—M—” + tmp);
if (!tmp.equals(“”) && !tmp.equals(“0”))
totalHD += Double.parseDouble(tmp
.substring(0, tmp.length() – 1));
}
if (m == 3) {
System.out.println(“—M—” + tmp);
if (!tmp.equals(“none”) && !tmp.equals(“0”))
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() – 1));
System.out.println(“” + usedHD);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
//上面写在userd和total写反了,懒得改了,就反着用了
System.out.println(“—-totalHD—-” + usedHD);
System.out.println(“—-usedHD—-” + totalHD);
return (totalHD / usedHD) * 100;
}
public static void main(String args) throws Exception {
DiskSpace cpu = new DiskSpace();
System.out.println(“-cpu used:” + cpu.getCpuUsage() + “%”);
System.out.println(“-mem used:” + cpu.getMemUsage() + “%”);
System.out.println(“-HD used:” + cpu.getDeskUsage() + “%”);
System.out.println(“jvm监控”);
Runtime lRuntime = Runtime.getRuntime();
System.out.println(“Free Momery:” + lRuntime.freeMemory()+”K”);
System.out.println(“Max Momery:” + lRuntime.maxMemory()+”K”);
System.out.println(“Total Momery:” + lRuntime.totalMemory()+”K”);
System.out.println(“-Available Processors :”
+ lRuntime.availableProcessors());
}
}
package com.cmmb.util;
import java.io.*;
/**
* linux 下cpu 内存 磁盘 jvm的使用监控
* @author avery_leo
*
*/
public class DiskSpace {
/**
* 获取cpu使用情况
* @return
* @throws Exception
*/
public double getCpuUsage() throws Exception {
double cpuUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“top -b -n 1”);// 调用系统的“top”命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(” R “) != -1) {// 只分析正在运行空型的进程,top进程本身镇扒除外 &&
strArray = str.split(” “);
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 9) {// 第9列为CPU的使用百分比(RedHat
cpuUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuUsed;
}
/**
* 内存监控
* @return
* @throws Exception
*/
public double getMemUsage() throws Exception {
double menUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“top -b -n 1”);// 调用系统的御亏昌“top”命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(” R “) != -1) {// 只分析正在运行的进程,top进程本身除外 &&
//
// System.out.println(“”);
strArray = str.split(” “);
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 10) {
// 9)–第10列为mem的使用百分比(RedHat 9)
menUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return menUsed;
}
/**
* 获取磁盘空间大小
*
* @return
* @throws Exception
*/
public double getDeskUsage() throws Exception {
double totalHD = 0;
double usedHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“df -hl /home”);//df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(” “);
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
++m;
System.out.println(“—-tmp—-” + tmp);
if (tmp.indexOf(“G”) != -1) {
if (m == 2) {
System.out.println(“—G—-” + tmp);
if (!tmp.equals(“”) && !tmp.equals(“0”))
totalHD += Double.parseDouble(tmp
.substring(0, tmp.length() – 1)) * 1024;
}
if (m == 3) {
System.out.println(“—G—-” + tmp);
if (!tmp.equals(“none”) && !tmp.equals(“0”))
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() – 1)) * 1024;
}
}
if (tmp.indexOf(“M”) != -1) {
if (m == 2) {
System.out.println(“—M—” + tmp);
if (!tmp.equals(“”) && !tmp.equals(“0”))
totalHD += Double.parseDouble(tmp
.substring(0, tmp.length() – 1));
}
if (m == 3) {
System.out.println(“—M—” + tmp);
if (!tmp.equals(“none”) && !tmp.equals(“0”))
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() – 1));
System.out.println(“” + usedHD);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
//上面写在userd和total写反了,懒得改了,就反着用了
System.out.println(“—-totalHD—-” + usedHD);
System.out.println(“—-usedHD—-” + totalHD);
return (totalHD / usedHD) * 100;
}
public static void main(String args) throws Exception {
DiskSpace cpu = new DiskSpace();
System.out.println(“-cpu used:” + cpu.getCpuUsage() + “%”);
System.out.println(“-mem used:” + cpu.getMemUsage() + “%”);
System.out.println(“-HD used:” + cpu.getDeskUsage() + “%”);
System.out.println(“jvm监控”);
Runtime lRuntime = Runtime.getRuntime();
System.out.println(“Free Momery:” + lRuntime.freeMemory()+”K”);
System.out.println(“Max Momery:” + lRuntime.maxMemory()+”K”);
System.out.println(“Total Momery:” + lRuntime.totalMemory()+”K”);
System.out.println(“-Available Processors :”
+ lRuntime.availableProcessors());
}
}
linux环境下,在java中用sun.net.ftp.FtpClient类去读取文件名含有“点号”的文件时报错找不到文件
public class P1{
private int a=null;
private int rownum=0,colnum=0;
//存储每行激昌中最小的那个数的行和列,如”1,2″—之一行,第二列
private int rows=null;
//存储每列中更大的那明带扒个数的行和列,如”行液1,2″—之一行,第二列
private int cols=null;
private int num=0;
P1(int a,int row,int col){
this.a = a;
this.rownum=row;
this.colnum=col;
rows = new int;
cols = new int
;
}
java对升枯哪.号解析的问题。linux是对.不敏感的。简单,转义字符,搞定。2023\.9\.9日吵码XX详情.doc
或者
2023\\.9\\.9日XX详情.doc
java window和linux FileInputStream读文件路径问题
Linux系统下的
文件夹
路径和window下的不一样,windows下就需要写成“\\photos”因为java会把之一个”\”当成
转义字符
给”吃了“。但在linux下就是
“/photos”呵呵,是不是很郁闷阿。所以你的if (myFile.newFolder(path+”\\photos”))
就应该写成if (myFile.newFolder(path+”/photos”))以此类推。
public static final String FILE_SEPARATOR = System.getProperties()。getProperty(”file.separator”);
文件
分隔符
(在 UNIX 系统中是“/”),window 是”\”
为了程序的可移植性,使用File.separator来写路径。
File(String pathname)
通过将给定路径名
字符串
转换为抽象路径名来创建一个新 File 实例。
public static final String separator
与系统有关的默认名称分隔符,为了方便,如漏它被表示为一个字符串。此字符串只包含一个字符,即 separatorChar。
public static final char separatorChar
与系统有关的默认名称分隔符。此字段被枝雀初始化为包含系统属性 file.separator 值的之一个字符。在 UNIX 系统上,此字段的值为 ‘/’;在 Microsoft Windows 系统上,它为 ‘\\’。
注意:
路径名字符串与抽象路径名之间的转换与系统有关。将抽象路径名转换为路径名字符串时,每个名称与下一个名称之间用一个默认分隔符 隔开。默认名称分隔符由系统属性 file.separator 定义,可通过此类的公共静态字段 separator 和 separatorChar 使其可用。将路径名渣搭烂字符串转换为抽象路径名时,可以使用默认名称分隔符或者底层系统支持的任何其他名称分隔符来分隔其中的名称
System.getProperty(“user.dir”) 获取工程路径名
System.getProperty(“file.separator”).equals(“/”) 判断linux的方法。
用File.separator这个可以直接替换路径问题,不需要判断
关于linux 读取文件 java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
香港服务器首选树叶云,2H2G首月10元开通。
树叶云(www.IDC.Net)提供简单好用,价格厚道的香港/美国云服务器和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/187196.html<