0

I have an address like: "http://address.com/f1/f2/data.txt"

I'm implementing a Java program that needs to read from the text file, data.txt, preferably line by line, looking for a pattern.

I've searched, what I found and tried was using BufferedReader and Jsoup(I know that using Jsoup doen't make that sense bacause the file is a text file not a html, but I tried it). None of them worked fine and in both I got this error: (No such file or directory)

Here is my code:

public static String getData(String path, String pattern) {
    StringBuilder contentBuilder = new StringBuilder();
    String str ="";
    try {
        BufferedReader in = new BufferedReader(new FileReader(path));
        while ((str = in.readLine()) != null) {
            System.out.println(str);
            if (str.equals(pattern))
                break;
        }
       str = in.readLine();
       in.close();
       } catch (IOException e) {
           System.out.println(e.toString());
       }
    return str;

}

Any clue how to read this file?

Elik
  • 477
  • 3
  • 6
  • 11

2 Answers2

0
    URL url = new URL("http://address.com/f1/f2/data.txt");
    URLConnection yc = url.openConnection();
    InputStreamReader = inputStreamReader = 
        new InputStreamReader(yc.getInputStream());
    BufferedReader in = new BufferedReader(inputStreamReader);
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();
Kamil.H
  • 458
  • 2
  • 8
0

You could do it using below:

URL myurl = new URL(path);
BufferedReader in = new BufferedReader(new InputStreamReader(
                myurl.openStream()));

See this for your reference.

SMA
  • 36,381
  • 8
  • 49
  • 73