Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Stream cannot be resolved to a type

I'm trying to write a script that looks at all the files in a given directory, takes each of their paths, and prints it to screen. Whenever I run my program it says stream cannot be resolved to a type. I know the error has something to do with my variable type but I do not know what to change my variable type to. Any help is appreciated.

My Code:

import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.stream.Stream;

class FileFinder{
    

    public void getFiles(){
        Path path = Paths.get("C:/Users/MYNAME/Documents/TestFiles");
        
        Stream<Path> subPaths = Files.walk(path);
        subPaths.forEach(System.out::println);

    }
}

Error Message:

Exception in thread "getFiles" java.lang.Error: Unresolved compilation problem: 
        Stream cannot be resolved to a type

        at FileFinder.getFiles(FileFinder.java:12)
question from:https://stackoverflow.com/questions/65516657/stream-cannot-be-resolved-to-a-type

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

1 Reply

0 votes
by (71.8m points)

Wrong Stream

You have an import for java.nio.stream.Stream. But you need an import for java.util.stream.Stream.

The Javadoc for Files.walk tells us the method returns a java.util.stream.Stream<Path> object.

import java.util.stream.Stream;

Closing I/O streams

By the way, that Stream object implements AutoCloseable. So you can use try-with-resources syntax to make sure it is closed properly, one way or another.

While closing some kinds of streams may be unnecessary, ones involving I/O should be closed. I assume this includes your Files.walk stream.

To quote the Stream interface Javadoc:

Most stream instances do not actually need to be closed after use, as they are backed by collections, arrays, or generating functions, which require no special resource management. Generally, only streams whose source is an IO channel, such as those returned by Files.lines(Path), will require closing. If a stream does require closing, it must be opened as a resource within a try-with-resources statement or similar control structure to ensure that it is closed promptly after its operations have completed.

Basic code sample

        Path path = Paths.get( "/Users/basilbourque/stuff" );
        try (
                Stream < Path > subPaths = Files.walk( path ) ;
        )
        {
            subPaths.forEach( System.out :: println );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }

Full sample code app

Here is a complete app, including the import statements.

package work.basil.example;

import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
import java.util.stream.Stream;

public class FileFinder
{
    public void walkFiles ( )
    {
        Path path = Paths.get( "/Users/basilbourque/stuff" );
        try (
                Stream < Path > subPaths = Files.walk( path ) ;
        )
        {
            subPaths.forEach( System.out :: println );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
    }

    public static void main ( String[] args )
    {
        FileFinder app = new FileFinder();
        app.demo();
    }

    private void demo ( )
    {
        this.walkFiles();
    }
}

When run:

/Users/basilbourque/stuff
/Users/basilbourque/stuff/some_text.txt
/Users/basilbourque/stuff/slow-draft.txt
/Users/basilbourque/stuff/text.txt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...