[英]how to remove '?' sign in the end of file read in java
我正在學習文件輸入輸出,並且我有問題。 a.txt有一些文本,當它讀取並打印世界時,在末尾有“?”標記。.如何將其刪除。
import java.io.*;
class fRead{
public static void main(String args[])throws IOException
{
FileInputStream fin=new FileInputStream("a.txt");
int i=0;
while(i!=-1){
i=fin.read();
System.out.print((char)i);
}
fin.close();
}
}
CMD中的結果:-G:\\ file> javac fRead.java
G:\\ file> java fRead
VINAYAK計算機學院?
***'?'***末尾不在文件末尾。
發生此問題,因為您打印-1
,你從一開始read()
將其轉換為后char
,即使循環應該停止時,沒有任何印刷-1
被返回。
您可以通過將作業與否定項的檢查組合來解決此問題:
while ((i = fin.read()) != -1) {
System.out.print((char)i);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.