本文介绍了使用扫描仪从文本文件中读取字符串,然后打印出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本文件,其中包含字符串-"!- =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ==========="
,如下面的代码所示.如果文本文件包含此字符串,则需要从文本文件中读取它,然后再次打印出来.问题是我无法弄清楚为什么我的代码没有打印出来.
I have a text file which has the string, - "!- =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ==========="
in it as you can see in the code below. IF the text file contains this string I need to read it from the text file and then print it out again. The problem is I cant work out why my code is not printing it.
任何帮助将不胜感激!
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代替读取整行.
use below code, scanner next gives just a word use nextLine instead to read whole line..
Scanner scan = new Scanner(file);
String str1 = scan.nextLine();
if(str1.equals("!- =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ==========="))
System.out.println(str1);
scan.close();
这篇关于使用扫描仪从文本文件中读取字符串,然后打印出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!