java.io.EOFException使用DataInputStream.readUTF读取String

时间:2017-05-02 05:21:01

标签: java datainputstream

我坚持使用Java编写的项目代码的一部分。每次调用以下方法时都会出现问题:

public static void printData(File f,int n){

   try {
       DataInputStream dis = new DataInputStream(new FileInputStream(f));
       for (int i = 0; i < 5; i++) {
        System.out.println(dis.readUTF());
       }

       dis.skipBytes(43*(n-10));

       for (int i = 0; i < 5; i++) {
        System.out.println(dis.readUTF());
       }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

每当我尝试跳过一定数量的字节时,代码就会抛出抛出EOFException,换句话说,当程序在第二个FOR循环中尝试再次读取时程序停止(第一个工作正常),但似乎它与skipBytes方法有关......但我不清楚是什么导致了这种情况。欢迎任何帮助。

顺便说一下,43是每行的字节数,n(2 ^ 9)是用f写的行数。

1 个答案:

答案 0 :(得分:0)

请参阅DataInputStream.readUTF()方法的文档。如果出现以下情况,则抛出EOFException:

dis.skipBytes(43*(n-10));

因此,您已经读取了所有字节,或者您在

处跳过了它们
# Try and enumerate the VM's services 
foreach ($VM in $VMs) {
        Write-Output "Listing all services..."
        Write-Output ("VM: {0}" -f $VM.Name)
        $ResourceGroupName=$VM.ResourceGroupName
        $Name=$VM.Name
        $status=(Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $Name -Status).Statuses[1].code
        if($status -like "PowerState/deallocated")
        {
            Write-Output "VM is stopped, starting VM"
            if (Start-AzureRmVm -ResourceGroupName $ResourceGroupName -Name $Name)
            {
                Write-Output "Start VM successfuly"
            }else
            {
                Write-Output "Start VM failed"
                break
            }

        }
        ##get VM's Public IP
        $nicName = ($VM.NetworkInterfaceIDs[0] -split '/')[-1]
        $pip = (Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $ResourceGroupName).IpConfigurations.publicipaddress.id
        $PublicIP=(Get-AzureRmPublicIpAddress -ResourceGroupName shui -Name ($pip -split '/')[-1]).IpAddress

        $Uri="http://$($PublicIP):5986"
        Write-Output "Get ConnectionUri $Uri"

        ##get Credential from assets
        $shui=Get-AutomationPSCredential -Name 'shui' 

        $session=New-PSSession -ConnectionUri $Uri -Credential $shui -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)

        $result=Invoke-Command -Session $session -ScriptBlock {ipconfig}
        Write-Output "$result"
        Write-Output "Finished listing..."
}