java实现简单的shell命令

本文介绍了一个使用Java进行文件读取和正则表达式匹配的例子,演示了如何从文件中查找特定字符串并打印出来的方法。通过修改代码中的正则表达式,可以灵活地搜索不同的关键词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.grep "hello"file:检查文件里的每一行,将开头是hello的行打印出来。
文件操作和正则表达式比较复杂,但此处用的却较为简单
直接上代码吧:

  import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class test{
    	public static void printByFileReaderLine(String filePath) throws IOException
    	{    		    		
    	    BufferedReader reader=new BufferedReader(new FileReader(filePath));
    	    //采用按行读取的方式从文件中读取
    	    if(!reader.ready())
    	    {
    	        System.out.println("文件流暂时无法读取");
    	        return;
    	    }
    	    String line;
    	    while((line=reader.readLine())!=null)
    	    {
    	        Pattern pattern=Pattern.compile("hello");  	  
    	        Matcher matcher=pattern.matcher(line);
    	        if(matcher.find())System.out.println(line);	        
    	    }
    	    //正则匹配,找到有hello的行并打印
    	    reader.close();	    
    	}
    public static void main(String args[]) throws IOException {
    	String str="content";
    	printByFileReaderLine(str);
    	}
    }

关于更多的文件操作,可以看看这个博客:https://blog.youkuaiyun.com/qq_21808961/article/details/81561464
正则匹配:https://www.cnblogs.com/yw0219/p/8047938.html
2.echo “test\n\string\nfor\ngrep”|grep “string”。这条语句将echo后面的字符串打印内容作为grep的输入参数,会打印出含有string的行
其实稍微改一点就可以,把需要匹配的hello换成输入的string

 import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class test{
    	public static void printByFileReaderLine(String filePath) throws IOException
    	{
    		System.out.print("请输入:");
    		Scanner sc=new Scanner(System.in);
    		String str=sc.nextLine();
    	    BufferedReader reader=new BufferedReader(new FileReader(filePath));
    	    if(!reader.ready())
    	    {
    	        System.out.println("文件流暂时无法读取");
    	        return;
    	    }
    	    String line;
    	    while((line=reader.readLine())!=null)
    	    {
    	        //Pattern pattern=Pattern.compile("hello");
    	        Pattern pattern=Pattern.compile(str);
    	        Matcher matcher=pattern.matcher(line);
    	        if(matcher.find())System.out.println(line);	        
    	    }
    	    reader.close();	    
    	}
    public static void main(String args[]) throws IOException {
    	String str="content";
    	printByFileReaderLine(str);
    	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值