Java Simulation linux Command grep__linux

Source: Internet
Author: User

On the Internet to see the MATLAB simulation linux grep command, think of Java also to simulate the Linux command grep,

The algorithm is very simple, here directly on the code:


/**
* Simulate UNIX command grep operation, output line number and line contents
* @param path of the file to be queried
* @param the string that query queries
* @throws IOException

* */
public static void grep (String path,string query) throws ioexception{
int lineno=0;
String line = null;

FileReader in=new FileReader (path);
BufferedReader br=new BufferedReader (in);
SYSTEM.OUT.PRINTLN ("Documents to be processed:" +path);
System.out.println ("line number-------------" + "contains" the "+query+");

Line=br.readline ();
while (Line!=null) {
lineno++;
if (line.indexof (query)!=-1)
System.out.println (lineno+ "---------------" +line);
Line=br.readline ();
}
}

Write the test code in JUNIT4 as follows:

//@Ignore
@Test
public void Testgrep () throws Exception {
String query= "link";
String path= "E:/greptest.txt";
Stringutil.grep (Path,query);
}

For the following test file Greptest.txt:

The difference between forward () and redirect () in the Java Servlet API


The former is only a diversion of control in the container,
No turning address is displayed in the client browser's address bar;
The latter is a complete jump, the browser will get the address of the jump, and resend the request link.
In this way, you can see the link address after the jump from the address bar of the browser.
Therefore, the former is more efficient, when the former can meet the needs, try to use the forward () method,
And, this also helps to hide the actual links. In some cases,
For example, you must use the Sendredirect () method if you want to jump to a resource on another server.

The resulting test results are as follows:

Files to be processed: e:/greptest.txt
Line number-------------The line content that contains ' links '
6---------------The latter is a complete jump, the browser will get the address of the jump and resend the request link.
7---------------In this way, you can see the link address after the jump from the browser's address bar.
9---------------and this also helps to hide the actual links. In some cases,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.