黑马程序员_IO流(四)File文件操作

------- android培训java培训、期待与您交流! ----------

基本操作:创建、删除、判断、获取信息

1、创建:
boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false。
和输出流不同,输出流对象一建立就会创建文件,而且文件已经存在会覆盖。
boolean mkdir():创建此抽象路径名指定的目录。
boolean mkdirs():创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
2、删除:
boolean delete():删除失败返回false。
void deleteOnExit():在程序退出时删除指定文件。
3、判断:
boolean canExecute():测试应用程序是否可以执行此抽象路径名表示的文件。
int compareTo(File pathname):按字母顺序比较两个抽象路径名。
boolean exists():测试此抽象路径名表示的文件或目录是否存在。
boolean isDirectory():测试此抽象路径名表示的文件是否是一个目录。先判断文件或目录是否存在。
boolean isFile():测试此抽象路径名表示的文件是否是一个标准文件。先判断文件或目录是否存在。
boolean isHidden():测试此抽象路径名指定的文件是否是一个隐藏文件。
4、获取信息:
getName();
getPath();
getParent();
getAbsolutePath();
lastModified();
length();

import java.io.*;
class  FileDemo
{
	public static void main(String[] args)  throws IOException
	{
		method_3();
	}
	//创建File对象
	public static void consMethod()
	{
		//将a.txt封装成File对象,可以是已有的和未出现的文件或文件夹。
		File f1 = new File("a.txt");
		//路径和文件名分别传入
		File f2 = new File("F:\\abc","b.txt");

		File d = new File("F:\\abc");
		File f3 = new File(d,"c.txt");
		//跨平台目录分隔符:File.separator
	}
	//创建和删除文件
	public static void method_1() throws IOException
	{
		File f = new File("file.txt");
		//虚拟机退出时删除此文件
		f.deleteOnExit();
		//可以创建文件返回true,不可创建返回false
		System.out.println("创建:"+f.createNewFile());
		//System.out.println("删除:"+f.delete());
	}
	public static void method_2() throws IOException
	{
		File f = new File("file.txt");
		//创建文件夹
		File dir = new File("abc");
		System.out.println("创建:"+dir.mkdir());
	}
	public static void method_3() throws IOException
	{
		File f = new File("a.txt");
		System.out.println("path:"+f.getPath());
		System.out.println("abspath:"+f.getAbsolutePath());
		System.out.println("parent:"+f.getParent());//该方法返回的是绝对路径中的父目录,如果获取的是相对路径返回null,或者相对路径的文件父目录。
		System.out.println("last:"+f.lastModified());
	}
	//重命名
	public static void method_4() throws IOException
	{
		File f1 = new File("c:\\1.txt");
		File f2 = new File("c:\\2.txt");
		System.out.println(f1.renameTo(f2));
	}
}

删除一个带内容的目录

原理:
在Windows中,删除目录从里面往外删除的。
那么要用到递归。

import java.io.*;
class  RemoveDir
{
	public static void main(String[] args) 
	{
		File dir = new File("d:\\testdir");
		removeDir(dir);
	}

	public static void removeDir(File dir)
	{
		File[] files = dir.listFiles();
		for (int x = 0;x<files.length ;x++ )
		{
			if (files[x].isDirectory())
			{
				removeDir(files[x]);
			}
			else
			{
				System.out.println(files[x].toString()+"-file-"+files[x].delete());
			}
		}
		System.out.println(dir+"::dir::"+dir.delete());
	}
}

Properties 是hashtable的子类。

也就是说它具备Map集合的特点,而且它里面存储的键值对都是字符串。
是集合和IO技术相结合的集合容器。
该对象的特点:可以用于键值对形式的配置文件。
在加载数据时,需要数据有固定格式:键=值。

import java.io.*;
import java.util.*;
class  PropertiesDemo
{
	public static void main(String[] args) 
	{
		method_1();
	}
	//设置和获取元素
	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();
		for (String s : names )
		{
			System.out.println(s+" : "+prop.getProperty(s));
		}

	}
	/*
	演示将流中的数据存入集合
	1、用一个流和info.txt文件关联;
	2、读取一行数据,将该行数据用“=”进行切割;
	3、等号左边作为键,右边作为值。存入到Properties集合中即可。
	*/
	public static void method_1() throws IOException
	{
		BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
		String line = null;
		Properties prop = new Properties();
		while ((line=bufr.readLine())!=null)
		{
			String[] arr = line.split("=");
			System.out.println(arr[0]+"--"+arr[1]);
			prop.setProperty(arr[0],arr[1]);
		}
		bufr.close();
		System.out.println(prop);
	}
	//JDK 1.6——load方法
	public static void loadDemo()
	{
		Properties prop = new Properties();
		FileInputStream fis = new FileInputStream("info.txt");
		prop.load(fis);
		System.out.println(prop);
	}
	//store方法
}

Properties存取配置文件

/*
用于记录应用程序运行次数。
如果使用次数已到,那么给出注册提示。

使用在内存中运行的计数器是不行的,无法在下次启动程序时继续计数。
需要在程序启动时加载该计时器,并加一后重新存储。
可以定义一个配置文件记录这个计数器。
*/
import java.io.*;
import java.util.*;
class  RunCount
{
	public static void main(String[] args) throws IOException
	{
		Properties prop = new Properties();

		File file = new File("count.ini");
		if (!file.exists())
		{
			file.createNewFile();
		}
		FileInputStream fis = new FileInputStream(file);

		prop.load(fis);

		int count = 0;
		String value = prop.getProperty("time");
		if (value!=null)
		{
			count = Integer.parseInt(value);
			if (count>=5)
			{
				System.out.println("使用次数已到!");
				return;
			}
		}
		count++;
		prop.setProperty("time",count+"");
		FileOutputStream fos = new FileOutputStream(file);
		prop.store(fos,"");
		fos.close();
		fis.close();
	}
}

打印流

该流提供了打印方法,可以将各种数据类型的数据都原样打印。


字节打印流:
PrintStream
构造函数可以接收的参数类型:
1、File对象。File
2、字符串路径。String
3、字节输出流。OutPutStream


字符打印流
PrintWriter
构造函数可以接收的参数类型:
1、File对象。File
2、字符串路径。String
3、字节输出流。OutPutStream
4、字符输出流。Writer

import java.io.*;
class  PrintStreamDemo
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(System.out,true);//true可自动刷新
		String line = null;
		while ((line=bufr.readLine())!=null)
		{
			if ("over".equals(line))
			{
				break;
			}
			out.println(line.toUpperCase());
			//out.flush();
		}
		out.close();
		bufr.close();
	}
}

文件合并

import java.io.*;
import java.util.*;
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:\\a.txt");

		byte[] buf = new byte[1024];
		int len = 0;
		while ((len=sis.read(buf))!=-1)
		{
			fos.write(buf,0,len);
		}
		fos.close();
		sis.close();
	}
}

文件切割

import java.io.*;
import java.util.*;
class SplitFile 
{
	public static void main(String[] args) throws IOException
	{
		merge();
	}
	//切割文件
	public static void splitFile() throws IOException
	{
		FileInputStream fis = new FileInputStream("d:\\a.gif");
		FileOutputStream fos = null;

		byte[] buf = new byte[1024*1024];
		int len = 0;
		int count = 1;
		while ((len=fis.read(buf))!=-1)
		{
			fos = new FileOutputStream("d:\\splitFiles\\"+(count++)+".part");
			fos.write(buf,0,len);
			fos.close();
		}
		fis.close();
	}
	//合并文件
	public static void merge() throws IOException
	{
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
		for (int x=1; x<=3 ; x++ )
		{
			al.add(new FileInputStream("d:\\splitFiles\\"+x+".part"));
		}
		final Iterator<FileInputStream> it = al.iterator();
		Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
		{
			public boolean hasMoreElements()
			{
				return it.hasNext();
			}
			public FileInputStream nextElement()
			{
				return it.next();
			}
		};
		SequenceInputStream sis = new SequenceInputStream(en);
		FileOutputStream fos = new FileOutputStream("d:\\0.gif");
		byte[] buf = new byte[1024];
		int len = 0;
		while ((len=sis.read(buf))!=-1)
		{
			fos.write(buf,0,len);

		}
		fos.close();
		sis.close();
	}
}


------- android培训java培训、期待与您交流! ----------
详细请查看:http://edu.csdn.net/heima/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值