bufferedreader输入流读取器更改...?

时间:2014-03-26 21:57:47

标签: java bufferedreader

URL u = new URL(url);
String expected = "";
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader r= new InputStreamReader(in);

所以这里是我的代码,我想要一个很小的帮助,上面是从网址获取内容,但现在我想使用相同的代码从文件中读取我需要在上面的代码中更改的内容.. ..i意味着应该有一些东西需要在uc.getInputStream()的位置改变...那么那是什么

InputStream in = new BufferedInputStream(uc.getInputStream());

1 个答案:

答案 0 :(得分:0)

查看课程FileInputStream

您只需使用该代码并以类似方式执行此操作即可。

InputStream in = new FileInputStream(new File("C:/temp/test.txt"));
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
        System.out.println(out.toString());   //Prints the string content read from input stream
        reader.close();