java中获取路径的几种方式

总是忘记, 备份一下,方便下次用.

第一种:

File directory = new File("");//参数为空

String courseFile = directory.getCanonicalPath()
;System.out.println(courseFile);

结果:C:\Documents and
Settings\Administrator\workspace\projectName获取当前类的所在工程路径;

第二种:

File f = new File(this.getClass().getResource("/").getPath());

System.out.println(f);

结果:C:\Documents and Settings\Administrator\workspace\projectName\bin

如果不加“/”

File f = new File(this.getClass().getResource("").getPath());

System.out.println(f);

结果:C:\Documents and
Settings\Administrator\workspace\projectName\bin\com\test

第三种:URL xmlpath =
this.getClass().getClassLoader().getResource("selected.txt");

System.out.println(xmlpath);

结果:file:/C:/Documents and
Settings/Administrator/workspace/projectName/bin/selected.txt

第四种:

System.out.println(System.getProperty("user.dir"));

结果:C:\Documents and Settings\Administrator\workspace\projectName

第五种:System.out.println( System.getProperty("java.class.path"));

结果:C:\Documents and Settings\Administrator\workspace\projectName\bin



import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.RandomAccessFile;

public class FileOperation {

/**
* 创建文件
* @param fileName
* @return
*/
public static boolean createFile(File fileName)throws Exception{
boolean flag=false;
try{
if(!fileName.exists()){
fileName.createNewFile();
flag=true;
}
}catch(Exception e){
e.printStackTrace();
}
return true;
}

/**
* 读TXT文件内容
* @param fileName
* @return
*/
public static String readTxtFile(File fileName)throws Exception{
String result=null;
FileReader fileReader=null;
BufferedReader bufferedReader=null;
try{
fileReader=new FileReader(fileName);
bufferedReader=new BufferedReader(fileReader);
try{
String read=null;
while((read=bufferedReader.readLine())!=null){
result=result+read+"\r\n";
}
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(bufferedReader!=null){
bufferedReader.close();
}
if(fileReader!=null){
fileReader.close();
}
}
System.out.println("读取出来的文件内容是:"+"\r\n"+result);
return result;
}

public static boolean writeTxtFile(String content,File fileName)throws Exception{
RandomAccessFile mm=null;
boolean flag=false;
FileOutputStream o=null;
try {
o = new FileOutputStream(fileName);
o.write(content.getBytes("GBK"));
o.close();
// mm=new RandomAccessFile(fileName,"rw");
// mm.writeBytes(content);
flag=true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(mm!=null){
mm.close();
}
}
return flag;
}

public static void contentToTxt(String filePath, String content) {
String str = new String(); //原有txt内容
String s1 = new String();//内容更新
try {
File f = new File(filePath);
if (f.exists()) {
System.out.print("文件存在");
} else {
System.out.print("文件不存在");
f.createNewFile();// 不存在则创建
}
BufferedReader input = new BufferedReader(new FileReader(f));

while ((str = input.readLine()) != null) {
s1 += str + "\n";
}
System.out.println(s1);
input.close();
s1 += content;

BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();

}
}

}

java中获取路径的几种方式

时间: 2024-08-02 02:42:14

java中获取路径的几种方式的相关文章

Java中获取配置文件的四种方式

一,类加载器classLoader 二,getResourcesAsStream方法 三,Properties对象 四,ResouceBundle对象 五,四种方式代码演示 public class LoadProperties { public static void main(String[] args) throws Exception, IOException { //方式一,在项目同级目录下 /*Properties p = new Properties(); p.load(new F

JAVA中集合输出的四种方式

在JAVA中Collection输出有四种方式,分别如下: 一) Iterator输出. 该方式适用于Collection的所有子类. public class Hello { public static void main(String[] args) throws Exception { Set<Person> javaProgramers = new HashSet<Person>(); javaProgramers.add(new Person("aaron&qu

java中设置代理的两种方式

1 前言 有时候我们的程序中要提供可以使用代理访问网络,代理的方式包括http.https.ftp.socks代理.比如在IE浏览器设置代理. 那我们在我们的java程序中使用代理呢,有如下两种方式.直接上代码. 2 采用设置系统属性 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import jav

JAVA中获取路径

内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径:   以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.getRequestURI() 结果:/TEST/test.jsp (2)得到工程名:request.getContextPath() 结果:/TEST (3)得到当前页面所在目录下全名称:request.getServletPath() 结果:如果页面在jsp目录下 /TEST/jsp/test.jsp

java动态获取WebService的两种方式(复杂参数类型)

java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_FlightOrderSearchRequest request) { Object myAllMessage; OrderSearchListRes response = null; try { String endpoint = carGlobalSetting.getEndpoint(); JaxWsD

Java中获取类名的3种方法!

获取类名的方法 Java 中获取类名的方式主要有以下三种. getName() 返回的是虚拟机里面的class的类名表现形式. getCanonicalName() 返回的是更容易理解的类名表示. getSimpleName() 返回的是类的简称. 都有什么区别? 通过一个实例来看下它们主要的区别. public class TestClass { public static void main(String[] args) { // 外部普通类 System.out.println("方法名

AngularJS中获取数据源的几种方式

在AngularJS中,可以从$rootScope中获取数据源,也可以把获取数据的逻辑封装在service中,然后注入到app.run函数中,或者注入到controller中.本篇就来整理获取数据的几种方式. ■ 数据源放在$rootScope中 var app = angular.module("app",[]); app.run(function($rootScope){ $rootScope.todos = [ {item:"",done:true}, {it

5.struts2中Action类中获取ServletAPI的三种方式

**Servlet的API的访问(开发中偶尔会使用到)** 1.在Action类中也可以获取到Servlet一些常用的API,有如下三种方式获取 * 完全解耦合的方式 * 使用接口注入的方式 * 使用ServletActionContext中静态方法直接访问Servlet的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的页面上. * 提供JSP注册的页面,演示下面这三种方式: <h3>注册页面</

JAVA中实现多线程的四种方式

Java中多线程实现方式主要有四种:1<继承Thread类.2<实现Runnable接口.3<实现Callable接口通过FutureTask包装器来创建Thread线程.4<使用ExecutorService.Callable.Future实现有返回结果的多线程. 其中前两种方式线程执行完后都没有返回值,后两种是带返回值的. 1.继承Thread类创建线程 Thread类本质上是实现了Runnable接口的一个实例,代表一个线程的实例.启动线程的唯一方法就是通过Thread类的s