编写一个java程序来读取文本文件而不使用程序中的任何import语句

时间:2020-12-04 20:46:51

This was the question asked in one of the interview I have to write a program but there should not be any import statements in the program as i need to import java.io.* package. Is this program more efficient without using import statements?

这是我在编写一个程序时遇到的一个问题,但程序中不应该有任何import语句,因为我需要导入java.io. *包。如果不使用import语句,这个程序是否更有效?

6 个解决方案

#1


1  

It is not more efficient. I guess it is just Guys tried to figure out whether you understand what goes under import or not.

效率不高。我想这只是Guys试图弄清楚你是否了解进口的内容。

There is a difference in "imports" in C++ and Java.
And it seams that they just check your awareness of it.

C ++和Java中的“imports”有所不同。接缝他们只是检查你的意识。

#2


1  

If you explicitly import on the beginning of the source-file or explicitly qualify the class-use (java.io.Writer input = new java.io.Writer ...) makes no difference as the compiler creates the bytecode.

如果您在源文件的开头显式导入或显式限定类使用(java.io.Writer input = new java.io.Writer ...),则编译器创建字节码时没有任何区别。

#3


1  

This is a silly question. You will have to use the fully qualified names in your source code. I don't think that this makes any difference than using imports.

这是一个愚蠢的问题。您必须在源代码中使用完全限定名称。我认为这与使用导入没有任何区别。

The question would had a meaning, if you were asked to do a task that can be implemented only with java.lang classes. But reading a file is not such a task.

如果要求您执行只能使用java.lang类实现的任务,那么这个问题就有了意义。但是阅读文件并不是一项任务。

#4


1  

Import statements don't affect run-time efficiency.

导入语句不会影响运行时效率。

But it is an interesting question. I assume you can do this in a platform-dependent way. For example:

但这是一个有趣的问题。我假设你可以以平台相关的方式做到这一点。例如:

Use java.lang.Runtime to execute (exec) a native command (like cat) and capture its output, using process.getInputStream().read(..) (this uses InputStream, but doesn't import it)

使用java.lang.Runtime执行(exec)本机命令(如cat)并使用process.getInputStream()捕获其输出.read(..)(这使用InputStream,但不导入它)

Another option, which I assume is not the point of the question, because it will be a dumb question then, is to use the fully-qualified names of the java.io. classes in your code.

另一种选择,我认为这不是问题的关键,因为那将是一个愚蠢的问题,是使用java.io的完全限定名称。代码中的类。

#5


0  

One possible way:

一种可能的方式:

Use java.lang.Runtime to call OS commands to read the file.

使用java.lang.Runtime调用OS命令来读取文件。

#6


0  

yes you can use fully qualified name. as the compiler will covert your import statements to fully qualified names.

是的,你可以使用完全限定的名称。因为编译器会将您的import语句转换为完全限定名。

#1


1  

It is not more efficient. I guess it is just Guys tried to figure out whether you understand what goes under import or not.

效率不高。我想这只是Guys试图弄清楚你是否了解进口的内容。

There is a difference in "imports" in C++ and Java.
And it seams that they just check your awareness of it.

C ++和Java中的“imports”有所不同。接缝他们只是检查你的意识。

#2


1  

If you explicitly import on the beginning of the source-file or explicitly qualify the class-use (java.io.Writer input = new java.io.Writer ...) makes no difference as the compiler creates the bytecode.

如果您在源文件的开头显式导入或显式限定类使用(java.io.Writer input = new java.io.Writer ...),则编译器创建字节码时没有任何区别。

#3


1  

This is a silly question. You will have to use the fully qualified names in your source code. I don't think that this makes any difference than using imports.

这是一个愚蠢的问题。您必须在源代码中使用完全限定名称。我认为这与使用导入没有任何区别。

The question would had a meaning, if you were asked to do a task that can be implemented only with java.lang classes. But reading a file is not such a task.

如果要求您执行只能使用java.lang类实现的任务,那么这个问题就有了意义。但是阅读文件并不是一项任务。

#4


1  

Import statements don't affect run-time efficiency.

导入语句不会影响运行时效率。

But it is an interesting question. I assume you can do this in a platform-dependent way. For example:

但这是一个有趣的问题。我假设你可以以平台相关的方式做到这一点。例如:

Use java.lang.Runtime to execute (exec) a native command (like cat) and capture its output, using process.getInputStream().read(..) (this uses InputStream, but doesn't import it)

使用java.lang.Runtime执行(exec)本机命令(如cat)并使用process.getInputStream()捕获其输出.read(..)(这使用InputStream,但不导入它)

Another option, which I assume is not the point of the question, because it will be a dumb question then, is to use the fully-qualified names of the java.io. classes in your code.

另一种选择,我认为这不是问题的关键,因为那将是一个愚蠢的问题,是使用java.io的完全限定名称。代码中的类。

#5


0  

One possible way:

一种可能的方式:

Use java.lang.Runtime to call OS commands to read the file.

使用java.lang.Runtime调用OS命令来读取文件。

#6


0  

yes you can use fully qualified name. as the compiler will covert your import statements to fully qualified names.

是的,你可以使用完全限定的名称。因为编译器会将您的import语句转换为完全限定名。