如何將某個文件轉換爲字符串?然後從該字符串獲取原始文件?這需要使用Java來實現它。Java Applet - 將文件轉換爲字符串
這將是代碼示例或教程鏈接。
必須轉換字符串中的整個文件(不是內容)然後將字符串轉換爲文件。
謝謝。
如何將某個文件轉換爲字符串?然後從該字符串獲取原始文件?這需要使用Java來實現它。Java Applet - 將文件轉換爲字符串
這將是代碼示例或教程鏈接。
必須轉換字符串中的整個文件(不是內容)然後將字符串轉換爲文件。
謝謝。
注意:在以下兩種情況下,您都必須數字簽名您的小程序,以便它被授予從文件系統讀取/寫入文件系統的權限!
如果你可以使用外部庫,使用Apache commons-io(你將不得不簽署這些罐子以及)
FileUtils.readFileToString(..)
FileUtils.writeStringToFile(..)
如果你不能,你可以看到this tutorial。
這是爲文本文件 - 但如果你想讀二進制文件(例如.png),你應該而不是閱讀它作爲字符串。相反,它讀作byte[]
和使用commons-codec -
編碼:String base64String = new String(Base64.encodeBase64(byteArray));
解碼:byte[] originalBytes = Base64.decodeBase64(string.getByte());
Base64是用於存儲二進制數據字符串格式。
+1提到小程序必須簽名訪問文件系統 – stacker 2010-01-30 18:11:49
public static String getFileContent(File file) throws IOException
{
FileReader in=new FileReader(file);
StringWriter w= new StringWriter();
char buffer[]=new char[2048];
int n=0;
while((n=in.read(buffer))!=-1)
{
w.write(buffer, 0, n);
}
w.flush();
in.close();
return w.toString();
}
public static void toFileContent(String s,File file) throws IOException
{
FileWriter f=new FileWriter(file);
f.write(s);
f.flush();
f.close();
}
您的示例是否適用於.rar,.jpg,...文件? – Emanuel 2010-01-30 17:58:26
你爲什麼用'applet'標記這個問題。在大多數情況下,您無法從Applet讀取或寫入文件。 – Pierre 2010-01-30 17:34:08
因爲我需要這個applet應用程序。 – Emanuel 2010-01-30 17:42:21
hm,你需要轉換一個文件名 - (到/從)文件或文件_contents_(到/從)文件 – Bozho 2010-01-30 17:46:35