gpt4 book ai didi

java - 简写为[]

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:48 26 4
gpt4 key购买 nike

如何从Java文件中获取短(16位)数组?

这是我尝试的:

final static String FILE_NAME_INPUT = "./example.txt";

public static void main(String[] args) {
readFile(FILE_NAME_INPUT);
}

public static void readFile(String filename) {
short[] buffer = null;
File a_file = new File(filename);
try {
File file = new File(filename);

FileInputStream fis = new FileInputStream(filename);
DataInputStream dis = new DataInputStream(fis);

int length = (int)a_file.length();
buffer = new short[length];

//dis.readShort(buffer); DOESN'T WORK

for(int i = 0; i < length; i++) {
System.out.println("buffer[" + i + "]: " + buffer[i]);
}

dis.close();
}
catch(FileNotFoundException fe) {
System.out.println("FileNotFoundException: " + fe);
}
catch(IOException ioe) {
System.out.println("IOException: " + ioe);
}
}


但是行 dis.readShort(buffer)不起作用,因为 readShort()必须没有参数。

谢谢

最佳答案

您的缓冲区数组为空,但是如果要检查DataInputStream,可以使用类似的内容

    int counter =0;
while(dis.available()>0)
{
// read two bytes from data input

short s = dis.readShort();

// print the short value
buffer[counter] = s;
System.out.print(s+" ");
counter++;
}

关于java - 简写为[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35752048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com