您的位置:首页 > 编程语言 > Java开发

java IO流学习总结

2016-12-12 10:05 225 查看
类名不能与关键字相同

否则函数 或者 方法不能识别


1.新建java project

2.新建包

3.新建类

(名字都不能是关键字)

4.构造一个方法,用来读取文件

public void optFile(String rootPath,String fileName)throws Exception{
//创建文件对象
File dir =new File(rootPath);
//判断是否存在
if(!dir.exists()){
dir.mkdir();    //mkdir创建单层目录;mkdirs 创建多层目录
}else{
//判断指定的文件是否为目录
if(!dir.isDirectory()){
System.out.println(rootPath+"不是文件目录");
return;
}
}

//创建文件对象
File f1= new File(dir,fileName);
//判断文件是否存在
if(!f1.exists()){
f1.createNewFile();//不存在创建新文件
}
//文件若存在

//获取文件大小
System.out.println("文件大小"+f1.length());
//获取文件路径
System.out.println("文件路径"+f1.getPath());
//获取文件绝对路径
System.out.println("文件绝对路径"+f1.getAbsolutePath());
}


在main方法中调用此方法

public static void main(String[] args) {
String rootPath="D:\\demo";
String fileName="test1.txt";
try {
new FileDemo().optFile(rootPath,fileName); //调用类FileDemo的函数
} catch (Exception e) {
e.printStackTrace();
}
}


运行程序 得到结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: