集册 Java实例教程 从数据输入读取固定字符串

从数据输入读取固定字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

562
从DataInput读取固定字符串

//package com.nowjava;

import java.io.*;
/* from nowjava.com - 时代Java*/

public class Main {

    public static String readFixedString(DataInput in, int length)

            throws IOException {

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; i++) {

            char c = in.readChar();

            //if char is not Unicode zero add

            if (c != 0)

                sb.append(c);

        }

        return sb.toString();

    }

}