I am trying to read a URL and then convert it to a string and write the content to a text file, but I get the following exception when I compile the code. Here is my code and my exception:
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" + "\n" + " public static void getText(String url) throws Exception 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 catch (Exception ex) <> > > >
This is my exception:
Exception in thread "main" java.net.MalformedURLException: no protocol: url at java.net.URL.(URL.java:583) at java.net.URL.(URL.java:480) at java.net.URL.(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)
Process finished with exit code 1 What should be done to solve the issue? Thanks for all answers in advance. Update Thank you so much for your answer. I did what you said but I still get the same error.
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.(URL.java:583) at java.net.URL.(URL.java:480) at java.net.URL.(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)