读取URL时没有协议异常

时间:2014-06-11 05:37:48

标签: java

我正在尝试读取一个URL,然后将其转换为字符串并将内容写入文本文件,但在编译代码时出现以下异常。这是我的代码和我的例外:

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class Main {
public  static  String url = "google.com";
public  static  String fileName= null;
public  static  String fileConttent ="Something";

public static void main(String[] args) throws Exception {
      getText(new String(url));
}

public static void getText(String url) throws Exception {
    URL website = new URL("url\n" +
            "    public static void main(String[] args) throws Exception {\n" +
            "          getText(new String(url));\n" +
            "    }\n" +
            "\n" +
            "    public static void getText(String url) throws Exception {\n" +
            "        URL website = new URL(\"url");
    URLConnection connection = website.openConnection();
    BufferedReader in = new BufferedReader(
            new InputStreamReader(
                    connection.getInputStream()));

    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null)
        response.append(inputLine);

    in.close();

    String toBeWritten = response.toString();
    System.out.println(toBeWritten);
}




public static void createFile(String fileName,String fileContent){
    Writer writer = null;

    try {
        writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("C:\\Users\\Dell\\Documents\\t"+"fileName"), "utf-8"));
        writer.write(fileContent);
    } catch (IOException ex) {
        // report
    } finally {
        try {writer.close();} catch (Exception ex) {}
    }

}



}

这是我的例外:

    Exception in thread "main" java.net.MalformedURLException: no protocol: url
at java.net.URL.<init>(URL.java:583)
at java.net.URL.<init>(URL.java:480)
at java.net.URL.<init>(URL.java:429)
at Main.getText(Main.java:15)
at Main.main(Main.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

使用退出代码1完成处理

应该怎样解决这个问题?提前感谢所有答案。

更新

非常感谢您的回答。我做了你说的但我仍然得到同样的错误。

 Exception in thread "main" java.net.MalformedURLException: no protocol: url
public static void main(String[] args) throws Exception {
      getText(new String(url));
}

public static void getText(String url) throws Exception {
    URL website = new URL("url
at java.net.URL.<init>(URL.java:583)
at java.net.URL.<init>(URL.java:480)
at java.net.URL.<init>(URL.java:429)
at Main.getText(Main.java:15)
at Main.main(Main.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

使用退出代码1完成处理

3 个答案:

答案 0 :(得分:2)

public static String url = "google.com";

这是一个域,而不是一个URL(或一个名为“google.com”的文件的相对URL,但让我们不去那里)。 URL具有协议,例如HTTP。

答案 1 :(得分:0)

您需要将协议添加到您的网址:

public  static  String url = "http://google.com";

你必须完成你的代码(结果应该是什么):

URL website = new URL("url\n" +
            "    public static void main(String[] args) throws Exception {\n" +
            "          getText(new String(url));\n" +
            "    }\n" +
            "\n" +
            "    public static void getText(String url) throws Exception {\n" +
            "        URL website = new URL(\"url");

URL website = new URL(url);

答案 2 :(得分:0)

将您的代码更改为:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Main {
 public static String url = "https://www.google.com";
 public static String fileName = null;
 public static String fileConttent = "Something";

 public static void main(String[] args) throws Exception {
  getText(new String(url));
 }

 public static void getText(String url) throws Exception {
  URL website = new URL(url);
  URLConnection connection = website.openConnection();
  BufferedReader in = new BufferedReader(new InputStreamReader(
    connection.getInputStream()));

  StringBuilder response = new StringBuilder();
  String inputLine;

  while ((inputLine = in.readLine()) != null)
   response.append(inputLine);

  in.close();

  String toBeWritten = response.toString();
  System.out.println(toBeWritten);
 }
}