我编写了一个简单的程序来解码我拥有的 Base64 编码字符串。我使用 eclipse 搜索了一种方法来执行此操作,并发现方法 javax.xml.bind.DatatypeConverter.parseBase64Binary(String s)
正是这样做的。我发现当我使用该方法的完整位置时,程序运行良好:
public static void main(String args[]) {
String s = "cGFzc3dvcmQ6IGlsb3ZlbXlzZWxmISEx";
byte[] converted = javax.xml.bind.DatatypeConverter.parseBase64Binary(s);
System.out.println(new String(converted));
}
但由于某种原因,当我尝试导入位置时,Eclipse 给了我一个错误:
导入:
import javax.xml.bind.DatatypeConverter.*;
第一个代码中的新行 3:
byte[] converted = javax.xml.bind.DatatypeConverter.parseBase64Binary(s);
我在新行 3 上遇到错误:
The method parseBase64Binary(String) is undefined for the type **name of class**
我很乐意提供解释。
最佳答案
您需要进行静态
导入:
import static javax.xml.bind.DatatypeConverter.*;
或
import static javax.xml.bind.DatatypeConverter.parseBase64Binary;
然后您将能够执行以下操作:
byte[] converted = parseBase64Binary(s);
更多信息:
关于java - 为什么我不能从方法声明中单独导入?(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28696224/