package com.neusoft.io;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileTest {
public static void main(String[] args) throws IOException {
File f0=new File("E:/Java/abc.txt");
File f1=new File("E:\\Java\\abc.txt");
File f2=new File("E:\\Java\\abc.txt");
File f3=new File("E:\\Java");
File f4=new File(f3,"abc.txt");
System.out.println("canWrite:"+f1.canWrite());//是否能写
if(!f1.exists())
{
f1.createNewFile();//添加,创建文件
}
//f1.delete();//删除
System.out.println("getAbsolutePath:"+f1.getAbsolutePath());
System.out.println("getPath:"+f1.getPath());
System.out.println("getParent:"+f1.getParent());
System.out.println("getName:"+f1.getName());
File f5=f1.getParentFile();
System.out.println(f5.getName());
System.out.println("isDirectory:"+f1.isDirectory());//是否是文件夹
System.out.println("isFile:"+f1.isFile());//是否是文件
System.out.println("isHidden:"+f1.isHidden());
System.out.println("lastModified:"+new Date(f1.lastModified()));//最后修改的时间
System.out.println("length:"+f1.length());
System.out.println();
System.out.println("===========================");
String[] strs=f3.list();
for(String item:strs)
{
System.out.println(item);
}
System.out.println();
System.out.println("======================");
File[] files=f3.listFiles();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(File file:files)
{
if(file.isDirectory()==true)
{
System.out.println("Dir:"+file.getName());
}
else {
System.out.println("File:"+file.getName()+"\t"+
sdf.format(new Date(file.lastModified()))+"\t"+file.length());
}
}
System.out.println();
System.out.println("==========");
File[] dirs=File.listRoots();//文件系统跟
for(File dir:dirs)
{
System.out.println(dir.toString());
}
System.out.println();
System.out.println("=============");
File f6=new File("E:\\Java\\aaa\\bbb");
if(!f6.exists())
{
//f6.mkdir();//创建单层文件夹
f6.mkdirs();//创建多层文件夹
}
System.out.println("===============");
File f7=new File("E:\\Java\\ddd.txt");
//File f7=new File("D:\\bbb.txt");
f1.renameTo(f7);//重命名
f1.setReadOnly();//设置为只读
}
}
package com.neusoft.io;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Nio2Test {
public static void main(String[] args) {
Path path=Paths.get("E:\\Java\\abc.txt");
System.out.println(path.getNameCount()+"\t"
+path.getFileName()+"\t"
+path.getParent()+"\t"
+path.getRoot());
try {
Files.copy(Paths.get("E:\\Java\\ddd.txt"),Paths.get("d:\\dd.txt") );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.neusoft.io;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class Nio2WatchServiceTest {
public static void main(String[] args) {
Path path=Paths.get("E:\\Java");
try {
//创建监视器对象
WatchService ws=FileSystems.getDefault().newWatchService();
//注册监视器对象到path目录,以及注册事件
path.register(ws, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
while(true)
{
WatchKey key=ws.take();
for(WatchEvent<?> event:key.pollEvents())
{
System.out.println(event.context().toString()+",事件类型"
+event.kind());
if(key.reset())
{
return;
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}