要使用InputStreamReader读取网络资源,可以按照以下步骤进行:
URL url = new URL("http://www.example.com/resource.txt");
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
StringBuilder content = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
content.append(line);
}
bufferedReader.close();
inputStream.close();
通过以上步骤,您可以使用InputStreamReader读取网络资源的内容并保存到一个String变量中。您也可以根据需要进行其他处理。