首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在java控制台中显示网页内容?

如何在java控制台中显示网页内容?
EN

Stack Overflow用户
提问于 2018-10-09 01:20:39
回答 1查看 0关注 0票数 0

正在准备接受java的采访和面试准备我试图将任何网页的内容打印到java中的控制台,我已经尝试了几种方法但是如果你们有任何想法请不要尝试建议。

EN

回答 1

Stack Overflow用户

发布于 2018-10-09 10:43:38

代码语言:txt
复制
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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档