嗨,我想读一个URL,我得到一个字符串,我在控制台上打印该字符串,但我想读取该URL作为一个字节的字节,我没有得到如何读取如何逐字节读取url?
这里是我ReadTextFromURL
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class ReadTextFromURL {
public static void main(String[] args) {
try {
URL url = new URL("http://122.160.81.37:8080/mandim/MarketWise?m=agra");
ByteArrayOutputStream bais = new ByteArrayOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
int lin;
while ((lin = in.read()) != -1) {
System.out.println(lin);
}
in.close();
} catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
}catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
}
}
所需的输出
धान~1325|चावल~2050|ज्वर~920|जौ~810|मकई~1280|गेहूँ~1420|जो~1050|बेजर~-|जय~800
获得输出
2343
2366
2344
126
49
51
50
53
124
2330
2366
2357
2354
126
50
如何获得我想要的输出?
您的预期产出是多少?您当前的产出是多少? –
预计输出是逐字节读取字符串 – user3585120
你尝试过'in.read()'返回一个'int'('byte')吗? –