- 浏览: 335212 次
- 性别:
- 来自: 重庆
文章分类
最新评论
-
hjl0722:
...
Java中的异或 -
lucd:
f(New.<Person, List<Pet&g ...
第15章泛型 -
liujunhao225:
[Error: could not access: List; ...
mvel的使用 -
superscorpio:
public void testImportInContex ...
mvel的使用 -
yuyangtina:
哦,知道了,是继承的方法。谢谢你的分享。
HttpClient3.x发送Soap请求的方法
一、File类的一些常用方法:
1.createNewFile
public boolean createNewFile()
throws IOException
Atomically creates a new, empty file named by this abstract pathname if and only if a file with
this name does not yet exist. The check for the existence of the file and the creation of the
file if it does not exist are a single operation that is atomic with respect to all other
filesystem activities that might affect the file.
Note: this method should not be used for file-locking, as the resulting protocol cannot be made
to work reliably. The FileLock facility should be used instead.
Returns:
true if the named file does not exist and was successfully created; false if the named file
already exists
Throws:
IOException - If an I/O error occurred
SecurityException - If a security manager exists and its SecurityManager.checkWrite
(java.lang.String) method denies write access to the file
Since:
1.2
2.delete
public boolean delete()
Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a
directory, then the directory must be empty in order to be deleted.
Returns:
true if and only if the file or directory is successfully deleted; false otherwise
3.renameTo
public boolean renameTo(File dest)
Renames the file denoted by this abstract pathname.
Many aspects of the behavior of this method are inherently platform-dependent: The rename
operation might not be able to move a file from one filesystem to another, it might not be
atomic, and it might not succeed if a file with the destination abstract pathname already
exists. The return value should always be checked to make sure that the rename operation was
successful.
Parameters:
dest - The new abstract pathname for the named file
Returns:
true if and only if the renaming succeeded; false otherwise
这个方法可以用来移动文件夹的同时,修改文件夹的名称(同样适用于文件)
二、几个容易混淆的方法
(1)getPath()与getAbsolutePath()的区别
public static void test1(){
File file1 = new File(".\\test1.txt");
File file2 = new File("D:\\workspace\\test\\test1.txt");
System.out.println("-----默认相对路径:取得路径不同------");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println("-----默认绝对路径:取得路径相同------");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
}
得到的结果:
-----默认相对路径:取得路径不同------
.\test1.txt
D:\workspace\test\.\test1.txt
-----默认绝对路径:取得路径相同------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
因为getPath()得到的是构造file的时候的路径。
getAbsolutePath()得到的是全路径
如果构造的时候就是全路径那直接返回全路径
如果构造的时候试相对路径,返回当前目录的路径+构造file时候的路径
(2).getAbsolutePath()和getCanonicalPath()的不同
public static void test2() throws Exception{
File file = new File("..\\src\\test1.txt");
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
}
得到的结果
D:\workspace\test\..\src\test1.txt
D:\workspace\src\test1.txt
可以看到CanonicalPath不但是全路径,而且把..或者.这样的符号解析出来。
(3).
public static void test3() throws Exception{
File file = new File("D:\\Text.txt");
System.out.println(file.getCanonicalPath());
}
确定你的系统是Windows系统。
确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:
D:\Text.txt
注意这里试大写的Text.txt
在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果
D:\text.txt
同样的代码得到不同的结果。
同时可以对比getAbsolutePath()看看,这个得到的结果是一样的。
原因:
window是大小写不敏感的,也就是说在windows上test.txt和Test.txt是一个文件,所以在windows上当文件不
存在时,得到的路径就是按照输入的路径。但当文件存在时,就会按照实际的情况来显示。这也就是建立文件
后和删除文件后会有不同的原因。文件夹和文件类似。
发表评论
-
java发邮件
2013-05-07 16:55 854发邮件需要用到mail.jar包 import ja ... -
PropertyChangeSupport的使用
2012-12-20 14:06 2689import java.beans.PropertyCh ... -
JVM运行时数据区的划分
2012-11-28 09:20 837虚拟机运行时数据区 ... -
威风威风
2012-11-20 11:26 10641. //a 不 ... -
Http基本认证
2012-10-29 15:20 973在HTTP中,基本认证是一种用来允许Web浏 ... -
如何获取美国时间
2012-10-26 10:41 2560TimeZone tz=TimeZone.getT ... -
base64加密算法简介
2012-10-23 11:32 1049什么是base64呢? 它是一种加密 ... -
比较器comparator
2012-10-11 10:46 778排序的规律跟方法的参数顺序有关。 该接口有个方法:in ... -
文件输入输出时的编码问题
2012-10-09 16:38 1038Java读取文件的 ... -
java源文件编码问题
2012-10-09 16:00 4635Java编译器在对 ... -
在java中利用ant对目录进行压缩
2012-08-31 17:05 886import java.io.File; impo ... -
文件读写例子
2012-07-07 10:19 965import java.io.BufferedReade ... -
字符串详解
2012-06-26 15:51 853一、字符串的六道经典题 看明白了,基本上就掌握了字符串的原理 ... -
私有成员的访问
2012-06-21 09:50 818不通过getXXX()方法获取类的私有域: public c ... -
jar文件
2012-05-31 23:41 917JAR(Java Archive,Java 归档文件) ... -
mvel的使用
2012-05-23 16:03 10535MVEL is a powerful expressi ... -
NotePad++的格式编码
2012-04-25 10:01 11954notepad默认存储文件的格式是ansi格式,如果想修改,可 ... -
字符编码概述
2012-04-24 23:59 763ASCII码 我们知道,在计 ... -
http协议的消息头
2012-04-18 17:25 2633一、HTTP消息头主要分为 ... -
HTTP协议详解
2012-04-18 17:17 936一、简介 1.http默认端口号为80,Https的端 ...
相关推荐
被封装在一起的类有以下几个: Compare------------------数据比较类(如比较时间大小) Cookie-------------------操作Cookie的类 DataBase-----------------数据操作的类 Encrypt------------------...
也许每一个站点都可能会对上传文件有许多的限制,这些限制会包括 文件类型,文件大小,扩展名,以及上传目录的存在与否,上传文件的存在与否,目录的可写性,可读性,上传文件的改名及怎样把文件从缓存当中复制到你...
在b/s开发中经常用到的javaScript技术整理 一、验证类 1、数字验证内 1.1 整数 1.2 大于0的整数 (用于传来的ID的验证) 1.3 负整数的验证 1.4 整数不能大于iMax 1.5 整数不能小于iMin 2、时间类 ...
相关信息:CFileDialog 用于取文件名的几个成员函数: 假如选择的文件是C:\WINDOWS\TEST.EXE 则(1)GetPathName();取文件名全称,包括完整路径。取回C:\WINDOWS\TEST.EXE (2)GetFileTitle();取文件全名:TEST.EXE (3)...
文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...
这不当前手下的项目就用到了这个,于是总结了几个循环创建目录的方法。 方法一:使用glob循环 <?php //方法一:使用glob循环 function myscandir1($path, &$arr) { foreach (glob($path) as $file) { if (is...
184、Servlet执行时一般实现哪几个方法? 44 185、getServletContext()和getServletConfig()的意思 44 186、Hashtable和HashMap 44 187、JAVA SERVLET API中forward() 与redirect()的区别? 44 189、Can a Java ...
184、Servlet执行时一般实现哪几个方法? 44 185、getServletContext()和getServletConfig()的意思 44 186、Hashtable和HashMap 44 187、JAVA SERVLET API中forward() 与redirect()的区别? 44 189、Can a Java ...
不完全统计,C语言标准库中的头文件有15个之多,所以我主要介绍常用的这四个头文件stdio.h,string.h,math.h,stdlib.h,以后用到其他的再做补充。下面上干货: 1.<stdio>:定义了输入输出函数、类型以及宏,函数几乎...
创建了几个String Object? 两个 28、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。 以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题。 public class ThreadTest1{ ...
创建了几个String Object? 答:两个对象,一个是“xyx”,一个是指向“xyx”的引用对象s。 38.abstract class和interface有什么区别? 答: 声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于...
创建了几个String Object? 两个 31、EJB包括(SessionBean,EntityBean)说出他们的生命周期,及如何管理事务的? SessionBean: Stateless Session Bean 的生命周期是由容器决定的,当客户机发出请求要建立一个...
Linux 操作系统基础教程 清华大学信息学院计算机系 ...从网上下载的,但是我不推荐易用这种方法得到 Linux,因为仅仅核心就有几十个 Mbit 的 数据量,而一个完整的发行版本大概都是 1Gbit 左右的数据量...
对话框中我们常用了以下几种: 1、文件对话框(FileDialog) 它又常用到两个: 打开文件对话框(OpenFileDialog) 保存文件对话(SaveFileDialog)...在介绍的过程中我用到了一个自己开发的类:File,主要是文件操作的……
C# winform对话框用法大全 收藏 对话框中我们常用了以下几种: 1、文件对话框(FileDialog) 它又常用到两个: 打开文件对话框(OpenFileDialog) ...在介绍的过程中我用到了一个自己开发的类:File,主要是文件操作的
如前所述,我是一个狮子座男人,一度我认为学习Java会使我看起来与众不同,可是几个月以后我放弃了这个选择,我看了论坛里关于这两种语言孰优孰劣的讨论,最终选择了C#,请不要问我为何做出这样的选择,很多人认为...
要求3:另外,请为Bank类添加几个统计方法 1.统计银行所有账户余额总数 2.统计所有信用账户透支额度总数 要求4:编写测试类 写个测试类,测试以上代码能否正常工作。 要求5:覆盖toString方法 查看对象的内容。 ...
答:多形:一个类中多个同名方法。继承:子类继承父类。 jsp内置对象? 答:request 用户端请求 response 回应 pageContext 网页属性 session 会话 out 输出 page 当前网页 exception 错误网页 application ...
MySql的用户管理是通过User表来实现的,添加新用户常用的方法有两个,一是在User表插入相应的数据行,同时设置相应的权限;二是通过GRANT命令创建具有某种权限的用户。其中GRANT的常用用法如下: grant all on mydb...