简体   繁体   中英

Java read from certain line from txt file and thread it

So what i'm wanting to do is read from "UsernameList.txt" and somehow make it do something like this

Text in the file:

construct
Hustle
savior
power
Revenge

Read the first line, connect, while it's reading the second line doing the same thing. Is this possible? and how might this be done?

public class main {
    public static void main(String[] args) throws IOException, InterruptedException {
        BufferedReader fileR = new BufferedReader(new FileReader("UsernameList.txt"));

        String line = null;
        while ((line = fileR.readLine()) != null) {
            Document doc = Jsoup.connect("https://twitter.com/" + line).get();
            doc.html();
        }
    }
}

You can do something like this.

public class Tester implements Runnable {

    String line = null;

    public static void main(String[] args) throws IOException {
        BufferedReader fileR = new BufferedReader(new FileReader("UsernameList.txt"));
        String line = null;
        while ((line = fileR.readLine()) != null) {
            Tester tester = new Tester();
            tester.line = line;
            Thread thread = new Thread(tester);
            thread.start();
        }

    }

    @Override
    public void run() {
        Document doc = Jsoup.connect("https://twitter.com/" + line).get();
        doc.html();
    }
}

You can have threads that implement Callable, which accepts the keywords connects and responds with required data. Also you can use Executor framework to control the the number of thread running at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM