18 IO 字符集编码

1、File

一个File类的对象,表示了磁盘上的文件或目录

•File类提供了与平台无关的方法来对磁盘上的文件或目录进行操作
•File类直接处理文件和文件系统。

•File类没有指定信息怎样从文件读取或向文件存储

File myFile = new File(" myfile. txt");
File myDir = new File(" MyDocs");
File myFile = new File( myDir, "myfile. txt");
这些构造方法取决于访问文件的方式。例如,若在应用程序里只用一个文件,第一种创建文件的结构是最容易的。但若在同一目录里打开数个文件,则后种方法更好一些。


判断是文件还是目录

file.isDirectory();
file.isFile();
创建目录

File  file =new  File("D:/wang/bing/jia");
System.out.println(file.isDirectory());
file.mkdirs();
System.out.println(file.isDirectory());
过滤文件:利用匿名内部类

String [] names=	file.list(new FilenameFilter()
		{
			
			@Override
			public boolean accept(File dir, String name)
			{
				if (name .endsWith(".txt"))
				{
					return true;
					}
				return false;
			}
		});



2、stream

字节流 
InputStream

FileInputStream fileInputStream1=new FileInputStream("D:/bingjia.txt");
	byte []buffer=new  byte [1024];
	int length=fileInputStream1.read(buffer,0,buffer.length);
	while(length!=-1)
	{
		String string =new String(buffer,0,length);
		System.out.println(string);
		length=fileInputStream1.read(buffer,0,buffer.length);
	}
	fileInputStream1.close();
OutPutStream



OutputStream outputStream =new FileOutputStream("D:/miss.txt");
	
	String string ="i miss you";
	
	byte [] bufferwrite=string.getBytes();
	
	outputStream.write(bufferwrite);
	
	outputStream.close();
缓冲字节流
通过把内存缓冲区连到输入/输出流扩展一个过滤流类。该缓冲区允许Java对多个字节同时进行输入/输出操作,提高了程序性能。

OutputStream outputStream=new FileOutputStream("D:/miss1.txt");
		BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(outputStream);
		
		bufferedOutputStream.write("i miss you".getBytes());
		
		bufferedOutputStream.close();


数据文件流

DataOutputStream(写入二进制文件)

写文件

DataOutputStream dataOutputStream=new DataOutputStream (new BufferedOutputStream(new FileOutputStream("D:/missbingjia.txt")));
		byte a=1;
		int b=222;
		char c='b';
		
		dataOutputStream.writeByte(a);
		dataOutputStream.writeInt(b);
		dataOutputStream.writeChar(c);
		
		dataOutputStream.close();

DataInputStream(读取二进制文件)

读文件

DataInputStream dataInputStream=new DataInputStream(new BufferedInputStream(new FileInputStream("D:/missbingjia")));
		System.out.println(dataInputStream.readByte());
		System.out.println(dataInputStream.readInt());
		System.out.println(dataInputStream.readChar());

字符流


尽管字节流提供了处理任何类型输入/输出操作的足够的功能,它们不能直接操作Unicode字符。既然Java的一个主要目的是支持“只写一次,到处运行”的哲学,包括直接的字符输入/输出支持是必要的。

由于Java采用16位的Unicode字符,因此需要基于字符的输入/输出操作。从Java1.1版开始,加入了专门处理字符流的抽象类Reader和Writer,前者用于处理输入,后者用于处理输出。这两个类类似于InputStream和OuputStream,也只是提供一些用于字符流的规定,本身不能用来生成对象

Java程序语言使用Unicode来表示字符串和字符, Unicode使用两个字节来表示一个字符,即一个字符占16位

FileOutputStream fos = new FileOutputStream("file.txt");
		
		OutputStreamWriter osw = new OutputStreamWriter(fos);
		
		BufferedWriter bw = new BufferedWriter(osw);
		
		bw.write("http://www.google.com");
		bw.write("\n");
		bw.write("http://www.baidu.com");
		
		bw.close();
		
		FileInputStream fis = new FileInputStream("file.txt");
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		
		
		String str = br.readLine();
		
		while(null != str)
		{
			System.out.println(str);
			
			str = br.readLine();
		}
		
		br.close();

FileReader FileWriter

FileReader fileReader=new FileReader("D:/bingjia.txt");
		char []buffer=new char [1024];
		fileReader.read(buffer);
		fileReader.close();
		System.out.println(new String(buffer));

ASCII

ASCII码与一个8位(bit)二进制数对应。其最高位是0,相应的十进制数是0-127。如,数字“0”的编码用十进制数表示就是48。另有128个扩展的ASCII码,最高位都是1,由一些制表符和其它符号组成。ASCII是现今最通用的单字节编码系统。

Unicode

Unicode:这是一种通用的字符集,对所有语言的文字进行了统一编码,对每一个字符都用2个字节来表示,对于英文字符采取前面加“0”字节的策略实现等长兼容。如 “a” 的ASCII码为0x61,UNICODE就为0x00,0x61。(在internet上传输效率较低)

UTF-8

Eight-bit UCS Transformation Format,(UCS,Universal Character Set,通用字符集,UCS 是所有其他字符集标准的一个超集)。一个7位的ASCII码值,对应的UTF码是一个字节。如果字符是0x0000,或在0x0080与0x007f之间,对应的UTF码是两个字节,如果字符在0x0800与0xffff之间,对应的UTF码是三个字节(汉字为3个字节)  不等长

RandomAccessFile

RandomAccessFile类同时实现了DataInput和DataOutput接口,提供了对文件随机存取的功能,利用这个类可以在文件的任何位置读取或写入数据。
•RandomAccessFile类提供了一个文件指针,用来标志要进行读写操作的下一数据的位置。

RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
public void write(RandomAccessFile raf) throws Exception
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>raf.writeInt(this.id);
<span style="white-space:pre">		</span>raf.writeUTF(this.name);
<span style="white-space:pre">		</span>raf.writeDouble(this.height);
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public void read(RandomAccessFile raf) throws Exception
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>this.id = raf.readInt();
<span style="white-space:pre">		</span>this.name = raf.readUTF();
<span style="white-space:pre">		</span>this.height = raf.readDouble();
<span style="white-space:pre">	</span>}

序列化

将对象转换为字节流保存起来,并在以后还原这个对象,这种机制叫做对象序列化。

一个类若想被序列化,则需要实现java.io.Serializable接口,该接口中没有定义任何方法,是一个标识性接口(Marker Interface),当一个类实现了该接口,就表示这个类的对象是可以序列化的。



在序列化时,static变量是无法序列化的;如果A包含了对B的引用,那么在序列化A的时候也会将B一并地序列化;如果此时A可以序列化,B无法序列化,那么当序列化A的时候就会发生异常,这时就需要将对B的引用设为transient,该关键字表示变量不会被序列化。

class Person implements Serializable
{
	int age;

	String name;

	double height;

	public Person(int age, String name, double height)
	{
		this.age = age;
		this.name = name;
		this.height = height;
	}
}

public static void main(String[] args) throws Exception
	{
		Person p1 = new Person(20, "zhangsan", 4.55);
		Person p2 = new Person(50, "lisi", 4.67);
		Person p3 = new Person(10, "wangwu", 17.78);

		FileOutputStream fos = new FileOutputStream("person.txt");

		ObjectOutputStream oos = new ObjectOutputStream(fos);

		oos.writeObject(p1);
		oos.writeObject(p2);
		oos.writeObject(p3);

		oos.close();

		System.out.println("--------------------");

		FileInputStream fis = new FileInputStream("person.txt");

		ObjectInputStream ois = new ObjectInputStream(fis);

		Person p = null;

		for (int i = 0; i < 3; i++)
		{
			p = (Person) ois.readObject();

			System.out.println(p.age + "," + p.name + "," + p.height);
		}

		ois.close();
	}

当我们在一个待序列化/反序列化的类中实现了以上两个private方法(方法声明要与上面的保持完全的一致),那么就允许我们以更加底层、更加细粒度的方式控制序列化/反序列化的过程。


转载于:https://my.oschina.net/900116/blog/482250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值