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中获取路径的几种方式的更多相关文章
- java中获取路径的几种基本的方法
package com.ygh.blog.realpath; import java.io.File; import java.io.IOException; import java.io.Input ...
- java项目获取路径的几种方式
第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); ...
- JAVA中获取路径
内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径: 以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.get ...
- Java中HashMap遍历的两种方式
Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...
- JAVA中集合输出的四种方式
在JAVA中Collection输出有四种方式,分别如下: 一) Iterator输出. 该方式适用于Collection的所有子类. public class Hello { public stat ...
- java中数组复制的两种方式
在java中数组复制有两种方式: 一:System.arraycopy(原数组,开始copy的下标,存放copy内容的数组,开始存放的下标,需要copy的长度); 这个方法需要先创建一个空的存放cop ...
- java动态获取WebService的两种方式(复杂参数类型)
java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...
- 干货 | Java中获取类名的3种方法!
获取类名的方法 Java 中获取类名的方式主要有以下三种. getName() 返回的是虚拟机里面的class的类名表现形式. getCanonicalName() 返回的是更容易理解的类名表示. g ...
- java中获取路径中的空格处理(%20)问题
在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...
随机推荐
- 9.openssl ca
用于签名证书请求.生成CRL.维护一个记录已颁发证书和这些证书状态的数据库. 证书请求私用CA的私钥签名之后就是证书. [root@xuexi tmp]# man ca SYNOPSIS openss ...
- JetBrains激活
https://www.imsxm.com/jetbrains-license-server/ 已经累计为大家激活1360577次 :) JetBrains授权服务器:http://idea.imsx ...
- Python中出现的异常
简单的写几种我知道的关于Python中出现的异常含义,希望大神批评指正,我只是学软件开发的菜鸟,前面的路还很长,我会努力学习! 什么是异常? 异常既是一个事件,该事件会在程序执行过程中发生,影响了程序 ...
- HTML DOM Element
HTML DOM Element(元素) 文档本身是文档节点 . 所有 HTML 元素是元素节点 . 所有 HTML 属性是属性节点 HTML 元素内的文本是文本节点 . 注释是注释节点 . NODE ...
- 使用hex6x 进行十六进制转换
接触DSP两年多,虽然烧写Flash的操作都没问题,但是要是问起来为什么这么做的,就有点自惭形秽了.所以花些时间,查阅一下资料,整理一下. 1.先看看BurnFlash都需要什么东西. XXX.out ...
- javascript [object,Object]
今天给html标签的属性赋值为对象时,发现取出来的值为 [object,Object],感觉有点奇怪. 代码如下: <!DOCTYPE html> <html> <hea ...
- liToSpan
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Java中public,private,protected,和默认的区别
Java中public,private,protected,和默认的区别 1.private修饰词,表示成员是私有的,只有自身可以访问: 2.protected,表示受保护权限,体现在继承,即子类可以 ...
- MVC NPOI Linq导出Excel通用类
之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...
- Git学习(三)——暂存区、远程仓库、增删改管理
一.工作区和暂存区 工作区(Working Directory) 就是在你的电脑里能看到的目录 版本库(Repository) 工作区中的一个隐藏目录.git,这个不算工作区,而是Git版本库.Git ...