我有一个问题:“如何在 Java 中将 File 转换为 String 并再次将该 String 取回 File?”
我的代码:
public static void main(String[]args) throws IOException{
String fff = fileToString("Book.xlsx");
byte[] bytes = fff.getBytes();
File someFile = new File("Book2.xlsx");
FileOutputStream fos = new FileOutputStream(someFile);
fos.write(bytes);
fos.flush();
fos.close();
}
public static String fileToString(String file) {
String result = null;
DataInputStream in = null;
try {
File f = new File(file);
byte[] buffer = new byte[(int) f.length()];
in = new DataInputStream(new FileInputStream(f));
in.readFully(buffer);
result = new String(buffer);
} catch (IOException e) {
throw new RuntimeException("IO problem in fileToString", e);
} finally {
try {
in.close();
} catch (IOException e) { /* ignore it */
}
}
return result;
}
我如何才能Book1.xlsx
在字符串中取回并保存在book2.xlsx
?? Book2.xlsx
一片空白....