将文本文件从 URL 直接发送到扫描仪



我正在使用java,目前,我可以从互联网上下载一个文本文件,读取该文件,然后将该文件发送到Scanner中。是否可以跳过将其写入硬盘驱动器并将其直接发送到扫描仪?我尝试更改一些代码,但没有用。

URL link = new URL("http://shayconcepts.com/programming/ComicDownloader/version.txt");
ReadableByteChannel rbc = Channels.newChannel(link.openStream());//Gets the html page
FileOutputStream fos = new FileOutputStream("version.txt");//Creates the output name of the output file to be saved to the computer
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
fos.close();
Scanner sc = new Scanner(new FileReader("version.txt"));

是的,这绝对是可能的。就像你说的那样:从 URL 获得的输入流直接输入到扫描仪中。

Scanner sc = new Scanner(link.openStream());

它还有一个接受输入流的构造函数。它顺便接受字符集作为第二个参数,如果文本文件可能采用与平台默认字符不同的字符编码,您可能希望使用它,否则您可能会面临 Mojibake 的风险。

Scanner sc = new Scanner(link.openStream(), "UTF-8");

最新更新