• 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();
    	}
    }
  • 相关阅读:
    -for循环案例(下)
    for循环案例(上)
    for循环
    判断语句案例
    判断语句
    操作符优先级
    windows 下安装图片标注软件 labelling和出错解决
    tf-faster rcnn
    目标检测——Faster R-CNN 详解、Pytorch搭建、训练自己的数据集
    java idea 配置安装
  • 原文地址:https://www.cnblogs.com/wanyakun/p/3403220.html
Copyright © 2020-2023  润新知