博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
File类
阅读量:2395 次
发布时间:2019-05-10

本文共 1366 字,大约阅读时间需要 4 分钟。

流只能操作数据,而要操作被封装成对象的文件数据必须使用file类,例如文件的属性之类。

/*流只能操作数据,而要操作被封装成对象的文件数据必须使用file类,例如文件的属性之类。*/import java.io.*;public class anli{	public static void main (String[] args)throws IOException	{		File file = new File ("demo.txt");//File是可以指定父目录和子文件名字的		file.createNewFile();  //如果创建成功就返回true,如果失败就返回false								//separator是标准分隔符。。。		//file.delete();         删除文件...		//file.createTempFile("demo","txt");		//file.deleteOnExit();   虚拟机退出时自动删除指定的文件和目录,防止执行的过程中跑出异常,导致文件没被删除的现象		sop(file.canExecute());  //检查是否可以执行该文件。。。		sop(file.exists());     //检查是否存在该文件		File f = new File ("aa");		sop(f.mkdir());      //创建目录,最多只能创建一级子目录。。。如果要创建多级子目录就是使用mkdir()		sop(f.isDirectory());  //判断是不是目录		sop(f.isFile());     //判断是不是文件		sop(f.isAbsolute()); //判断是不是绝对路径,并不需要说文件真的存在,只要输入的文件名的方式		sop(f.getPath());    //返回目录或者文件的名字		sop(f.getAbsolutePath());  //返回绝对路径		sop(f.getParent());    //只有File里面是绝对路径的才能返回父目录,相对路径的就返回null		File fi = new File ("demo");		sop(f.renameTo(fi));   //参数是File,重新改变名字。。。把f的名字改成fi的名字。。。		File[] files = File.listRoots();  //列出可用的文件系统根,也就是可用的盘符。。。		for (File fil : files)		{			sop(fil);		}		File file2 = new File ("c:\\");		String[] names = file2.list();  //返回一个字符串数组,返回的是文件夹里面所有的文件名和目录名,其中包含隐藏的文件和目录。。。		for(String name2 : names)		{			sop(name2);		}	}	public static void sop (Object obj)	{		System.out.println(obj);	}}

转载于:https://my.oschina.net/u/2356176/blog/466605

你可能感兴趣的文章
IDL vector filed plot
查看>>
piecewise constant function 阶跃常函数
查看>>
IDL save postscript file
查看>>
Bibtex如何使authors in the citation 最多显示两个
查看>>
Bibtex 如何cite 不同格式
查看>>
Cmake environmental variables: how to make find_package, find_path and find_library work
查看>>
Cmake space in path windows
查看>>
Differences between Tesla and a GeForce Series GPU
查看>>
Faster Parallel Reductions on Kepler
查看>>
NVIDIA Tesla C2075 vs Tesla K10 theoretical performance
查看>>
Fast floor/ceiling functions C
查看>>
Continue Long Statements on Multiple Lines Matlab
查看>>
What does “warning: not all control paths return a value” mean? (C++)
查看>>
C++ 运算符优先级
查看>>
Savitzky-Golay smoothing
查看>>
IDL get variable size in bytes
查看>>
high-frequency emphasis filter matlab
查看>>
cat -n
查看>>
使用 ftrace 调试 Linux 内核,第 2 部分
查看>>
使用 ftrace 调试 Linux 内核,第 3 部分
查看>>