做_操_件_文_Java
作_操_件_文_Java
/*******************************************************************************
* Copyright(c) 2005-2009 Huawei Tech. Co., Ltd.
* All rights reserved.
*
* Author: xbliuc
* Date : 2011-3-19
*******************************************************************************/
package com.rt.xbliuc.treeviewer;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Date;
/**
* 文件操作
*/
public class FileTest
{
/**
* TODO 添加方法注释
* @param args
* @throws IOException
*/
@SuppressWarnings("all")
public static void main(String[] args) throws IOException
{
//文件
//File file = new File("F:\\a.txt");
// System.out.println("文件是否存在: " + file.exists());
// System.out.println("文件是否可读: " + file.canRead());
// System.out.println("文件是否可写: " + file.canWrite());
// System.out.println("文件名: " + file.getName());
// System.out.println("获取文件或文件夹的根路径: " + file.getParent());
// System.out.println("文件路径: " + file.getPath());
// System.out.println("文件绝对路径: " + file.getAbsolutePath());
// System.out.println("判断是否是文件: " + file.isFile());
// System.out.println("判断是否是路径: " + file.isDirectory());
// System.out.println("文件最后一个修改的时间: " + file.lastModified());
// System.out.println("创建文件(如果文件存在就不创建)" + new File("F:\\b.txt"));
// System.out.println("获取文件或路径的正规路径:" + file.getCanonicalPath());
// System.out.println("删除当前文件: "+file.delete());
//文件夹
//File file2 = new File("F:\\FileTest");
// System.out.println("判断是否是路径: " + file2.isDirectory());
// System.out.println("返回路径下的所有文件名: " + file2.list());
// System.out.println("创建文件夹(父类不存在创建失败):"
// + new File("F:\\FileTest2").mkdir());
// System.out.println("创建文件夹(父类不存在,创建父目录):"
// + new File("F:\\b\\FileTest2").mkdirs());
//getFileDatile("F:\\FileTest");
//deleteFileDatile("F:\\FileTest");
fileFilter("F:\\FileTest");
//递归删除某个目录下的所有
public static void operationFile(File file) throws IOException
{
File files = new File("F:\\abc.doc");
if (!files.exists())//判断文件是否存在
{
files.createNewFile();
}
System.out.println(new File("F:\\aa\\bb\\cc").mkdirs());
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{
operationFile(files[i]);
}
else
{
files[i].delete();//删除文件
if (i == files.length - 1)
{
file.delete();//删除文件夹
}
}
}
}
//打印目录下的所有信息
public static void operationFile(File file) throws IOException
{
System.out.println("文件夹:" + file.getName());
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{
operationFile(files[i]);
}
else
{
System.out.println("文件:" + files[i].getName());
}
}
}
/**
* 打印目录下的所有信息
* @param filePath
*/
@SuppressWarnings("nls")
public static void getFileDatile(String filePath)
{
File file = new File(filePath);
if (file.isDirectory())
{
System.out.println("文件夹名:" + file.getName());
String[] list = file.list();
for (String string : list)
{
File files = new File(filePath + "\\" + string);
if (files.isDirectory())
{
getFileDatile(filePath + "\\" + string);
}
else
{
System.out.println("文件名: " + string + " | 是否可读: "
+ files.canRead() + " | 最后修改时间: "
+ new Date(file.lastModified()));
}
}
}
}
/**
* TODO 添加方法注释
* @param filePath
*/
@SuppressWarnings("all")
public static void deleteFileDatile(String filePath)
{
File file = new File(filePath);
if (file.list().length == 0)
{
file.delete();
}
if (file.isDirectory())
{
String[] list = file.list();
for (String string : list)
{
File files = new File(filePath + "\\" + string);
if (files.isDirectory())
{
deleteFileDatile(filePath + "\\" + string);
}
else
{
files.delete();
}
}
}
}
/**
* 文件过滤器
* @param filePath
*/
public static void fileFilter(String filePath)
{
File file = new File(filePath);
FilenameFilter filter = new FilenameFilter()
{
@Override
public boolean accept(File dir, String name)
{
File files = new File(dir, name);
if (files.isFile() && name.indexOf(".txt") != -1) //$NON-NLS-1$
{
return true;
}
return false;
}
};
String[] str = file.list(filter);
for (String string : str)
{
System.out.println(string);
}
}
}
//创建文件
public static void createFile()
{
File file = new File("F:/", "lxb.txt"); //创建文件对象
if (file.exists())
{
file.delete();
System.out.println("文件已删除");
}
else
{
try
{
FileOutputStream out = new FileOutputStream(file);//创建FileOutputStream对象
byte buy[] = "我有一只小猫".getBytes(); //创建byte型数组
out.write(buy); //将数组信息写入到文件中
out.close(); //关闭流
}
catch (Exception e1)
{
e1.printStackTrace();
}
try
{
FileInputStream in = new FileInputStream(file);//创建FileInputStream类对象
byte byt[] = new byte[1024];//创建byt数组
int len = in.read(byt);//从文件中读取数据
System.out.println("文件中的信息是:" + new String(byt, 0, len));//打印
in.close();//关闭流
}
catch (Exception e)
{
e.printStackTrace();
}
}
// try
// {
// file.createNewFile();
// String name = file.getName(); //获取文件名
// Long length = file.length(); //获取文件长度
// boolean hidden = file.isHidden(); //判断是否隐藏
// System.out.println("文件已创建");
// System.out.println("文件名称" + name);
// System.out.println("文件长度" + length);
// System.out.println("文件是否隐藏" + hidden);
// }
// catch (IOException e)
// {
// e.printStackTrace();
// }
// //}
// }
//
// //打印本地Ip
// public void aaaaa()
// {
// //System.out.println(this.username);
// InetAddress ip;
// try
// {
// ip = InetAddress.getLocalHost();
// String localname = ip.getHostName();
// String locaip = ip.getHostAddress();
// System.out.println(localname);
// System.out.println(locaip);
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
}
/*******************************************************************************
* Copyright(c) 2005-2009 Huawei Tech. Co., Ltd.
* All rights reserved.
*
* Author: xbliuc
* Date : 2011-3-19
*******************************************************************************/
package com.rt.xbliuc.treeviewer;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Date;
/**
* 文件操作
*/
public class FileTest
{
/**
* TODO 添加方法注释
* @param args
* @throws IOException
*/
@SuppressWarnings("all")
public static void main(String[] args) throws IOException
{
//文件
//File file = new File("F:\\a.txt");
// System.out.println("文件是否存在: " + file.exists());
// System.out.println("文件是否可读: " + file.canRead());
// System.out.println("文件是否可写: " + file.canWrite());
// System.out.println("文件名: " + file.getName());
// System.out.println("获取文件或文件夹的根路径: " + file.getParent());
// System.out.println("文件路径: " + file.getPath());
// System.out.println("文件绝对路径: " + file.getAbsolutePath());
// System.out.println("判断是否是文件: " + file.isFile());
// System.out.println("判断是否是路径: " + file.isDirectory());
// System.out.println("文件最后一个修改的时间: " + file.lastModified());
// System.out.println("创建文件(如果文件存在就不创建)" + new File("F:\\b.txt"));
// System.out.println("获取文件或路径的正规路径:" + file.getCanonicalPath());
// System.out.println("删除当前文件: "+file.delete());
//文件夹
//File file2 = new File("F:\\FileTest");
// System.out.println("判断是否是路径: " + file2.isDirectory());
// System.out.println("返回路径下的所有文件名: " + file2.list());
// System.out.println("创建文件夹(父类不存在创建失败):"
// + new File("F:\\FileTest2").mkdir());
// System.out.println("创建文件夹(父类不存在,创建父目录):"
// + new File("F:\\b\\FileTest2").mkdirs());
//getFileDatile("F:\\FileTest");
//deleteFileDatile("F:\\FileTest");
fileFilter("F:\\FileTest");
//递归删除某个目录下的所有
public static void operationFile(File file) throws IOException
{
File files = new File("F:\\abc.doc");
if (!files.exists())//判断文件是否存在
{
files.createNewFile();
}
System.out.println(new File("F:\\aa\\bb\\cc").mkdirs());
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{
operationFile(files[i]);
}
else
{
files[i].delete();//删除文件
if (i == files.length - 1)
{
file.delete();//删除文件夹
}
}
}
}
//打印目录下的所有信息
public static void operationFile(File file) throws IOException
{
System.out.println("文件夹:" + file.getName());
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{
operationFile(files[i]);
}
else
{
System.out.println("文件:" + files[i].getName());
}
}
}
/**
* 打印目录下的所有信息
* @param filePath
*/
@SuppressWarnings("nls")
public static void getFileDatile(String filePath)
{
File file = new File(filePath);
if (file.isDirectory())
{
System.out.println("文件夹名:" + file.getName());
String[] list = file.list();
for (String string : list)
{
File files = new File(filePath + "\\" + string);
if (files.isDirectory())
{
getFileDatile(filePath + "\\" + string);
}
else
{
System.out.println("文件名: " + string + " | 是否可读: "
+ files.canRead() + " | 最后修改时间: "
+ new Date(file.lastModified()));
}
}
}
}
/**
* TODO 添加方法注释
* @param filePath
*/
@SuppressWarnings("all")
public static void deleteFileDatile(String filePath)
{
File file = new File(filePath);
if (file.list().length == 0)
{
file.delete();
}
if (file.isDirectory())
{
String[] list = file.list();
for (String string : list)
{
File files = new File(filePath + "\\" + string);
if (files.isDirectory())
{
deleteFileDatile(filePath + "\\" + string);
}
else
{
files.delete();
}
}
}
}
/**
* 文件过滤器
* @param filePath
*/
public static void fileFilter(String filePath)
{
File file = new File(filePath);
FilenameFilter filter = new FilenameFilter()
{
@Override
public boolean accept(File dir, String name)
{
File files = new File(dir, name);
if (files.isFile() && name.indexOf(".txt") != -1) //$NON-NLS-1$
{
return true;
}
return false;
}
};
String[] str = file.list(filter);
for (String string : str)
{
System.out.println(string);
}
}
}
//创建文件
public static void createFile()
{
File file = new File("F:/", "lxb.txt"); //创建文件对象
if (file.exists())
{
file.delete();
System.out.println("文件已删除");
}
else
{
try
{
FileOutputStream out = new FileOutputStream(file);//创建FileOutputStream对象
byte buy[] = "我有一只小猫".getBytes(); //创建byte型数组
out.write(buy); //将数组信息写入到文件中
out.close(); //关闭流
}
catch (Exception e1)
{
e1.printStackTrace();
}
try
{
FileInputStream in = new FileInputStream(file);//创建FileInputStream类对象
byte byt[] = new byte[1024];//创建byt数组
int len = in.read(byt);//从文件中读取数据
System.out.println("文件中的信息是:" + new String(byt, 0, len));//打印
in.close();//关闭流
}
catch (Exception e)
{
e.printStackTrace();
}
}
// try
// {
// file.createNewFile();
// String name = file.getName(); //获取文件名
// Long length = file.length(); //获取文件长度
// boolean hidden = file.isHidden(); //判断是否隐藏
// System.out.println("文件已创建");
// System.out.println("文件名称" + name);
// System.out.println("文件长度" + length);
// System.out.println("文件是否隐藏" + hidden);
// }
// catch (IOException e)
// {
// e.printStackTrace();
// }
// //}
// }
//
// //打印本地Ip
// public void aaaaa()
// {
// //System.out.println(this.username);
// InetAddress ip;
// try
// {
// ip = InetAddress.getLocalHost();
// String localname = ip.getHostName();
// String locaip = ip.getHostAddress();
// System.out.println(localname);
// System.out.println(locaip);
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
}