Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

java - Using scanner to read string from text file and then print out

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.

Any help would be appreciated thanks!

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();
        }   
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...