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();
    	}
    }
  • 相关阅读:
    10个用jQuery实现图片幻灯片/画廊效果和源码
    老赵面试题参考答案(二)
    C#的显式接口和隐式接口
    老赵面试题参考答案(三)
    C#中的参数传递:值类型(value type)和引用类型(reference type)
    word转换成html的方法
    老赵面试题参考答案(四)
    五个Metro UI 风格的网页设计
    老赵面试题参考答案(六)
    概要设计怎么写?
  • 原文地址:https://www.cnblogs.com/wanyakun/p/3403220.html
Copyright © 2011-2022 走看看