这些用户输入读取方式有何不同? [英] What is the difference in these ways to read in user-input?

查看:65
本文介绍了这些用户输入读取方式有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个主题如何获取java的基本用户输入,尽管这个特定问题的答案已经足够,我想知道为什么有这么多不同的方式来读取用户输入。 特别是这些不同的用户输入读取方法的优缺点是什么?什么时候使用其他方式才有意义?

I came across this topic How to get basic user input for java and although the answer for this particular question was sufficient, I wondered why there are so many different ways to read in user-input. In particular what are the pros and cons of these different ways to read in user-input? When would it make sense to use one over the other?

这些帖子中提到的可能方式。

These where the possible ways mentioned in that post.



  1. 扫描仪类

  2. BufferedReader和InputStreamReader类

  3. DataInputStream类

  4. 控制台类

  1. Scanner class
  2. BufferedReader and InputStreamReader classes
  3. DataInputStream class
  4. Console class



推荐答案

它们都打算用于不同的事情。我会尽力解释,但我并不完全理解文档而且我没有广泛使用这些文档,所以如果你发现任何错误,请告诉我。

They're all intended for use in different things. I'll do my best to explain, but I didn't totally understand the docs and I haven't used each of these extensively, so if you spot any mistakes please let me know.


  1. Scanner 类用于处理几乎任何流的输入,并为开发人员提供一种简单易用的方法管理它而不学习太多课程。这是相当基本的,但大部分时间,它足够 - 例如,如果你只是想以简单易学的方式获得用户输入,扫描仪是你想要什么。

  1. The Scanner class is meant to process input from just about any stream and give the developer a nice, easy way to manage it without learning too many classes. It's fairly basic, but most of the time, it's plenty enough -- for example, if you just want to get user input in a simple-to-learn way, Scanner is what you want.

BufferedReader 类是为了快速读取文件,代价是记忆。它的意思是包装其他更简单的类,如 FileInputStream (技术上,扩展InputStream 的任何类),以及通过缓冲接下来的几个字节并从该数组中读取而不是直接从流中读取,并且仅在流到达缓冲区末尾时从流中读取。如果你对速度比低内存使用更感兴趣,你会想要这个 - 虽然除非你明确告诉它,否则它确实不会使用更多的内存。

The BufferedReader class is meant to read from files quickly, at the expense of memory. It's meant to be a wrapper around other, simpler classes like FileInputStream (technically, any class which extends InputStream) and, by buffering the next few bytes and reading from that array instead of directly the stream, and only reading from the stream when it reaches the end of the buffer. If you're more interested in speed than low memory usage, you'll want this -- though it really doesn't use much more memory unless you explicitly tell it to.

DataInputStream 是一个更通用的数据输入类。如果您只想从流中读取基元 - 而不是直接读取各个位并组成相应的类型,这为您提供了一种简单的方法。 此页是文档说去的地方有关方法实际工作方式的更多信息。

DataInputStream is a more general input class for data. It's if you just want to read primitives from a stream -- rather than reading individual bits directly and composing the respective types, this provides a simple method to do that for you. This page is where the docs say to go for more information about how the methods actually work.

控制台类明确用于 c ommand l ine i nterface(CLI)程序 - 即用于从控制台读取数据和向控制台写入数据。这就像 Scanner - 一个简单易学的课程,用于完成基本任务。

The Console class is explicitly for a command line interface (CLI) program -- that is, for reading data from and writing data to a console. It's like Scanner -- a simple, easy-to-learn class for doing basic tasks.

在一个句子中,它们用于不同的任务,你使用哪一个应该由你想要做的事情决定。

In a sentence, they're meant for different tasks, and which one you use should be determined by what you're trying to do.

这篇关于这些用户输入读取方式有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆