2.1 File的用法
马 克-to-win:sun公司设计File类,本身不能用来读数据或写数据。(要想读写数据,必须和其它io流的类配合使用,比如 FileInputStream等)File类的功能就是对磁盘上的文件或目录做一些非读写方面的工作,比如看看文件在哪个目录,哪天创建的,创建个新空 文件等。
例:2.1.1
import java.io.*;
public class TestMark_to_win {
public static void main(String args[]) throws Exception {
/*File(String parent, String child) Creates a new File instance from a
parent pathname string and a child pathname string.the parent pathname is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file. */
File f1 = new File("c:\\tmp", "tmp\\1.txt");
//File f1=new File("1.txt");
/*getName就是lastname*/
System.out.println("getName is " + f1.getName());
/* getPath Returns: The string form of this abstract pathname。即原样返回。 */
System.out.println("getPath is" + f1.getPath());
/* getAbsolutePath: Returns the absolute pathname string of this
abstract pathname. If this abstract pathname is already absolute,
then the pathname string is simply returned as if by the getPath()
method.if you use File f1=new File("1.txt"); you can see the
difference.结果会变成
getName is 1.txt
getPath is1.txt
Absolute Path is D:\eclipseJee\t1\1.txt
not */
System.out.println("Absolute Path is " + f1.getAbsolutePath());
System.out.println(f1.exists() ? "exist" : "not");
}
}
结果是:
getName is 1.txt
getPath isc:\tmp\tmp\1.txt
Absolute Path is c:\tmp\tmp\1.txt
not
例:2.1.2
import java.io.*;
public class TestMark_to_win {
public static void main(String[] args) {
File path = new File("c:/");
/*Returns an array of strings naming the files and directories .*/
String[] list = path.list();
for (int i = 0; i < list.length; i++)
System.out.println(list[i]);
}
}
更多请见:https://blog.csdn.net/qq_44639795/article/details/102568855
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有