[英]Using scanner to read string from text file and then print out
我有一個文本文件,其中包含字符串- "!- =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ==========="
請參見下面的代碼。 如果文本文件包含此字符串,則需要從文本文件中讀取它,然后再次打印出來。 問題是我無法弄清楚為什么我的代碼沒有打印出來。
任何幫助,將不勝感激,謝謝!
public class Main {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("C:/Users/Anton/Pictures/1 x geotransform0.5m shading.txt");
Scanner scan = new Scanner(file);
while(scan.hasNext()){
String str = scan.next();
if(str == "!- =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ==========="){
System.out.print(str);
}
}
scan.close();
}
}
使用以下代碼,掃描儀接下來僅給出一個單詞,使用nextLine代替讀取整行。
Scanner scan = new Scanner(file);
String str1 = scan.nextLine();
if(str1.equals("!- =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ==========="))
System.out.println(str1);
scan.close();
用這個
if(str.equals("!- =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========")){
System.out.print(str);
}
代替
if(str == "!- =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ==========="){
System.out.print(str);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.