0
嘗試讀取文本文件的內容時收到錯誤消息「java.lang.ClassCastException:org.openstack4j.openstack.common.DLPayloadEntity incompatible with java.io.File」 。對象存儲 - 讀取文本文件時出錯
如何在不寫入新文件的情況下讀取文本文件?
我試着將文件直接通過使用Apache下議院/ IO庫的「readFiletoString()方法轉換成字符串:
public String test() {
ObjectStorageDAO os;
List<String> containers = null;
String str = null;
try {
os = new ObjectStorageDAO(auth_url, userId, password, project, domainName);
// List the containers
containers = os.containers();
SwiftObject so = os.getObject("MyContainer", "message.txt");
File file = (File) so.download();
str = FileUtils.readFileToString(file, "UTF-8");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
str = e.toString();
}
return str;
}
將該文件作爲InputStream讀取並將其轉換爲String解決了問題。 我將try-block改爲 嘗試{os = new DictionaryDAO(auth_url,userId,password,project,domainName); \t \t \t //列出容器 \t \t \t containers = os.containers(); \t \t \t SwiftObject so = os.getObject(「SpellChecking」,「english.dic」); \t \t \t DLPayload file = so.download(); \t \t \t str = getStringFromInputStream(file.getInputStream()); \t \t \t \t \t \t \t \t} https://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/ –