1. java获取当前路径的几种方法
1、利用System.getProperty()函数获取当前路径:
System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
2、使用File提供的函数获取当前路径:
File directory = new File("");//设定为当前文件夹
try{
System.out.println(directory.getCanonicalPath());//获取标准的路径
System.out.println(directory.getAbsolutePath());//获取绝对路径
}catch(Exceptin e){}
File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(".")和new File("..")两种路径有所区别。
# 对于getCanonicalPath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹
# 对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径
# 至于getPath()函数,得到的只是你在new File()时设定的路径
比如当前的路径为 C:/test :
File directory = new File("abc");
directory.getCanonicalPath(); //得到的是C:/test/abc
directory.getAbsolutePath(); //得到的是C:/test/abc
direcotry.getPath(); //得到的是abc
File directory = new File(".");
directory.getCanonicalPath(); //得到的是C:/test
directory.getAbsolutePath(); //得到的是C:/test/.
direcotry.getPath(); //得到的是.
File directory = new File("..");
directory.getCanonicalPath(); //得到的是C:/
directory.getAbsolutePath(); //得到的是C:/test/..
direcotry.getPath(); //得到的是..
另外:System.getProperty()中的字符串参数如下:
System.getProperty()参数大全
# java.version Java Runtime Environment version
# java.vendor Java Runtime Environment vendor
# java.vendor.url Java vendor URL
# java.home Java installation directory
# java.vm.specification.version Java Virtual Machine specification version
# java.vm.specification.vendor Java Virtual Machine specification vendor
# java.vm.specification.name Java Virtual Machine specification name
# java.vm.version Java Virtual Machine implementation version
# java.vm.vendor Java Virtual Machine implementation vendor
# java.vm.name Java Virtual Machine implementation name
# java.specification.version Java Runtime Environment specification version
# java.specification.vendor Java Runtime Environment specification vendor
# java.specification.name Java Runtime Environment specification name
# java.class.version Java class format version number
# java.class.path Java class path
# java.library.path List of paths to search when loading libraries
# java.io.tmpdir Default temp file path
# java.compiler Name of JIT compiler to use
# java.ext.dirs Path of extension directory or directories
# os.name Operating system name
# os.arch Operating system architecture
# os.version Operating system version
# file.separator File separator ("/" on UNIX)
# path.separator Path separator (":" on UNIX)
# line.separator Line separator ("/n" on UNIX)
# user.name User’s account name
# user.home User’s home directory
# user.dir User’s current working directory
JAVA中获取路径:
1.jsp中取得路径:
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")
结果:D:/resin/webapps/TEST/test.jsp
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:/resin/webapps/TEST
2.在类中取得路径:
(1)类的绝对路径:Class.class.getClass().getResource("/").getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:System.getProperty("user.dir")
结果:D:/TEST
3.在Servlet中取得路径:
(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:/Tomcat/webapps/TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test
2. java 获取int类型数据长度的方法
可以使用以下三种方法:
一、基于对数的解决方案:
valueOf 方法返回的相关Number对象持有传递的参数的值。
该参数可以是基本数据类型,字符串(String),等等。 这个方法是一个静态方法。该方法可以采取两个参数,其中一个是String,另一个是基数。
使用语句:int numberOfDigits = String.valueOf(n).length();。
二、使用对数方法:
java.lang.StrictMath.log10() 方法返回以10为底的对数的double值。
它包括了一些情况: 如果参数为NaN或小于零,那么结果为NaN。 如果参数为正无穷大,那么结果为正无穷大。 如果参数为正零或负零,那么结果是负无穷大。 如果参数等于10N的整数n,那么结果是n。
使用语句:int nDigits = floor(log10(abs(the_integer))) + 1;需要使用inclue math.h库。
三、判断数据的大小:
整数类型,任意一个数字<10就是一位数,实现代码如下:
3. JAVA反射中通过Class.forname()如何带参数的方法怎么赋值和调用呀
要调用带参数的方法方法举个例子:package com;
import java.lang.reflect.Method;
public class InvokeMethod {
public static void main(String args[]){
try
{
//动态加载Student类
Class student = Class.forName("com.Student");
//获取Student类名称为printInfo的方法
Method method1 = student.getMethod("printInfo");
//调用printInfo方法
method1.invoke(student.newInstance());
//获取Student类名称为printInfo的方法,String.class是方法的参数类型
Method method2 = student.getMethod("printAddress",String.class);
//调用printAddress方法,其中HK是为方法传递一个参数值
method2.invoke(student.newInstance(),"hello关知红");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
4. java中 如何得到泛型参数的类型,并在另一个方法中使用
在Java编程语言中,处理泛型时,获取泛型参数类型并在其他方法中使用是一个有趣的挑战。考虑以下示例代码:
public class B {
public <T> T set(T t) {
return t; //这里接受到return回去。
}
public void get() {
//注意这里String不是B的子类,会报错。
T t = (T) set(new String());
}
}
在这个例子中,我们定义了一个名为B的类。类中包含两个方法:set和get。set方法接受一个泛型参数T,并返回该参数。get方法尝试将一个String实例转换为泛型类型T。
然而,直接在get方法中使用泛型参数T时会遇到类型转换错误。这是因为Java泛型在运行时被擦除,导致编译器无法保证T的具体类型。为了在get方法中正确使用T,我们需要一些额外的逻辑,例如通过反射获取类型信息。
例如,可以通过以下方式改进get方法:
public void get() {
ParameterizedType parameterizedType = getClass().getGenericSuperclass();
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
Type tType = actualTypeArguments[0];
T t = (T) set(new String());
}
通过这种方式,我们可以在运行时获取泛型参数类型,并在get方法中使用它。这种方法需要使用反射API来访问泛型类型信息。
需要注意的是,这种方法在某些场景下可能会引入额外的开销,并且可能违反类型安全性。因此,在实际项目中使用时应谨慎考虑。
此外,还可以通过创建一个泛型类或接口,使泛型参数类型在类或接口级别上定义,从而在方法中更方便地使用。例如:
public class B<T> {
public T set(T t) {
return t;
}
public void get() {
T t = (T) set(new String());
}
}
通过这种方式,泛型参数T在类级别上定义,可以在类的所有方法中直接使用,无需额外的类型转换。
总之,在Java中处理泛型参数类型时,需要灵活运用反射和泛型类等技术来实现所需功能。同时,应考虑性能和类型安全性的平衡,选择最适合项目需求的方法。