1 にバッファを加え、データ数を返し、読み取り後に -1 を返します。
public class MyClass {
public static void main(String[] args) throws IOException{
FileInputStream inputStream;
File file=new File("D:\\bg.txt");
try {
inputStream=new FileInputStream(file);
byte[] b=new byte[1024];
int d;
while ((d=inputStream.read(b))!=-1){
System.out.println(d);//输出 12
}
System.out.println("+**"+d);//输出 -1
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (inputStream!=null){
inputStream.close();
}
}
}
}
2 バッファは追加されず、1 バイトが返され、読み取り後に -1 が返されます。
public class MyClass {
public static void main(String[] args) throws IOException{
FileInputStream inputStream;
File file=new File("D:\\bg.txt");
try {
inputStream=new FileInputStream(file);
int d;
while ((d=inputStream.read())!=-1){
System.out.println(d);
/* 输出
230
137
147
229
188
128
231
156
139
231
156
139
*/
}
System.out.println("+**"+d);//输出 -1
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (inputStream!=null){
inputStream.close();
}
}
}
}
Java Xiaobaiの学習ノート、質問がある場合はガイドしてください