JavaSe基础XX18——IO流_7

*59-IO流(RandomAccessFile-写入)









		/*
		 * RandomAccessFile
		 * 一看这个类名字,纠结。不是io体系中的子类。
		 * 
		 * 特点:
		 * 1,该对象即能读,又能写。
		 * 2,该对象内部维护了一个byte数组,并通过指针可以操作数组中的元素,
		 * 3,可以通过getFilePointer方法获取指针的位置,和通过seek方法设置指针的位置。
		 * 4,其实该对象就是将字节输入流和输出流进行了封装。 
		 * 5,该对象的源或者目的只能是文件。通过构造函数就可以看出。 
		 * 
		 * 
		 */








609四个字节,高八位删除了。












	//使用RandomAccessFile对象写入一些人员信息,比如姓名和年龄。
	public static void writeFile() throws IOException{
		/*
		 * 如果文件不存在,则创建,如果文件存在,不创建
		 * 
		 */
		RandomAccessFile raf = new RandomAccessFile("ranacc.txt","rw");
		
		raf.write("张三".getBytes());
		raf.writeInt(97);
		raf.write("小强".getBytes());
		raf.writeInt(99);
//		
		raf.close();
	}




*60-IO流(RandomAccessFile-读取&随机读取)


读。
















	public static void readFile() throws IOException {
		
		RandomAccessFile raf = new RandomAccessFile("ranacc.txt", "r");
		
		//通过seek设置指针的位置。
		raf.seek(1*8);//随机的读取。只要指定指针的位置即可。 
		
		byte[] buf = new byte[4];
		raf.read(buf);
		
		String name = new String(buf);
		
		int age = raf.readInt();
		
		System.out.println("name="+name);
		System.out.println("age="+age);
		
		System.out.println("pos:"+raf.getFilePointer());
		
		raf.close();
		
		
	}



*61-IO流(RandomAccessFile-随机写入&细节)










	public static void randomWrite() throws IOException{
		RandomAccessFile raf = new RandomAccessFile("ranacc.txt", "rw");
		
		//往指定位置写入数据。
		raf.seek(3*8);
		
		raf.write("哈哈".getBytes());
		raf.writeInt(108);
		
		raf.close();
	}

随机访问数据,一般存的数据是有规律的。

如果是不规律的名字,则可以用好几个位来存储。



*62-IO流(管道流-PipedStream)




以前读入和读出是分开的。没有关系的。

而管道流是使得两者产生了关系。



必须结合多线程来用。


package cn.itcast.io.p4.piped;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedStream {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		PipedInputStream input = new PipedInputStream();
		PipedOutputStream output = new PipedOutputStream();
		
		input.connect(output);
		
		new Thread(new Input(input)).start();
		new Thread(new Output(output)).start();
		
	}

}


class Input implements Runnable{
	
	private PipedInputStream in;
	Input(PipedInputStream in){
		this.in = in;
	}
	public void run(){
		
		try {
			byte[] buf = new byte[1024];
			int len = in.read(buf);
			
			String s = new String(buf,0,len);
			
			System.out.println("s="+s);
			in.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}

class Output implements Runnable{
	private PipedOutputStream out;
	Output(PipedOutputStream out){
		this.out = out;
	}
	public void run(){
		
		try {
			Thread.sleep(5000);
			out.write("hi,管道来了!".getBytes());
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}


*63-IO流(操作基本类型数据的流对象-DataStream)











package cn.itcast.io.p5.datastream;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

public class DataSteamDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
//		writeData();
		readData();
		
	}

	public static void readData() throws IOException {
		
		DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
		
		String str = dis.readUTF();
		
		System.out.println(str);
	}

	public static void writeData() throws IOException {
		
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
		
		dos.writeUTF("你好");
		
		dos.close();
		
		
	}

}

*64-IO流(操作数组的流)






操作文件:调用资源、

操作内存:维护的数组,没有调用底层资源,所以是无效的。关闭之后,仍然可以使用。





package cn.itcast.io.p6.bytestream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayStreamDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) {

		ByteArrayInputStream bis = new ByteArrayInputStream("abcedf".getBytes());
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		
		int ch = 0;
		
		while((ch=bis.read())!=-1){
			bos.write(ch);
		}
		
		System.out.println(bos.toString());
	}

}





*65-IO流(编码表)








*66-IO流(简单编码解码)

文字和二进制之间的转码解码


	/**
	 * @param str
	 * @throws UnsupportedEncodingException
	 */
	public static void encodeDemo(String str)
			throws UnsupportedEncodingException {
		//编码;
		byte[] buf = str.getBytes("UTF-8");
		
//		printBytes(buf);
		
		//解码:
		String s1 = new String(buf,"UTF-8");
		
		System.out.println("s1="+s1);
	}

	private static void printBytes(byte[] buf) {
		for(byte b : buf){
			System.out.print(b +" ");
		}
	}



*67-IO流(编码解码问题_1)







Tomcat上很常见。




*68-IO流(编码解码问题_2)



为什么用iso8859-1 可以呢?



	public static void main(String[] args) throws IOException {

		/*
		 * 字符串 --> 字节数组:编码。
		 * 字节数组 --> 字符串:解码。
		 * 
		 * 你好:GBK:  -60 -29 -70 -61
		 * 
		 * 你好: utf-8: -28 -67 -96 -27 -91 -67 
		 * 
		 * 
		 * 如果你编错了,解不出来。
		 * 如果编对了,解错了,有可能有救。
		 */
		
		String str = "谢谢";
		
		byte[] buf = str.getBytes("gbk");
		
		String s1 = new String(buf,"UTF-8");
		
		System.out.println("s1="+s1);
		
		
		byte[] buf2 = s1.getBytes("UTF-8");//获取源字节.
		
		printBytes(buf2);//-17 -65 -67 -17 -65 -67 -17 -65 -67  你好
					//-17 -65 -67 -17 -65 -67 -17 -65 -67 -17 -65 -67 哈哈
					//-48 -69 -48 -69  谢谢
		String s2 = new String(buf2,"GBK");
		
		System.out.println("s2="+s2);
		
		
//		encodeDemo(str);
		
		
		
	}


*69-IO流(联通问题)




联通:编码正确,解码错误。









package cn.itcast.io.p7.encode;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class LianTong {

	/**
	 * @param args
	 * @throws IOException 
	 */

	
	public static void main(String[] args) throws IOException {
		
		String str = "联通";
		/*
		11000001
		10101010
		11001101
		10101000
		*/

		
		byte[] buf = str.getBytes("gbk");
		
		for(byte b :buf){
			System.out.println(Integer.toBinaryString(b&255));
		}
	}
	
}



*70-IO流(练习-按字节截取字符串)


	/*
	  	在java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符。
		但对应的字节数不同,一个汉字占两个字节。
		定义一个方法,按照最大的字节数来取子串。
		如:对于“ab你好”,如果取三个字节,那么子串就是ab与“你”字的半个,
		那么半个就要舍弃。如果去四个字节就是“ab你”,取五个字节还是“ab你”.
		
		
	 */
		int len = str.getBytes("utf-8").length;		
		for(int x=0; x<len; x++){
			System.out.println("截取"+(x+1)+"个字节结果是:"+cutStringByU8Byte(str, x+1));
		}
		




GBK编码:

	public static String cutStringByByte(String str,int len) throws IOException{
		
		byte[] buf = str.getBytes("gbk");
		
		int count = 0;
		for(int x=len-1; x>=0; x--){
			if(buf[x]<0)
				count++;
			else
				break;
		}
		
		if(count%2==0)
			return new String(buf,0,len,"gbk");
		else
			return new String(buf,0,len-1,"gbk");
	}


GBk中有些字 第一个字节是负的,第二个是正的!




这个时候 应该怎么办?

这个方法到底可以用么?可以。




package cn.itcast.io.p7.encode;

import java.io.IOException;


public class Test {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		String str = "ab你好cd谢谢";
//		str = "ab琲琲cd琲琲";
		
//		int len = str.getBytes("gbk").length;		
//		for(int x=0; x<len; x++){
//			System.out.println("截取"+(x+1)+"个字节结果是:"+cutStringByByte(str, x+1));
//		}
		
		int len = str.getBytes("utf-8").length;		
		for(int x=0; x<len; x++){
			System.out.println("截取"+(x+1)+"个字节结果是:"+cutStringByU8Byte(str, x+1));
		}
		
		
		
//		String str = "琲";
//		byte[] buf = str.getBytes("gbk");
//		for(byte b : buf){
//			System.out.println(b);//-84  105 
//		}
			
	}
	
	/*
	  	在java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符。
		但对应的字节数不同,一个汉字占两个字节。
		定义一个方法,按照最大的字节数来取子串。
		如:对于“ab你好”,如果取三个字节,那么子串就是ab与“你”字的半个,
		那么半个就要舍弃。如果去四个字节就是“ab你”,取五个字节还是“ab你”.
		
		
	 */
	
	public static String cutStringByU8Byte(String str, int len) throws IOException {
		
		byte[] buf = str.getBytes("utf-8");
		
		int count = 0;
		for(int x=len-1; x>=0; x--){
			if(buf[x]<0)
				count++;
			else
				break;
		}
		
		if(count%3==0)
			return new String(buf,0,len,"utf-8");
		else if(count%3==1)
			return new String(buf,0,len-1,"utf-8");
		else 
			return new String(buf,0,len-2,"utf-8");
		
	}

	public static String cutStringByByte(String str,int len) throws IOException{
		
		byte[] buf = str.getBytes("gbk");
		
		int count = 0;
		for(int x=len-1; x>=0; x--){
			if(buf[x]<0)
				count++;
			else
				break;
		}
		
		if(count%2==0)
			return new String(buf,0,len,"gbk");
		else
			return new String(buf,0,len-1,"gbk");
	}
}








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值