------- android培训、java培训、期待与您交流! ----------
File类:
对文件或者文件夹的描述类,方便对文件或者文件夹的属性信息进行操作。
class FileDemo{
public static void main(String[] args){
}
//第一种构造方式:创建File对象,可以将已有的和未出现的文件或者文件夹封装成对象。
File f = new File("a.txt");
//第二种构造方式:
File f1 = new File("c:\\abc","b.txt");//第一个参数为目录,第二个参数为文件名,比第一种灵活
//第三种构造方式:
File d = new File("c:"+File.separator+"abc");//创建目录File对象。File.separator为跨平台的分隔符。
File f3 = new File(d, "c.txt");//将目录File对象作为构造参数之一
}
常见方法:
1,创建。
boolean createNewFile();在指定位置创建文件。
boolean mkdir();创建文件夹
boolean mkdirs();创建多级文件夹。
2,删除。
boolean delete();删除失败返回false。
boolean deleteOnExit();在程序退出时删除文件。
3,判断。
boolean exists();//*最常用的方法,判断文件是否存在。
boolean isFile();//*判断是否是文件
boolean isDirectory();//*判断是否是文件夹
boolean isHidden();//由于java操作不了隐藏文件,所以要先判断一下是否隐藏,只操作非隐藏的文件
boolean isAbsolute;//是否是绝对路径
boolean canExecute();判断文件是否是可执行文件。
boolean canRead();//是否可读
boolean canWrite();//是否可写
int compareTo();//按字母比较路径名
4,获取信息。
getName();
getPath();
getParent();
getAbsolutePath();
lastModified();//最后一次修改时间
length();
class FileDemo{
public static void main(Stirng[] args){
method_1();
}
public static void method_1(){
File f = new File("file.txt");
sop("create:" + f.createNewFile());//如果文件已存在,创建失败,返回false。如果创建成功,返回true。
sop("delete:" + f.delete());//删除文件,如果删除成功,返回true,不成功返回false。
f.deleteOnExit();//当文件被流使用的时候,普通的删除方法是删不掉的,所以需要调用此方法,在程序退出时将文件删除。
}
public static void method_2(){
File f= new File("file.txt");
sop("execute:" + f.canExecute());//可执行返回true,不可执行返回false
sop("exists:" + f.exists());//判断文件是否存在。
//创建文件夹
File dir = new File("abc");
sop("mkdir+"+dir.mkdir());//mkdir()方法,只能创建一级目录,如果需要多级,则使用mkdirs();
File dirs = new File("sds\\gre\\bt\\cc\\hgh\\hh");
sop("mkdirs:" + dirs.mkdirs());//创建多级文件夹。
}
public static void method_3(){
File f = new File("file.txt");
//f.createNewFile();
f.mkdir();
//记住:在判断文件对象是否是文件或者目录时,必须要先判断该文件对象封装的内容是否存在,通过exists判断。
sop("dir:" + f.isDirectory());
sop("file:" + f.isFile());//判断是否是文件,如果不存在或者是目录,则返回false
sop(f.isAbsolute());//判断是否是绝对路径。
}
public static void method_4(){
File f = new File("c:\\a.txt");
sop("path:"+ f.get{Path());//对象中封装的是什么路径,就获取什么路径(相对、绝对路径)
sop("abspath:"+ f.getAbsolutePath());//无论封装的是相对还是绝对的,都是获取绝对路径
sop("parent:" + f.getParent());//该方法返回的是绝对路径中的父目录。如果获取的是相对路径,返回null
//如果相对路径中有上一层目录,那么该目录就是返回结果。
}
public static void method_5(){
File f1 = new File("c:\\Test.java");
File f2 = new File("d:\\haha.java");
sop("rename:" + f1.renameTo(f2));//修改文件的路径及名称(相当于剪切并改名);
}
}
File的其他方法:
static listRoots();//返回盘符列表(c:\、D:\、....)
list();//获得指定目录下所有文件名,包含隐藏文件。调用该方法必须是目录对象,而且必须存在。
listFiles;//与list()方法相似,返回的是File对象数组。
FilenameFilter接口:依据其accept方法返回值是否为真来过滤文件对象。返回为真则说明文件是符合条件的,返回假则说明不符合条件。
class FileDemo2{
public static void main(Sting[] args){
File dir = new File("d:\\");
String[] arr = dir.list(new FilenameFilter(){
public boolean accept(File dir, String name){
return name.endWith(".java");
}
});
for(String name : arr){
System.out.println(name);
}
}
}
Properties:是Hashtable的子类。
也就是说它具备map集合的特点,而且它里面存储的键值对都是字符串。
是集合中和IO技术想结合的集合容器。
该对象的特点:可以用于键值对形式的配置文件。
public static void main(String[] args){
setAndGet();
}
//设置和获取元素。
public static void setAndGet(){
Properties prop = new Properties();
prop.setProperty("zhangsan","30");
prop.setProperty("lisi","39");
String value = prop.getProperty("lisi");
System.out.println(value);
Set<String> names = prop.stringPropertyNames();//返回所有key的set集合。
}
//将文件里的配置数据读到集合中
public static void loadDemo() throws IOException{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("info.txt");
//将流中的数据加载进集合。
prop.load(fis);
prop.list(System.out);
prop.setProperty("wangwu","39");
FileOutputStream fos = new FileOutputStream("info.txt");
prop.store(fos,"haha");//haha为注释信息,生成文件会在开始加入注释信息和修改时间,前面会有#,加入#的为注释,将来不会被properties类加载。
}
*如果要操作xml表示的配置文件,需要用org.w3c.dom包中的Document对象提供的方法。不过比较麻烦。目前基本都是使用dom4j(dom for java)来解析。
打印流PrintWriter和PrintStream
该流提供了打印方法,可以将各种数据类型的数据都原样打印。
字节打印流,
PrintStream:
构造函数可以接受的参数类型:
1,file对象。
2,字符串路径。
3,字节输出流。OutputStream
字符打印流:
PrintWriter:
1,file对象。
2,字符串路径。
3,字节输出流。OutputStream
4,字符输出流。Writer
class PrintStreamDemo{
public static void main(String[] args){
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//PrintWriter out = new PrintWriter(System.out,true);//往控制台上打,第二个参数为是否自动刷新
PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);由于String作为参数的构造方法没有自动刷新,所以将路径封装到输出流中。
String line = null;
while((line=bufr.readLine())!=null){
out.println(line.toUpperCase());//打印出并加换行。
}
out.close();
bufr.close();
}
}
序列流SequenceInputStream
作用:将多个输入流对象拼成一个流对象
用途:当需要从多个源读取数据,输出到一个目的时。
例:将三个文件合并成一个文件。
class SequenceDemo{
public static void main(String[] args) throws IOException{
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream("d:\\1.txt"));
v.add(new FileInputStream("d:\\2.txt"));
v.add(new FileInputStream("d:\\3.txt"));
Enumeration<FileInputStream> en = v.elements();
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("d:\\4.txt");
byte[] buf = new byte[1024];
int len = 0;
while((len = sis.read(buf)) != -1){
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}
切割文件(等份切割):
class SpliFile{
public static void main(String[] args)throw IOException{
}
public static void splitFile(){
FileInputStream fis = new FileInputStream("d:\\1.bmp");
FileOutputStream fos = null;
byte[] buf = new Byte[1024*1024];//建立一个1MB的缓冲区
int len = 0;
int count = 1;
while((len=fis.read(buf))!=-1){
fos = FileOutputStream("d:\\splitFile\\"+count+".part");//每次循环将1MB数据
fos.write(buf.0.len);
fos.close();
}
if(fis != null){
fis.close();
}
}
}