File类

IO流(Input Output) :
IO技术主要的作用是解决设备与设备之间 的数据传输问题。 比如: 硬盘--->内存          内存的数据---->硬盘上            把键盘的数据------->内存中
IO技术的应用场景:
导出报表 , 上传大头照   、 下载 、 解释xml文件 ... 

数据保存到硬盘上,该数据就可以做到永久性的保存。   数据一般是以文件的形式保存到硬盘上

sun使用了一个File类描述了文件或者文件夹的。
File类可以描述一个文件或者一个文件夹。

File类的构造方法:
File(String pathname)  指定文件或者文件夹的路径创建一个File对象。
File(File parent, String child)   根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。 
此构造函数用于操作同一路径下的多个文件,就不用重复指定父路径了
File(String parent, String child) 

目录分隔符:  在windows机器上 的目录分隔符是 \  ,在linux机器上的目录分隔符是/ .

注意:  在windows上面\ 与 / 都可以使用作为目录分隔符。 而且,如果写/ 的时候只需要写一个即可。

public class Demo1 {
	
	public static void main(String[] args) {
		//File file = new File("F:"+File.separator+"a.txt"); //  在linux机器上是不合法路径
		File file = new File("F:/a.txt");  
		//Windows操作系统认两种分隔符:\\和/都可以,所以在Windows上可以不用通过separator获取分隔符,直接使用/即可
		
		/*File parentFile = new File("F:\\");
		File file = new File("F:\\","a.txt");*/
		System.out.println("存在吗? "+ file.exists());  // exists 判断该文件是否存在,存在返回true,否则返回false。
//		System.out.println("目录分隔符:"+ File.separator);	separator返回一个与系统相关的目录分隔符  \
	}
//	File(File parent, String child)
	public static void test(String child){
		File parentFile = new File("");
		File file = new File(parentFile,child);
		
	}
	
	
	
	
}

路径问题: 
绝对路径: 该文件在硬盘上 的完整路径。绝对路径一般都是以盘符开头的。
相对路径:  相对路径就是资源文件相对于当前程序所在的路径。

. 当前路径
 .. 上一级路径
 
注意: 如果程序当前所在的路径与资源文件不是在同一个盘下面,是没法写相对路径 的。

public class Demo2 {

	public static void main(String[] args) {
		File file = new  File(".");
		System.out.println("当前路径是:"+ file.getAbsolutePath());
		//获取当前路径的绝对路径
		
		File file2 = new File("..\\..\\a.txt");
		System.out.println("存在吗?"+ file2.exists());
		
	}
	
}


你可能感兴趣的:(JavaSE)