zoukankan      html  css  js  c++  java
  • Java输入输出流

    Java输入输入流有的时候很难理解,特别是read,write,到底是数据从什么地方到什么地方呢。下面有个图标示了数据的方向: 下面的代码是以Java字符流中的CharArrayReader和CharArrayWriter为例的代码:
    package com.javaBooks.sixth;
    import java.io.CharArrayReader;
    import java.io.IOException;
    
    public class CharArrayReaderTest {
    	public static void main(String[] args) throws IOException {
    		String s = "BUAASEM108CHENDANGYANG";
    		int length = s.length();
    		char charArray[] = new char[length];
    		s.getChars(0, length, charArray, 0);
    		CharArrayReader inputChar1 = new CharArrayReader(charArray);
    		CharArrayReader inputChar2 = new CharArrayReader(charArray, 0, 5);
    		int i;
    		System.out.println("inputChar1 is: ");
    		while ((i = inputChar1.read()) != -1) {
    			System.out.print((char) i);
    		}
    		System.out.println();
    		System.out.println("inputChar2 is: ");
    		while ((i = inputChar2.read()) != -1) {
    			System.out.print((char) i);
    		}
    		System.out.println();
    	}
    }
    
    package com.javaBooks.sixth;
    
    import java.io.CharArrayWriter;
    import java.io.IOException;
    
    public class CharArrayWriterTest {
    	public static void main(String[] args) throws IOException {
    		CharArrayWriter charArrayWriter = new CharArrayWriter();
    		String s = "BUAASEM108CHENDANGYANG";
    		char buffer[] = new char[s.length()];
    		s.getChars(0, s.length(), buffer, 0);
    		charArrayWriter.write(buffer);
    		System.out.println("String buffer ");
    		System.out.println(charArrayWriter.toString());
    		System.out.println("write to an array");
    		char chars[] = charArrayWriter.toCharArray();
    		for (int i = 0; i < chars.length; i++) {
    			System.out.print(chars[i]);
    		}
    		System.out.println();
    	}
    }
  • 相关阅读:
    解决Too many connections问题
    TPCC-MySQL安装、使用及结果解读
    Spring事务配置
    【转】Spring事务介绍
    Spring AOP的实现原理
    Spring AOP的使用
    Spring整合Junit4进行单元测试
    Spring Bean定义的三种方式
    spring集成Redis(单机、集群)
    Redis搭建(五):Cluster集群搭建
  • 原文地址:https://www.cnblogs.com/wanyakun/p/3403220.html
Copyright © 2011-2022 走看看