正在准备接受java的采访和面试准备我试图将任何网页的内容打印到java中的控制台,我已经尝试了几种方法但是如果你们有任何想法请不要尝试建议。
发布于 2018-10-09 10:43:38
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DisplayPageContentOnConsole {
public static void main(String args[]){
try{
URLConnection connection = new URL("https://www.google.com").openConnection();
InputStream is = connection.getInputStream();
byte buffer[]=new byte[1024];
int length=0;
while((length=is.read(buffer))>0){
System.out.print(new String(buffer,0,length));
}
is.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
If you will pass the path of any file(*http://www.example.com/a.png*) you have to save it properly otherwise it will display string of bytes. If it is normal .html, .txt or any character oriented file.
https://stackoverflow.com/questions/-100002858
复制相似问题