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

第一种:

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中获取路径的几种方式的更多相关文章

  1. java中获取路径的几种基本的方法

    package com.ygh.blog.realpath; import java.io.File; import java.io.IOException; import java.io.Input ...

  2. java项目获取路径的几种方式

    第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); ...

  3. JAVA中获取路径

    内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径:   以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.get ...

  4. Java中HashMap遍历的两种方式

    Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...

  5. JAVA中集合输出的四种方式

    在JAVA中Collection输出有四种方式,分别如下: 一) Iterator输出. 该方式适用于Collection的所有子类. public class Hello { public stat ...

  6. java中数组复制的两种方式

    在java中数组复制有两种方式: 一:System.arraycopy(原数组,开始copy的下标,存放copy内容的数组,开始存放的下标,需要copy的长度); 这个方法需要先创建一个空的存放cop ...

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

    java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...

  8. 干货 | Java中获取类名的3种方法!

    获取类名的方法 Java 中获取类名的方式主要有以下三种. getName() 返回的是虚拟机里面的class的类名表现形式. getCanonicalName() 返回的是更容易理解的类名表示. g ...

  9. java中获取路径中的空格处理(%20)问题

    在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...

随机推荐

  1. discuz中方法

    discuz中检验是否是邮箱 function isemail($email) { && strlen($email) <= && preg_match(&quo ...

  2. 类似于QQ的简单的聊天代码

    先编辑页面 package com.lovo.feichun; import java.awt.Color;import java.awt.Component;import java.awt.Cont ...

  3. LR常用函数以及调用自定义函数

    2.LR常用函数以及调用自定义函数 2.1.LR常用函数以及对信息的判断 2.1.1. LR内部自定义函数 在LR脚本中定义变量和编写自定义函数,需将变量的声明放在脚本其他内容的上方,否则会提示[il ...

  4. php阻止网页被用户频繁刷新

    一般情况下,用户浏览网页的速度都是几秒十几秒甚至更长时间刷新一页,但有时候又会遇到网页被恶意快速刷新,从而导致正常用户浏览速度缓慢,如何来解决这个问题呢?可以使用如下代码来实现每ip页面访问数量限制: ...

  5. vim编辑器详解

    vi(vim)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率. vi是“visual interface”的 ...

  6. ArcGIS Engine 刷新问题

    link: http://www.cnblogs.com/Jingkunliu/archive/2013/01/10/2854710.html PartialRefresh方法是部分刷新,效率方面比单 ...

  7. C, C#, AS3的变量

    高级语言中变量分值类型和引用类型, C中则分栈和堆, 在作为函数参数传递时,值类型复制,引用类型传递引用,不复制: 高级语言中一些基本类型默认是值类型, 其他基本都是引用类型. C语言中栈和堆变量可以 ...

  8. Unknown column 'a.root_id' in 'where clause'解决方法

    最近遇到一个问题, update  sy_user as a inner join sy_user_charge_log as b on b.username=a.username set recon ...

  9. HealthKit的使用

    一.项目中关联HealthKit框架 1.在Capabilities选项中打开HealthyKit选项 HealthKit关联路径 首先填写好你项目的Bundle Identifier并且选好Team ...

  10. List<Object> to JSONArray一

    package com.beijxing.TestMain; import java.util.ArrayList; import java.util.List; import com.beijxin ...