码迷,mamicode.com
首页 > 编程语言 > 详细

java-IO学习

时间:2017-11-11 00:23:24      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:apach   xxxxx   file类   exit   tabs   exist   虚拟机   bsp   下载   

之前一直没去学过IO,现在要写上传下载,必须得看了。

File类

public class FileTest
{
public static void main(String[] args)
throws IOException
{
// 以当前路径来创建一个File对象
//新建一个file的对象,这个对象,代表的是一个路径,“.”是当前目录的相对路径。
//File类,使用文件路径字符串来创建File实例,该字符串可以是绝对路径,也可以是相对路径
File file = new File(".");
// 直接获取文件名,输出一点
System.out.println(file.getName());
// 获取相对路径的父路径可能出错,下面代码输出null
System.out.println(file.getParent());
// 获取绝对路径
//这里的路径是java虚拟机运行的所在地址,这里输出的是tomcat的bin目录地址
System.out.println(file.getAbsoluteFile());
// 获取上一级路径
System.out.println(file.getAbsoluteFile().getParent());
// 在当前路径下创建一个临时文件

//现在就真的创建了一个文件 aaaXXXXXXXXX.txt
//File tmpFile = File.createTempFile("aaa", ".txt", file);

}
}

 

 

File 的 exits()方法

 

File file = new File(".");
System.out.println(file.exists());// true   当前路径存在,返回true
 File file1=new File("test.txt");
System.out.println(file1.exists());//true 当前路径有test.txt文件,返回true
File file2=new File("test");
System.out.println(file2.exists());// false  当前路径没有test 文件,返回false
File file3=new File("content.txt");
System.out.println(file3.exists());//false  当前路径没有content.txt,返回false
File file4=new File("D:\\apache-tomcat-9.0.0.M26\\bin\\content.txt");
System.out.println(file4.exists());//true  该绝对路径有content.txt 返回true

java-IO学习

标签:apach   xxxxx   file类   exit   tabs   exist   虚拟机   bsp   下载   

原文地址:http://www.cnblogs.com/zhizhiyin/p/7816922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!