fixedlengthstringio.java

来自「java程序设计导论(daniel liang著) 所有偶数课后习题答案」· Java 代码 · 共 32 行

JAVA
32
字号
import java.io.*;public class FixedLengthStringIO {  // Read fixed number of characters from a DataInput stream  public static String readFixedLengthString(int size,    DataInput in) throws IOException {    // Declare an array of characters    char[] chars = new char[size];    // Read fixed number of characters to the array    for (int i = 0; i < size; i++)      chars[i] = in.readChar();    return new String(chars);  }  // Write fixed number of characters to a DataOutput stream  public static void writeFixedLengthString(String s, int size,    DataOutput out) throws IOException {    char[] chars = new char[size];        // Write characters from string into an array    s.getChars(0, s.length(), chars, 0);        // Fill in blank characters in the rest of the array    for (int i = Math.min(s.length(), size); i < chars.length; i++)      chars[i] = ' ';         // Create and write a new string padded with blank characters    out.writeChars(new String(chars));  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?