`
dupengtao
  • 浏览: 31406 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

黑马程序员_基础复习七(IO)

 
阅读更多

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

 

 

总结:

转换流的应用:

 

public static void main(String[] args) throws IOException
	{
		//发现读取键盘的方式,和readLine的功能是一致的。
		//那么已经有这个功能,直接去使用即可。
		//但是很遗憾。System.in。是字节读取流。
		//readLine是BufferedReader字符流对象的方法。

		//键盘录入。
		InputStream in = System.in;

		//需要将字节流转成字符流。
		InputStreamReader isr = new InputStreamReader(in);

		//使用缓冲区的readLine
		BufferedReader bufr = new BufferedReader(isr);

		String line  = null;

		while((line=bufr.readLine())!=null)
		{

			if("over".equals(line))
				break;
			System.out.println(line.toUpperCase());

		}
		bufr.close();
	}

 

 

 

 

 

IO流的操作规律

 

三个明确:

 

1,明确源(读)和目的(写)。

 

其实就是在明确输入流还是输出流。

 

源:输入流 InputStream  Reader

 

目的:输出流 OutputStream  Writer

 

 

2,明确操作的数据内容。

 

其实就是在明确字符流还是字节流。

 

如果数据都是纯文本数据使用字符流。

 

如果数据是非文本数据使用字节流。

 

前两步已经明确了使用哪一个体系。

 

 

3,明确具体的设备。

 

明确使用的具体对象。

 

源设备:内存,键盘(System.in),硬盘(文本)(File)。

 

目的设备:内存,控制台(System.out),硬盘(文件)(File).

 

扩展部分:需要进行高效操作吗?

 

是:加入缓冲区技术(Buffered)。

 

 

 

小练习:

 

 

--------------------------------------------------
需求1:
读取键盘,并键盘录入的数据变成大写打印在控制台上。

自己动手完成分析步骤。




需求2:
读取键盘录入数据,将数据变成大写,保存到一个文件中。

分析:
源:键盘,InputStream。Reader
是纯文本数据:Reader.
设备:System.in.

已经明确了使用读取字符流体系,可是设备是键盘System.in是读取字节流对象。
这时就需要将字节流转成字符流。用到了InputStreamReader.

需要高效吗?需要。Buffered

BufferedReader bufr = 
		new BufferedReader(new InputStreamReader(System.in));
大家记住,以后老师在说读键盘录入,就写这句代码!


目的:硬盘,OutputStream Writer
纯文本?yes Writer
设备:硬盘文件 FileWriter。
缓冲,yes

BufferedWriter bufw = new BufferedWriter(new FileWriter("a.txt"));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt")));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),"GBK"));
这三句都是等效的。使用都是默认的编码表GBK.

另加一个需求?
想要将录入的数据按照UTF-8另一个编码表的形式进行数据的存储。
那么目的就要变化了,因为FileWriter,虽然可以作为文件目的对象,但是该对象中的编码表是默认的GBK。
当想要指定编码表时,必须要使用转换流。
BufferedWriter bufw = 
	BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),"UTF-8"));


##############################################
#记住:凡是涉及到编码转换的操作,            #
#一定要想到转换流,如果只用默认码表,		 #
#可是使用转换流的子类FileWriter或FileReader. #
##############################################





-----------------------------------------------------


需求3:
读取一个文本文件,将文本文件的数据展现在控制台上。
分析:
源:硬盘文件。InputStream Reader。
纯文本?yes  Reader。
设备:硬盘 FileReader.
缓冲?yes  BufferedReader 

BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));

目的:OutputStream  Writer
纯文本?yes  Writer.
设备:控制台 System.out.

发现控制台的对象是System.out.是字节输出流。
因为是纯文本数据,使用Writer所以可以将字符流转成成字节串流。
使用了转换流 OutputStreamWriter

需要高效吗?需要。

BufferedWriter bufw = 
	new BufferedWriter(new OutputStreamWriter(System.out));



---------------------------------------------------


需求4:
读取一个文本文件,将数据变成大写,存到一个文件中。

分析:
源?目的?
源:一个硬盘文件。就明确了要使用InputStream  Reader
是纯文本的吗?是,就进一步明确了使用Reader.
什么设备:硬盘文件。在进一步明确了使用Reader体系中FileReader

FileReader fr = new FileReader("a.txt");

需要提高效率吗?需要!
BufferedReader bufr = new BufferedReader(fr);



目的:一个硬盘文件。使用体系 OutputStream Writer
是纯文本吗?是,使用Writer。
目的设备:硬盘文件.使用FileWriter.

FileWriter fw = new FileWriter("b.txt");

需要高效吗?需要!
BufferedWriter bufw = new BufferedWriter(fw);
*/

 

 

file类下的方法

 

public void deleteOnExit()

 

在虚拟机终止时,请求删除此抽象路径名表示的文件或目录。 文件(或目录)将以与注册相反的顺序删除。调用此方

 

法删除已注册为删除的文件或目录无效。根据 Java 语言规范中的定义,只有在虚拟机正常终止时,才会尝试执行删除操

 

作。 

 

一旦请求了删除操作,就无法取消该请求。所以应小心使用此方法。


getPath():file类中封装的是什么路径(绝对路径或相对路径)返回的就是什么路径

getAbsolutePath():无论file类中封装的什么样的路径都返回绝对路径

 

 

文件后缀过滤器:

 

 

public static void method_filter()
	{
		File dir = new File("d:\\java0217\\day02");

		String[] names = dir.list(new FilenameFilter()
		{
			public boolean accept(File dir,String name)
			{
				//System.out.println(dir+"......"+name);
				//return true;
				return name.endwith(".java");
			}
		});

		for(String name: names)
		{
		
			System.out.println(name);
		}
	}
	public static void main(String[] args) 
	{
		method_filter();
	}
 

 

Properties

void list(PrintStream out) 

 

          将属性列表输出到指定的输出流。 

 

  void list(PrintWriter out) 

 

          将属性列表输出到指定的输出流。 

 

prop.list(new PrintStream("sysinfo.txt"));//数据写入到文件中

 

prop.list(System.out);//数据写入到控制台

 

 

//演示一下load方法原理
	public static void demo_load_2()throws IOException
	{
		Properties prop = new Properties();
		BufferedReader bufr = 
			new BufferedReader(new InputStreamReader(new FileInputStream("user.txt")));

		String line = null;

		while((line=bufr.readLine())!=null)
		{
			String[] arr = line.split("=");

			prop.setProperty(arr[0],arr[1]);

		}

		prop.list(System.out);

		bufr.close();

	}



 

 

 

注意:

#号 是配置文件的注释信息

 

public Object setProperty(String key, String value)

 

调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。强制要求为属性的键和值使用字符串。

 

返回值是 Hashtable 调用 put 的结果。 这个方法可以将properties类中的数据进行修改,但是不能修改配置文件的信

 

息,

 

public void store(OutputStream out, String comments) throws IOException

 

这个方法可以修改配置文件信息,comments为注释,可以不写

 

 

让一个对象具有比较性,要实现comparable接口,之后重写

hashcode、equals、compareTo方法。

例子:

import java.io.*;
import java.util.*;
class Student implements Comparable<Student>
{
	private String name;
	private int ma,en,cn;
	private int sum;
	Student(String name,int ma,int en,int cn)
	{
		this.name = name;
		this.ma = ma;
		this.en = en;
		this.cn = cn;

		sum = ma+en+cn;
	}
	
	public int hashCode()
	{
		final int NUMBER = 29;
		return name.hashCode()+sum*NUMBER;
	}
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Student))
			throw new RuntimeException("存入了非Student对象");

		Student stu = (Student)obj;

		return this.name.equals(stu.name) && this.sum == stu.sum;

	}

	public int compareTo(Student stu)
	{
		int num = new Integer(this.sum).compareTo(new Integer(stu.sum));

		return num==0?this.name.compareTo(stu.name):num;
	}

	public String getName()
	{
		return name;
	}
	public int getSum()
	{
		return sum;
	}
}


 

 


文本键切割和合并

 

文件的切割与合并

import java.io.*;
import java.util.*;

/*

*/

class  SplitFile
{
	public static void main(String[] args) throws IOException
	{
		//splitFile();
		merge();
	}

	public static void merge()throws IOException
	{
		/*
		获取碎片文件中配置文件的信息。

		*/
		File dir = new File("c:\\partfiles\\");
		File[] files = dir.listFiles(new FileFilter()
		{
			public boolean accept(File pathname)
			{
				return pathname.getName().endsWith(".properties");
			}
		});
		
		File confFile = files[0];


		Properties prop = new Properties();

		FileInputStream fis = new FileInputStream(confFile);

		prop.load(fis);

		String filename = prop.getProperty("filename");
		int partCount = Integer.parseInt(prop.getProperty("partcount"));




			


		/*
		用于合并碎片文件。
		*/
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();

		for(int x=1; x<partCount; x++)
		{
			al.add(new FileInputStream(new File(dir,x+".mypart")));
		}

		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(new File(dir,filename));

		byte[] buf = new byte[1024*4];

		int len = 0;

		while((len=sis.read(buf))!=-1)
		{
			fos.write(buf,0,len);
		}

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

	public static void splitFile()throws IOException
	{

		File file = new File("c:\\0.bmp");

		File dir = new File("c:\\partfiles\\");

		FileInputStream fis = new FileInputStream(file);

		byte[] buf = new byte[1024*1024];

		int len = 0;

		FileOutputStream fos = null;
		int count = 1;
		
		while((len=fis.read(buf))!=-1)
		{
			fos = new FileOutputStream(new File(dir,(count++)+".mypart"));

			fos.write(buf,0,len);

			fos.close();
		}
		//System.out.println("count="+count);

		Properties prop = new Properties();

		prop.setProperty("partcount",count+"");
		prop.setProperty("filename",file.getName());

		File confFile = new File(dir,count+".properties");
		fos = new FileOutputStream(confFile);

		prop.store(fos,"conf");
		
		fos.close();
		fis.close();
	}
}
 

 

 

 

 

 

 

 

 

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

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics