(Practical JavaBean) Get the Java file path and remove spaces at both ends

Source: Internet
Author: User

(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

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.