黑马程序员---java学习笔记之IO3

来源:互联网 发布:苏州人口数据统计 编辑:程序博客网 时间:2024/06/14 11:48

1、计算某个应用程序的使用次数,该示例主要是Properties类的应用

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;public class AppCouterDemo {/** * @param args */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubProperties p = new Properties();File file = new File("properties.txt");if( !file.exists() )file.createNewFile();FileInputStream fis = new FileInputStream(file);int counter = 0; p.load(fis); String value = p.getProperty("counter"); if( value != null ) { counter = Integer.parseInt(value); //System.out.println(counter); if( counter > 3 ) { System.out.println("试用期已到"); return ; } } counter++; p.setProperty("counter",""+counter);  FileOutputStream fos = new FileOutputStream(file);  p.store(fos,"");  fis.close(); fos.close(); }}
2、合并流SequenceInputStream的示例合并两个MP3文件的代码:

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.Enumeration;import java.util.Vector;public class CombineStreamImg {public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFileInputStream fis1 = new FileInputStream("C:\\Users\\AZhan\\Desktop\\1.mp3");FileInputStream fis2 = new FileInputStream("C:\\Users\\AZhan\\Desktop\\2.mp3");Vector<FileInputStream> v = new Vector<FileInputStream>();v.add(fis1);v.add(fis2);Enumeration<FileInputStream> en = v.elements();SequenceInputStream sis = new SequenceInputStream(en);FileOutputStream fos = new FileOutputStream("C:\\Users\\AZhan\\Desktop\\3.mp3");byte[] buf = new byte[1024];int len = 0;while( (len = sis.read(buf)) != -1 ){fos.write(buf,0,len);}fos.close();sis.close();}}
3、分割MP3文件之后,被分割的文件仍旧可以播放。

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class SplitMP3Demo {/** * @param args */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFileInputStream fis = new FileInputStream("C:\\Users\\AZhan\\Desktop\\1.mp3");byte[] buf = new byte[1024*1024];int len = 0;int cnt = 0;while((len = fis.read(buf)) != -1){FileOutputStream fos =new FileOutputStream("C:\\Users\\AZhan\\Desktop\\1_"+(cnt++)+".mp3");fos.write(buf,0,len);fos.close();}fis.close();}}
4、文本文件,MP3文件都可以被分割和合并,并且分割或合并的文件还可以正常使用,但是图像文件就不可以了,不能切割也不能合并,切割之后不能读取,合并之后只能读取合并前的其中一幅。

5、PrintWriter:字符打印流,较常用;PrintStream:字节打印流。

6、对象的序列化:

        类中的成员变量或者成员函数都有一个数字标识,序列号就是根据这些数字标识计算出来的。

        序列化的类需要实现java.io.Serializable接口。

7、集合中涉及到io流的是Properties,io流中涉及到多线程的就是管道流(PipedInputStream和PipedOutputStream)

8、随机存取文件类RandomAccessFile类,该类的特点是,可读取写入文件内容,并且可以通过操作文件指针随机地存取文件内容。

9、使用utf-8字符编码集将数据写入到一个文件中

import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;public class DataCharSetNameDemo {public static void main(String[] args) throws IOException{OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("F:\\Date.txt"),"utf-8");osw.write("你好,寒假");osw.flush();osw.close();}}
10、DataInputStream和DataOutputStream流操作的是java的基本数据类型,另外需注意的是,当你使用DataOutputStream类中writeUTF方法将数据写入到文件时,读取时就只能使用DataInputStream类中的ReadUTF方法进行读取。

11、ByteArrayInputStream和ByteArrayOutputStream没用调用底层资源,所以关闭这两个流无效,即使关闭,之后也可以被调用。

12、总结

源设备:键盘(System.in)、硬盘(FileInputStream和FileReader)、内存(ByteArrayInputStream);

目的设备:控制台(System.out)、硬盘(FileOutputStream和FileReader)、内存(ByteArrayOutputStream)

13、ByteArrayOutputStream类中的writeTo方法可以将该字节数组输出流中的缓冲区数据输出到其他的目的设备中。

14、汇总:

操作基本数据类型:DataInputStream和DataOutputStream

操作字节数组:ByteArrayInputStream和ByteArrayOutputStream

操作字符数组:CharArrayReader和CharArrayWriter

操作字符串:StringReader和StringWriter

15、编码表:

        1):美国标准信息交换码ascii:使用一个字节的7位;

        2):欧洲的编码表iso8859-1:使用了一个字节的8位;

        3):GB2312:使用两个字节,总共有六千多汉字;

        4):GBK:融合了更多的汉字,两万多;

        5):Unicode:融合全世界的较常用文字;

        6):UTF-8:最低8位的Unicode转换格式,是Unicode的优化。

16.java中设计到编码表的是转化流。

17、硬盘上存储的其实是二进制数据,当我们打开一个文本文档时,之所以没有显示数字而是显示相应的数据,那是因为记事本是一个软件,它通过显示那些数字数据根据相应的编码表查得具体的数据,然后再显示出来。

0 0