Java 逐行读取文本文件的最佳方式,将每一行放入代码中

Java 逐行读取文本文件的最佳方式,将每一行放入代码中,java,Java,首先,我只是一个低级的网络程序员,所以对实际编程经验很少。 我已经得到了一个30000个URL的列表,我不会浪费时间点击每一个来检查它们是否有效——有没有办法读取它们所在的文本文件并让程序检查每一行 我目前拥有的代码是java语言,这就是我所知道的全部,如果还有更好的语言,请告诉我。 以下是我到目前为止的情况: public class UrlCheck { public static void main(String[] args) throws IOException {

首先,我只是一个低级的网络程序员,所以对实际编程经验很少。 我已经得到了一个30000个URL的列表,我不会浪费时间点击每一个来检查它们是否有效——有没有办法读取它们所在的文本文件并让程序检查每一行

我目前拥有的代码是java语言,这就是我所知道的全部,如果还有更好的语言,请告诉我。 以下是我到目前为止的情况:

public class UrlCheck {

    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.google.com");
        //Need to change this to make it read from text file
        try {
            InputStream inp = null;
            try {
                inp = url.openStream();
            } catch (UnknownHostException ex) {
                System.out.println("Invalid");
            }
            if (inp != null) {
                System.out.println("Valid");
            }
        } catch (MalformedURLException exc) {
            exc.printStackTrace();
        }
    }
}

您可以使用httpURLConnection。如果它无效,你将得不到任何回报

HttpURLConnection connection = null;
try{         
    URL myurl = new URL("http://www.myURL.com");        
    connection = (HttpURLConnection) myurl.openConnection(); 

    //Set request to header to reduce load 
    connection.setRequestMethod("HEAD");         
    int code = connection.getResponseCode();        
    System.out.println("" + code); 
} catch {
//Handle invalid URL
}

我不确定您的经验,但多线程解决方案在这里是可能的。在阅读文本文件时,将URL存储在线程安全的结构中,并允许多个线程尝试打开这些连接。这将是一个更有效的解决方案,因为当您在中读取30000个URL时,可能需要一段时间来测试它们

如果您不确定,请查看生产者-消费者示例:


首先,使用
BufferedReader
逐行读取文件,并检查每一行。下面的代码应该可以工作。当你遇到一个无效的URL时,你可以决定怎么做。你可以像我展示的那样把它打印出来,也可以写入另一个文件

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.UnknownHostException;

public class UrlCheck {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader("_filename"));
        String line;
        while ((line = br.readLine()) != null) {
           if(checkUrl(line)) {
               System.out.println("URL " + line + " was OK");
           } else {
               System.out.println("URL " + line + " was not VALID"); //handle error as you like
           }
        }

        br.close();
    }

    private static boolean checkUrl(String pUrl) throws IOException {
        URL url = new URL(pUrl);
        //Need to change this to make it read from text file
        try {
            InputStream inp = null;

            try {
                inp = url.openStream();
            } catch (UnknownHostException ex) {
                System.out.println("Invalid");
                return false;
            }
            if (inp != null) {
                System.out.println("Valid");
                return true;
            }
        } catch (MalformedURLException exc) {
            exc.printStackTrace();
            return false;
        }

        return true;
    }
}
checkUrl
方法也可以简化如下

private static boolean checkUrl(String pUrl) {
    URL url = null;
    InputStream inp = null;
    try {
        url = new URL(pUrl);
        inp = url.openStream();

        return inp != null;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (inp != null) {
                inp.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我的问题不是检查url是否有效,而是将30000个url放入当前url所在的位置。如果url有效或不够好,我当前拥有的代码将返回。url url=新url(包含url的字符串);我不是在说“现在有30000个url进入了url所在的位置”这句话,你是什么意思,请你解释清楚,url是如何分布的?您能发布示例
.txt文件吗?
?很高兴它有帮助(如果您满意,您也可以选择它作为正确答案:-))
public class UrlCheck {

    public static void main(String[] args) {
        try {
            URL url = new URL("http://www.google.com");
            //Open the Http connection
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //Get the http response code
            int responceCode = connection.getResponseCode();
            if (responceCode == HttpURLConnection.HTTP_OK) //if the http response code is 200 OK so the url is valid
            {
                System.out.println("Valid");
            } else //Else the url is not valid
            {
                System.out.println("Invalid");
            }
        } catch (MalformedURLException ex) {
            System.out.println("Invalid");
        } catch (IOException ex) {
            System.out.println("Invalid");
        }
    }
}