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

第一种:

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. java利用Aspose.cells.jar将本地excel文档转化成pdf(完美破解版 无水印 无中文乱码)

    下载aspose-cells-8.5.2.jar包 http://pan.baidu.com/s/1kUBzsQ7 JAVA代码 package webViewer; import java.io.* ...

  2. mybatis - resultMap

    resultMap有比较强大的自动映射,下面是摘自mybatis中文官网的的片段: 当自动映射查询结果时,MyBatis会获取sql返回的列名并在java类中查找相同名字的属性(忽略大小写). 这意味 ...

  3. java8中map的meger方法的使用

    java8中map有一个merge方法使用示例: /** * 打印出包含号码集的label的集合 * * @param args */ public static void main(String[] ...

  4. hive中拉链表

    在有些情况下,为了保持历史的一些状态,需要用拉链表来做,这样做目的在可以保留所有状态的情况下可以节省空间. 拉链表适用于以下几种情况吧 数据量有点大,表中某些字段有变化,但是呢变化的频率也不是很高,业 ...

  5. Mac PHP 环境配置+渗透环境

    /* 最近购入一台mac pro 配置自己的php环境以及渗透环境 留个笔记 */ Mac 自带的PHP 加 apache 所以就不需要install 咯.只需要down个mysql 就好了. 0x1 ...

  6. Winform button按钮设置快捷键

    1)Text属性命名时后面&加键名,如:文件(&F) 2)添加一个ContextMenuStrip控件,同时在Items里添加一个项,并为项设置快捷键,把Visible设置为false ...

  7. Linux守护进程

    什么是守护进程? 守护进程是生存期长的一种进程,它们常常在系统引导装入时启动,仅在系统关闭时在终止.它们没有控制终端并且在后台运行.Linux 系统中有很多守护进程用以执行系统的日常事物,而且服务器程 ...

  8. Scala

    简直就是java啊,mac上,下载,添加环境变量,source 就好了

  9. CMD打包文件,解压文件

    压缩%ProgramFiles%\Winrar\rar a c:\123.rar c:\123解压%ProgramFiles%Winrar\unrar.exe x c:\sp\sp.rar c:\sp

  10. Peterson算法

    #define FALSE 0 #define TRUE 1 #define N 2 /*进程数量 */ int turn; /* 现在轮到谁 */ int interested[N]; /*所有值初 ...