问题已经写在代码中
public static void main(String[] args) throws IOException
{
File file = new File(filepath);
InputStream input = new FileInputStream(file);
byte[] bytes = new byte[2000];
//在这里使用 new byte(file.available) 不知道为什么出现错误,一直显示数组元素超出
int count = 0;
while((bytes[count] = (byte)input.read()) != -1)
//暂时不知道为什么不能等于 -1 使循环结束
{
count++;
}
String content = new String(bytes);
System.out.println(content);
}
还有为什么这样不行,String content = new String(bytes); 会显示错误(实际上还是上面那个问题)
while(true)
{
bytes[count] = (byte)input.read();
count++;
}
String content = new String(bytes);
System.out.println(content);
file有available属性吗,应该是FileInputStream的available方法吧
看看这个:https://my.oschina.net/zzw922cn/blog/486990
byte[] bytes = new byte[inpuit.available()];
方括号,而且应该是 input 不是 f
读取文件的FileInputStream.available() = 0,不能用来初始化数组
new String保存是乱码了吗