字节流和字符流转换、标准输入流和标准输出流、打印流、对象序列化、随机流

使用InputStreamReader、OutputStreamWriter,将输入的字节流解码成字符流,读入程序;将程序中的字符流编码成字节流,输出

//使用InputStreamReader、OutputStreamWriter,将输入的字节流解码成字符流,读入程序;将程序中的字符流编码成字节流,输出
	@Test
	public void testInputOutStreamReader(){
		long start=System.currentTimeMillis();
		BufferedWriter bw = null;
		BufferedReader br = null;
		try {
			File f1=new File("hello.txt");
			FileInputStream fis=new FileInputStream(f1);
			InputStreamReader isr=new InputStreamReader(fis,"GBK");
			br=new BufferedReader(isr);
			
			File f2=new File("hello-tmp2.txt");
			FileOutputStream fos=new FileOutputStream(f2);			
			OutputStreamWriter osw=new OutputStreamWriter(fos, "unicode");
			bw=new BufferedWriter(osw);
			String str;
			while((str=br.readLine())!=null){
				System.out.println(str);
				bw.write(str);
				bw.newLine();
				bw.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(br!=null)
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(bw!=null)
			try {
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		long end=System.currentTimeMillis();
		System.out.println("执行时间:" + (end-start));
	}



使用标准输入输出流测试BufferredReader

	@Test
	public void testSTDStream(){
		long start=System.currentTimeMillis();
		BufferedReader br = null;
		try {
			InputStream is=System.in;
			InputStreamReader isr=new InputStreamReader(is);
			br = new BufferedReader(isr);
			String str = null;
			while(true){
				System.out.println("请输入:");
				str=br.readLine();
				if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
					break;
				}
				String str1=str.toUpperCase();
				System.out.println(str1);
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}finally{
			try {
				if(br!=null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
		long end=System.currentTimeMillis();
		System.out.println("执行时间:" + (end-start));
	}



测试打印流 PrintStream , PrintWriter

	@Test
	public void testPrintStream(){
		FileOutputStream fos = null;
		try {
			fos=new FileOutputStream(new File("print.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		//创建打印输出流,设置为自动刷新模式(写入换行符和'\n'都会刷新输出缓冲区)
		PrintStream ps=new PrintStream(fos,true);
		if(ps!=null){
			//将打印流设置到到ps上,进而输出到print.txt中
			System.setOut(ps);
		}
		for(int i=0;i<255;i++){
			System.out.print((char)i);
			if(i%50==0){
				System.out.println();
			}
		}
		ps.close();	
	}

使用数据流,数据流是一种二进制流

	//使用数据流输出
	@Test
	public void testDataOutputStream(){
		DataOutputStream dos = null;
		try {
			FileOutputStream fos=new FileOutputStream(new File("data.txt"));
			dos = new DataOutputStream(fos);
			dos.writeUTF("相逢是首歌,青春是绿色的河");
			dos.writeLong(45261783);
			dos.writeBoolean(true);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(dos!=null)
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}			
		}				
	}
	//使用数据流读入
	@Test
	public void testDataInputStream(){
		DataInputStream dis = null;
		try {
			FileInputStream fis=new FileInputStream("data.txt");
			dis = new DataInputStream(fis);
			String str=dis.readUTF();
			System.out.println(str);		
			Boolean b=dis.readBoolean();
			System.out.println(b);
			Long l=dis.readLong();
			System.out.println(l);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 	finally{
			if(dis!=null)
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}	
		}
	}

对象序列化机制 允许把内存中的 Java 对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的 Java 对象。

	@Test
	public void testObjectOutputStream(){	
/*	1)	要求此类是可序列化的,即实现serializable接口
	2)	要求类的属性也要实现serializable接口
	3)	此类有一个表示序列化版本标识符的静态变量
	4)	使用static和transient修饰的成员变量不能被序列化*/
		Person p1=new Person("小名",23);
		Person p2=new Person("小蓝",23);
		
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
			oos.writeObject(p1);
			oos.flush();
			oos.writeObject(p2);
			oos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(oos!=null)
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}			
		}
	}
	//对象反序列化
	@Test
	public void testObjectInputStream(){	
		ObjectInputStream ois = null;
		try {			
			Person p1,p2;
			ois = new ObjectInputStream(new FileInputStream("person.txt"));
			p1=(Person) ois.readObject();
			p2=(Person) ois.readObject();
			System.out.println(p1);
			System.out.println(p2);
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(ois!=null)
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}			
		}
	}

创建 RandomAccessFile 类实例需要指定一个 mode 参数,该参数指定 RandomAccessFile 的访问模式

	//RandomAccessFile测试
	@Test
	public void testRandomAccessFile1(){
		RandomAccessFile raf=null;
		RandomAccessFile raf2=null;
		try {
			raf2=new RandomAccessFile(new File("hello.txt"),"r");
			raf=new RandomAccessFile(new File("random.txt"),"rw");
			
			byte[] b=new byte[20];
			int len;
			while((len=raf2.read(b))!=-1){
				raf.write(b,0,len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(raf!=null){
				try {
					raf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	//覆盖插入
	@Test
	public void testRandomAccessFile2(){
		RandomAccessFile raf=null;
		try {
			raf=new RandomAccessFile(new File("random.txt"),"rw");
			raf.seek(3);
			//实际上是将指针读到第3个字节后面,覆盖插入xy两个字符
			raf.write("xy".getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(raf!=null){
				try {
					raf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//在任意位置不覆盖插入
	@Test
	public void testRandomAccessFile3(){
		RandomAccessFile raf=null;
		try {
			raf=new RandomAccessFile(new File("random.txt"),"rw");
			raf.seek(3);
			String str=raf.readLine();
			
			//移动到第4个字节的位置
			raf.seek(4);
			//获取文件的当前位置	
			long l=raf.getFilePointer();
			System.out.println(l);
			
			StringBuffer sb=new StringBuffer();//使用sb,缓存所有的字符串
			byte[] b=new byte[10];
			int len;
			while((len=raf.read(b))!=-1){
				sb.append(new String(b,0,len));
			}
			raf.seek(4);
			
			raf.write("xy".getBytes());
			raf.write(sb.toString().getBytes());
			//问题:中文依然有乱码
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(raf!=null){
				try {
					raf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值