(Practical JavaBean) Get the Java file path and remove spaces at both ends
1. How to get the current file path (File:/D:/workspace2/test/webroot/WEB-INF/classes /)
Package com. zcjl. Test. base;
Import java. Io. file;
Public class test {
Public static void main (string [] ARGs) throws exception {
System. Out. println (
Thread. currentthread (). getcontextclassloader (). getresource (""));
System. Out. println (test. Class. getclassloader (). getresource (""));
System. Out. println (classloader. getsystemresource (""));
System. Out. println (test. Class. getresource (""));
System. Out. println (test. Class. getresource ("/"));
System. Out. println (new file (""). getabsolutepath ());
System. Out. println (system. getproperty ("user. dir "));
}
}
Obtain the true file path
String file_real_path = request. getrealpath ("/uploadfiles /";);
// D:/tomcat 5.0/webapps/studyteach/uploadfiles
Obtain the server path (port 80, which can be converted to 8080)
Out. println ("document. Location = 'HTTP: //" + request. getservername () + "/uploadfiles/" + inf1.getfilesrc () + "';");
// Download
2 remove spaces at both ends
A comprehensive test class:
Public class test {
Public static void main (string [] ARGs ){
System. Out. println ("lefttrim:" + lefttrim ("123 123 123") + ":");
System. Out. println ("righttrim:" + righttrim ("123 123 123") + ":");
System. Out. println ("Trim:" + trim ("123 123 123") + ":");
System. Out. println ("lefttrim:" + TRIMS ("123 123 123", "L") + ":");
System. Out. println ("righttrim:" + TRIMS ("123 123 123", "R") + ":");
System. Out. println ("Trim:" + TRIMS ("123 123 123", "A") + ":");
}
/* Remove left and right Spaces */
Public static string trim (string Str ){
If (STR = NULL | Str. Equals ("")){
Return STR;
} Else {
// Return lefttrim (righttrim (STR ));
Return Str. replaceall ("^ [] + | [] + $ ","");
}
}
/* Remove the left space */
Public static string lefttrim (string Str ){
If (STR = NULL | Str. Equals ("")){
Return STR;
} Else {
Return Str. replaceall ("^ [] + ","");
}
}
/* Remove the right space */
Public static string righttrim (string Str ){
If (STR = NULL | Str. Equals ("")){
Return STR;
} Else {
Return Str. replaceall ("[] + $ ","");
}
}
/* L, R, and a are left, right, and spaces on both sides */
Public static string TRIMS (string STR, string flag ){
If (STR = NULL | Str. Equals ("")){
Return STR;
} Else {
STR = "" + STR;
If (flag = "L" | flag = "L")/* trim left side only */
{
String regularexp = "^ [] + ";
Return Str. replaceall (regularexp ,"");
}
Else if (flag = "R" | flag = "R")/* trim right side only */
{
String regularexp = "[] + $ ";
Return Str. replaceall (regularexp ,"");
}
Else/* defautly, trim both left and right side */
{
String regularexp = "^ [] + | [] + $ ";
Return Str. replaceall (regularexp ,"");
}
}
}
}
Running result:
Lefttrim: 123 123 123:
Righttrim: 123 123 123:
Trim: 123 123 123:
Lefttrim: 123 123 123:
Righttrim: 123 123 123:
Trim: 123 123 123:
2 remove the space at both ends from the "Blue Ocean" blog, please be sure to keep this source http://su3390.blog.51cto.com/176528/33671