javaSE_文件,javase文件
javaSE_文件,javase文件
1. 文件:
File file = new File("d:/temp/newfile.txt");
try {
// 创建文件
boolean bcreate = file.createNewFile();
System.out.println("创建文件成功?"+bcreate);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件是否存在?" + file.exists());
System.out.println("是否为文件?" + file.isFile());
System.out.println("是否为文件夹?" + file.isDirectory());
System.out.println("是否为隐藏文件 ?" + file.isHidden());
System.out.println("是否为绝对路径?" + file.isAbsolute());
System.out.println("是否可读?" + file.canRead());// true
System.out.println("是否可写?" + file.canWrite());// true
System.out.println("文件名:" + file.getName());// newfile.txt
System.out.println("路径名;" + file.getPath());// d:\temp\newfile.txt
System.out.println("父目录名:" + file.getParent());// d:\temp
// 文件属性
System.out.println("文件 大小:"+file.length());//1194
System.out.println("文件 大小:"+StringUtils.countFileSize(file.length()));//1.17KB
System.out.println("文件最后修改时间:"+file.lastModified());//1477927187842
System.out.println("文件最后修改时间:"+StringUtils.dateToString(
new Date(file.lastModified()), "yyyy/MM/dd HH:mm:ss"));//2016/10/31 23:19:47
boolean bDelete = file.delete();
System.out.println("删除文件:"+bDelete);
2. 目录:
File dir = new File("d:/temp/newdir/a");
if (!dir.exists()){
dir.mkdir();// 只能创建最后一层
dir.mkdirs();// 递归创建
}
System.out.println("文件夹最后修改时间:"+dir.lastModified());//1478018894440
System.out.println("文件夹最后修改时间:"+StringUtils.dateToString(
new Date(dir.lastModified()), "yyyy/MM/dd HH:mm:ss"));//2016/11/02 00:48:14
File dir2 = new File("d:/temp");
// 获取目录下所有文件
File[] files = dir2.listFiles(new FileFilter() {
public boolean accept(File fl) {
return !fl.isDirectory();
}
});
for (File file : files){
System.out.println(file.getName());
}
// 获取目录下所有文件名及目录名
String[] fileNames = dir2.list();
for (String fileName : fileNames){
System.out.println(fileName);
}
3. 对象序列化
前提是User类需要从Serializable类派生。
// 将对象序列化到文件
public static void objectToFile(){
try (
FileOutputStream fos = new FileOutputStream("d:/temp/object.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
){
User user = new User();
user.setAge(23);
user.setName("张三");
oos.writeObject(user);
} catch (Exception e) {
e.printStackTrace();
}
}
// 从文件反序列化到对象
public static void fileToObject(){
try (
FileInputStream fis = new FileInputStream("d:/temp/object.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
){
User user = (User) ois.readObject();
System.out.println(user.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
4.监听文件变化public static void watchFile() throws IOException, InterruptedException {
WatchService watchService = FileSystems.getDefault().newWatchService();
Paths.get("d:/").register(
watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE);
while (true) {
// 获取下一个文件变化时间
WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println(event.context() + "文件发生了" + event.kind() + "事件");
}
// 重设watchkey
boolean v = key.reset();
if (!v) {
break;
}
}
}
5. 复制文件
public static void copyFileForChannel(File src, File des) throws Exception {
int length = 2097152;
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(des);
FileChannel inC = in.getChannel();
FileChannel outC = out.getChannel();
ByteBuffer b = null;
while (true) {
if (inC.position() == inC.size()) {
inC.close();
outC.close();
return;
}
if ((inC.size() - inC.position()) < length) {
length = (int) (inC.size() - inC.position());
} else
length = 2097152;
b = ByteBuffer.allocateDirect(length);
inC.read(b);
b.flip();
outC.write(b);
outC.force(false);
}
}
6. 删除文件
public static boolean deleteFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
flag = file.delete();
return flag;
}
7. 锁定某一文件
FileChannel fc = null;
try {
File file = new File(filePath);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
fc = raf.getChannel();
FileLock fLock = fc.tryLock();
if (fLock != null && fLock.isValid()) {
// do
}
fLock.release();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fc != null) {
fc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
相关文章
- 暂无相关文章
用户点评