這是我的代碼。我正在從名為:words.txt”的檔案中列印只有 5 個字母的單詞,我正在使用 5 個字母的單詞,并創建一個名為:“five.txt”的新檔案。但是當我打開檔案時,這些單詞是不在那里 這是我的代碼:
public static void main(String[] args) throws IOException
{
try (Scanner in = new Scanner("words.txt");
PrintWriter out = new PrintWriter("five.txt");)
{
while (in.hasNext())
{
String word;
word = in.next();
if (word.length() == 5)
out.println(word);
}
}
catch (IOException e)
{
System.out.println("Could not read the words: " e.getMessage());
return; // Exit main now
}
}
uj5u.com熱心網友回復:
new Scanner("words.txt")
這并不像你認為的那樣。這將為輸入“words.txt”制作掃描儀。不是,'檔案“words.txt”的內容'。不,從字面上看,'words.txt'。其中有一個標記,長度為 9,即“words.txt”。
你想要new Scanner(new File("words.txt"))
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/513621.html
標籤:爪哇蚀方法