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,