集册 Java实例教程 将固定字符串写入数据输出

将固定字符串写入数据输出

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

528
将固定的字符串写入DataOutput

//package com.nowjava;

import java.io.*;/* 来自 N  o w  J a v a . c o m*/


public class Main {

    public static void writeFixedString(DataOutput out, int length, String s)

            throws IOException {

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

            if (i < s.length())

                out.writeChar(s.charAt(i)); //write char

            else

                out.writeChar(0); //write Unicode zero

        }

    }

}