我正在使用 java,目前,我可以从互联网下载一个文本文件,读取该文件,然后将该文件发送到扫描仪
。是否可以跳过将其写入硬盘并直接将其发送到扫描仪中?我尝试更改一些代码,但没有成功。
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());
它还有一个 constructor taking an input stream 。顺便说一下,它接受 charset as 2nd argument ,如果文本文件的字符编码可能与平台默认编码不同,您可能需要使用它,否则您可能会面临 Mojibake 的风险。 .
Scanner sc = new Scanner(link.openStream(), "UTF-8");
关于java - 将文本文件从 URL 直接发送到扫描仪中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12417292/