知道了doc文档的二进制流字符串后,如何还原为一份doc文档? 我取得了doc文档的二进制流字符串后,如何还原为一份doc文档?请大家给个思路或demo?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 获得了一个输入流,然后可以读取这个流的每个字节,用输出流循环写入到一个文件中参考代码比如你现在取得的DOC的流是 in//创建一个字节输出流BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("test.doc"));int b = 0;while((b = in.read()) != -1){ //循环读取in对象的字节 out.write(b); //写入到文件}in.close();out.close(); //好习惯 xiaoyuepk的方法我明白,不过我不是想要这个来的。我再描述一下我的问题。先找一份doc文档用ultraedit打开,我现在有中间部分的十六进制码字符串,我如何根据这个字符串生成回doc文档呢?或如何通过程序得到这个十六进制码呢? /* * Created on Sep 9, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */package com.jerrymouse.io.demo;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;/** * @author oyxz * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */public class Convert { public static void main(String[] args) throws Exception { //将一份文档转化为十六进制流的形式并写入到一个文本文件中 String fileName = "dat//demo.txt"; String strHex = file2Hex(fileName); //System.out.println(strHex); String outFileName = "dat//demo.out"; writeFileBuffer(outFileName, strHex); //将十六进制的字符串流还原为原本的文档 String str = readFileBuffer(outFileName); char[] c = str2CharArray(str); String newFileName = fileName.substring(0, fileName.indexOf('.')) + "_1.txt"; //System.out.println(newFileName); hex2File(c, newFileName); } private static char[] str2CharArray(String str) { int length = 0; if (str.length() % 2 == 0) length = str.length() / 2; else length = str.length() / 2 + 1; char[] c = new char[length]; String s = ""; int j = 0; for (int i = 0; i < str.length(); i += 2) { if ((i + 2) < str.length()) { s = str.substring(i, i + 2); } else { s = str.substring(i, str.length()); } //System.out.println(s); if (!s.equals("") && !s.equals("##")) c[j++] = (char) Integer.parseInt(s, 16); //System.out.println(c[j - 1]); } return c; } public static void hex2File(char[] c, String szFileName) throws Exception { BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(szFileName)); for (int i = 0; i < c.length; i++) { out.write(c[i]); //System.out.println(c[i]); } out.close(); } public static String file2Hex(String strFileName) throws Exception { StringBuffer result = new StringBuffer(); BufferedInputStream in = new BufferedInputStream(new FileInputStream( new File(strFileName))); int b = 0; while ((b = in.read()) != -1) { if (b == 0 || b == 13 || b == 10 || b == 9) { //空字符,换行符,回车符,TAB符 //System.out.println(b); result.append("0").append(Integer.toHexString(b).toUpperCase()); } else { result.append(Integer.toHexString(b).toUpperCase()); //System.out.println(Integer.toHexString(b).toUpperCase()); } } in.close(); return result.toString(); } public static String readFileBuffer(String szFileName) { String sz_FileContent = ""; BufferedInputStream bis = null; File file = null; try { file = new File(szFileName); bis = new BufferedInputStream(new FileInputStream(file)); //byte[] buff = new byte[310455]; //bis.read(buff, 0, 310455); //sz_FileContent += new String(buff); byte[] buff; int n = (int) file.length(); if (n < 2048) { buff = new byte[n]; } else { buff = new byte[2048]; } int bytesRead = 0; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { sz_FileContent += new String(buff); } //去掉一些无用字符 sz_FileContent = sz_FileContent.replaceAll("\r\n\t\t\t", "##"); sz_FileContent.trim(); //System.out.println(sz_FileContent); } catch (Exception e) { } finally { try { if (bis != null) bis.close(); } catch (Exception e) { } } return sz_FileContent; } public static void writeFileBuffer(String szFileName, String content) { try { File file = new File(szFileName); DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(file))); dos.writeBytes(content); dos.close(); } catch (Exception e) { } }} 以上实现为我写的将一份文件转成十六进制流,然后反向转回来的代码。在*.txt文件中测试通过,但是将*.doc文档转成十六进制流后,再转回来后,doc文档是坏的。请大家帮忙看看是什么问题。 /* * Created on Sep 9, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */package com.jerrymouse.io.demo;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;/** * @author oyxz * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */public class Convert { public static void main(String[] args) throws Exception { //将一份文档转化为十六进制流的形式并写入到一个文本文件中 String fileName = "dat//oa4.doc"; Convert convert = new Convert(); //String strHex = convert.file2Hex(fileName); //System.out.println(strHex); String outFileName = "dat//oa4.out"; //convert.writeFileBuffer(outFileName, strHex); //将十六进制的字符串流还原为原本的文档 String str = convert.readFileBuffer(outFileName); byte[] bytes = convert.str2ByteArray(str); //char[] c = convert.str2CharArray(str); String newFileName = fileName.substring(0, fileName.indexOf('.')) + "_1.doc"; convert.hex2File(bytes, newFileName); //convert.hex2File(c, newFileName); } private char[] str2CharArray(String str) { int length = 0; if (str.length() % 2 == 0) length = str.length() / 2; else length = str.length() / 2 + 1; char[] c = new char[length]; String s = ""; int j = 0; for (int i = 0; i < str.length(); i += 2) { s = str.substring(i, i + 2); c[j++] = (char) Integer.parseInt(s, 16); } return c; } private byte[] str2ByteArray(String str) { //注意:此方法和getBytes的不同之处是,这里需要两个字符转成一个字节码。 int length = 0; if (str.length() % 2 == 0) length = str.length() / 2; else length = str.length() / 2 + 1; byte[] result = new byte[length]; String s = ""; int j = 0; for (int i = 0; i < str.length(); i += 2) { if ((i + 2) < str.length()) { s = str.substring(i, i + 2); } else { s = str.substring(i, str.length()); } //System.out.println(s); if (!s.equals("") && !s.equals("##")) { //System.out.println(Integer.parseInt(s, 16)); byte b = new Integer(Integer.parseInt(s, 16)).byteValue(); result[j++] = b; //System.out.println(result[j - 1]); } } return result; } public void hex2File(byte[] bytes, String szFileName) throws Exception { ByteArrayInputStream bis = new ByteArrayInputStream(bytes); byte[] buffer = new byte[bytes.length]; FileOutputStream fos = new FileOutputStream(szFileName); int bytesum = 0; int byteread = 0; while ((byteread = bis.read(buffer)) != -1) { bytesum += byteread; fos.write(buffer, 0, byteread); } fos.close(); bis.close(); } private void hex2File(char[] c, String szFileName) throws Exception { BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(szFileName)); for (int i = 0; i < c.length; i++) { out.write(c[i]); } out.close(); } public void hex2File(String str, String szFileName) throws Exception { byte[] bytes = str2ByteArray(str); hex2File(bytes, szFileName); } public String file2Hex(String strFileName) throws Exception { StringBuffer result = new StringBuffer(); BufferedInputStream in = new BufferedInputStream(new FileInputStream( strFileName)); byte[] bytes = new byte[in.available()]; in.read(bytes); result.append(toHexString2(bytes)); //System.out.println(result.toString()); in.close(); return result.toString(); } public String readFileBuffer(String szFileName) { String sz_FileContent = ""; File file = new File(szFileName); sz_FileContent = readFileBuffer(szFileName, (int) file.length()); return sz_FileContent; } public String readFileBuffer(String szFileName, int size) { String sz_FileContent = ""; BufferedInputStream bis = null; File file = null; try { file = new File(szFileName); bis = new BufferedInputStream(new FileInputStream(file)); byte[] buff = new byte[size]; bis.read(buff, 0, size); sz_FileContent += new String(buff); //去掉一些输出时的格式无用字符 sz_FileContent = sz_FileContent.replaceAll("\r\n\t\t\t", ""); //System.out.println(sz_FileContent); } catch (Exception e) { } finally { try { if (bis != null) bis.close(); } catch (Exception e) { } } return sz_FileContent; } public void writeFileBuffer(String szFileName, String content) { try { File file = new File(szFileName); DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(file))); dos.writeBytes(content); dos.close(); } catch (Exception e) { } } private String toHexString1(byte[] b) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < b.length; ++i) { buffer.append(toHexString1(b[i])); } return buffer.toString(); } private String toHexString1(byte b) { String s = Integer.toHexString(b & 0xFF); if (s.length() == 1) { return "0" + s; } else { return s; } } private String toHexString2(byte[] b) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < b.length; ++i) { buffer.append(toHexString2(b[i])); } return buffer.toString(); } private String toHexString2(byte b) { char[] buffer = new char[2]; buffer[0] = Character.forDigit((b >>> 4) & 0x0F, 16); buffer[1] = Character.forDigit(b & 0x0F, 16); return new String(buffer); } private String toHexString3(byte[] bytes) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < bytes.length; ++i) { buffer.append(toHexString3(bytes[i])); } return buffer.toString(); } private String toHexString3(byte b) { int n = new Integer(b).intValue(); String s = ""; if (n >= 0) { s = Integer.toHexString(n); if (s.length() == 1) { s = "0" + s; } } else { s = Integer.toHexString(n).substring(6, 8); } return s; }}通过到网上查找相关资料,我已经解决了此问题,现将程序发布到此,和大家共享一下。同时多谢各位的帮助和提示。 谁能帮我看几行代码啊??谢谢了! 关于数据库操作的问题,求解 JAVA新手想找一位师傅 窗体关闭确认的实现问题 关于调用的问题 怎样能使程序自动填写网页表格并提交任务? JPanel动态添加JLabel不显示的问题 请教如何用程序读取另一台机器上的文件! RMI的实现?谢谢各位! 大家给我个建议,看我该不该离开这家单位? 请问有没有接受过 北大青鸟 或niit技术培训的,请说一下感受好嘛!我想去接受培训,但是不知那个号!!谢谢啦!!! 执行System.exit(0)操作后 程序没有完全关闭
参考代码比如你现在取得的DOC的流是 in//创建一个字节输出流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("test.doc"));
int b = 0;
while((b = in.read()) != -1){ //循环读取in对象的字节
out.write(b); //写入到文件
}
in.close();
out.close(); //好习惯
* Created on Sep 9, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.jerrymouse.io.demo;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;/**
* @author oyxz
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class Convert { public static void main(String[] args) throws Exception {
//将一份文档转化为十六进制流的形式并写入到一个文本文件中
String fileName = "dat//demo.txt"; String strHex = file2Hex(fileName);
//System.out.println(strHex); String outFileName = "dat//demo.out";
writeFileBuffer(outFileName, strHex); //将十六进制的字符串流还原为原本的文档
String str = readFileBuffer(outFileName);
char[] c = str2CharArray(str); String newFileName = fileName.substring(0, fileName.indexOf('.'))
+ "_1.txt";
//System.out.println(newFileName); hex2File(c, newFileName);
} private static char[] str2CharArray(String str) {
int length = 0;
if (str.length() % 2 == 0)
length = str.length() / 2;
else
length = str.length() / 2 + 1;
char[] c = new char[length];
String s = "";
int j = 0;
for (int i = 0; i < str.length(); i += 2) {
if ((i + 2) < str.length()) {
s = str.substring(i, i + 2);
} else {
s = str.substring(i, str.length());
}
//System.out.println(s);
if (!s.equals("") && !s.equals("##"))
c[j++] = (char) Integer.parseInt(s, 16);
//System.out.println(c[j - 1]);
}
return c;
} public static void hex2File(char[] c, String szFileName) throws Exception {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(szFileName));
for (int i = 0; i < c.length; i++) {
out.write(c[i]);
//System.out.println(c[i]);
}
out.close();
} public static String file2Hex(String strFileName) throws Exception {
StringBuffer result = new StringBuffer();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
new File(strFileName)));
int b = 0;
while ((b = in.read()) != -1) {
if (b == 0 || b == 13 || b == 10 || b == 9) { //空字符,换行符,回车符,TAB符
//System.out.println(b);
result.append("0").append(Integer.toHexString(b).toUpperCase());
} else {
result.append(Integer.toHexString(b).toUpperCase());
//System.out.println(Integer.toHexString(b).toUpperCase());
}
}
in.close();
return result.toString();
} public static String readFileBuffer(String szFileName) {
String sz_FileContent = "";
BufferedInputStream bis = null;
File file = null;
try {
file = new File(szFileName);
bis = new BufferedInputStream(new FileInputStream(file)); //byte[] buff = new byte[310455];
//bis.read(buff, 0, 310455);
//sz_FileContent += new String(buff); byte[] buff;
int n = (int) file.length();
if (n < 2048) {
buff = new byte[n];
} else {
buff = new byte[2048];
} int bytesRead = 0;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
sz_FileContent += new String(buff);
}
//去掉一些无用字符
sz_FileContent = sz_FileContent.replaceAll("\r\n\t\t\t", "##");
sz_FileContent.trim();
//System.out.println(sz_FileContent);
} catch (Exception e) {
} finally {
try {
if (bis != null)
bis.close();
} catch (Exception e) {
}
}
return sz_FileContent;
} public static void writeFileBuffer(String szFileName, String content) {
try {
File file = new File(szFileName);
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(file)));
dos.writeBytes(content);
dos.close();
} catch (Exception e) {
}
}
}
* Created on Sep 9, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.jerrymouse.io.demo;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;/**
* @author oyxz
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class Convert { public static void main(String[] args) throws Exception {
//将一份文档转化为十六进制流的形式并写入到一个文本文件中
String fileName = "dat//oa4.doc";
Convert convert = new Convert();
//String strHex = convert.file2Hex(fileName);
//System.out.println(strHex); String outFileName = "dat//oa4.out";
//convert.writeFileBuffer(outFileName, strHex); //将十六进制的字符串流还原为原本的文档
String str = convert.readFileBuffer(outFileName); byte[] bytes = convert.str2ByteArray(str);
//char[] c = convert.str2CharArray(str);
String newFileName = fileName.substring(0, fileName.indexOf('.'))
+ "_1.doc"; convert.hex2File(bytes, newFileName);
//convert.hex2File(c, newFileName);
} private char[] str2CharArray(String str) {
int length = 0;
if (str.length() % 2 == 0)
length = str.length() / 2;
else
length = str.length() / 2 + 1;
char[] c = new char[length];
String s = "";
int j = 0;
for (int i = 0; i < str.length(); i += 2) {
s = str.substring(i, i + 2);
c[j++] = (char) Integer.parseInt(s, 16);
}
return c;
} private byte[] str2ByteArray(String str) {
//注意:此方法和getBytes的不同之处是,这里需要两个字符转成一个字节码。
int length = 0;
if (str.length() % 2 == 0)
length = str.length() / 2;
else
length = str.length() / 2 + 1;
byte[] result = new byte[length];
String s = "";
int j = 0;
for (int i = 0; i < str.length(); i += 2) {
if ((i + 2) < str.length()) {
s = str.substring(i, i + 2);
} else {
s = str.substring(i, str.length());
}
//System.out.println(s);
if (!s.equals("") && !s.equals("##")) {
//System.out.println(Integer.parseInt(s, 16));
byte b = new Integer(Integer.parseInt(s, 16)).byteValue();
result[j++] = b;
//System.out.println(result[j - 1]);
}
}
return result;
} public void hex2File(byte[] bytes, String szFileName) throws Exception {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
byte[] buffer = new byte[bytes.length]; FileOutputStream fos = new FileOutputStream(szFileName);
int bytesum = 0;
int byteread = 0;
while ((byteread = bis.read(buffer)) != -1) {
bytesum += byteread;
fos.write(buffer, 0, byteread);
}
fos.close();
bis.close();
} private void hex2File(char[] c, String szFileName) throws Exception {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(szFileName));
for (int i = 0; i < c.length; i++) {
out.write(c[i]);
}
out.close();
} public void hex2File(String str, String szFileName) throws Exception {
byte[] bytes = str2ByteArray(str);
hex2File(bytes, szFileName);
} public String file2Hex(String strFileName) throws Exception {
StringBuffer result = new StringBuffer();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
strFileName));
byte[] bytes = new byte[in.available()];
in.read(bytes); result.append(toHexString2(bytes)); //System.out.println(result.toString());
in.close(); return result.toString();
} public String readFileBuffer(String szFileName) {
String sz_FileContent = "";
File file = new File(szFileName);
sz_FileContent = readFileBuffer(szFileName, (int) file.length());
return sz_FileContent;
} public String readFileBuffer(String szFileName, int size) {
String sz_FileContent = "";
BufferedInputStream bis = null;
File file = null;
try {
file = new File(szFileName);
bis = new BufferedInputStream(new FileInputStream(file)); byte[] buff = new byte[size];
bis.read(buff, 0, size);
sz_FileContent += new String(buff); //去掉一些输出时的格式无用字符
sz_FileContent = sz_FileContent.replaceAll("\r\n\t\t\t", "");
//System.out.println(sz_FileContent);
} catch (Exception e) {
} finally {
try {
if (bis != null)
bis.close();
} catch (Exception e) {
}
}
return sz_FileContent;
} public void writeFileBuffer(String szFileName, String content) {
try {
File file = new File(szFileName);
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(file)));
dos.writeBytes(content);
dos.close();
} catch (Exception e) {
}
} private String toHexString1(byte[] b) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i) {
buffer.append(toHexString1(b[i]));
}
return buffer.toString();
} private String toHexString1(byte b) {
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1) {
return "0" + s;
} else {
return s;
}
} private String toHexString2(byte[] b) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i) {
buffer.append(toHexString2(b[i]));
}
return buffer.toString();
} private String toHexString2(byte b) {
char[] buffer = new char[2];
buffer[0] = Character.forDigit((b >>> 4) & 0x0F, 16);
buffer[1] = Character.forDigit(b & 0x0F, 16);
return new String(buffer);
} private String toHexString3(byte[] bytes) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < bytes.length; ++i) {
buffer.append(toHexString3(bytes[i]));
}
return buffer.toString();
} private String toHexString3(byte b) {
int n = new Integer(b).intValue();
String s = "";
if (n >= 0) {
s = Integer.toHexString(n);
if (s.length() == 1) {
s = "0" + s;
}
} else {
s = Integer.toHexString(n).substring(6, 8);
}
return s;
}
}
通过到网上查找相关资料,我已经解决了此问题,现将程序发布到此,和大家共享一下。
同时多谢各位的帮助和提示。