这个程序是这个样子的,首先使用read()函数读取jpg文件转化为byte[],因为某种需要我把byte[]转化为了String,然后再转化为byte[];最后用write()函数保存到test.jpg;可是test.jpg却无法显示;
不知道为什么,谁能帮我解决这个问题import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class Test{
public static void main(String[] args) {
test("*.jpg");
} public static void test(String path) { byte[] b1 = read(new File(path));
String str = new String(b1);
byte[] b2 = str.getBytes();
write(new File("test.jpg"), b2);
} public static byte[] read(File f) {
try {
FileInputStream in = new FileInputStream(f);
int length = in.available();
byte[] b = new byte[length];
in.read(b, 0, length);
in.close();
return b;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static void write(File f, byte[] b) {
try {
FileOutputStream out = new FileOutputStream(f);
out.write(b);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
不知道为什么,谁能帮我解决这个问题import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class Test{
public static void main(String[] args) {
test("*.jpg");
} public static void test(String path) { byte[] b1 = read(new File(path));
String str = new String(b1);
byte[] b2 = str.getBytes();
write(new File("test.jpg"), b2);
} public static byte[] read(File f) {
try {
FileInputStream in = new FileInputStream(f);
int length = in.available();
byte[] b = new byte[length];
in.read(b, 0, length);
in.close();
return b;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static void write(File f, byte[] b) {
try {
FileOutputStream out = new FileOutputStream(f);
out.write(b);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
=================
private String FromFileToString(String filename)
{
try
{
in=new FileInputStream(filename);
byte[] buff=new byte[in.available()];
in.read(buff);
StringBuffer str=new StringBuffer();
for(int i=0;i<buff.length;i++)
{
str.append(buff[i]+" ");
}
return str.toString();
}
catch(IOException e)
{
return null;
}
finally
{
try
{
if(in!=null)
{
in.close();
}
}
catch(IOException e)
{
System.out.println("close the fileinputstream failed:"+e.getMessage());
}
}
}
{ this.Bytes=bytes.split(" ");
try
{
outfile=new FileOutputStream(filename);
for(int i=0;i<Bytes.length;i++)
{
outfile.write(Integer.parseInt(Bytes[i]));
}
}
catch(IOException e)
{
}
finally
{
try
{
if(outfile!=null)
{
outfile.close();
}
}
catch(IOException e)
{
System.out.println("close the write file in the server faild:"+e);
}
}
}
byte[] b2 = str.getBytes("ISO-8859-1");
test.jpg还是无法显示;代码:import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestBlob {
public static void main(String[] args) {
test("*.jpg");
} public static void test(String path) { String str = FromFileToString(path);
byte[] b = str.getBytes();
write(new File("test.jpg"), b);
} private static String FromFileToString(String filename) {
FileInputStream in = null;
try {
in = new FileInputStream(filename);
byte[] buff = new byte[in.available()];
in.read(buff); StringBuffer str = new StringBuffer();
for (int i = 0; i < buff.length; i++) {
str.append(buff[i] + " ");
}
return str.toString();
} catch (IOException e) {
return null;
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
System.out.println("close the fileinputstream failed:"
+ e.getMessage());
}
}
} public static void write(File f, byte[] b) {
try {
FileOutputStream out = new FileOutputStream(f);
out.write(b);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required. public String(byte[] bytes)
The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.
你给我的函数都用到了成员变量;
能不能给一个完整的程序代码啊?
谢谢
谢谢你提供的代码,成功了;
不过可不可以不使用空格啊,使用其他字符可以吗?比如*,#
String str = new String(b1,"ISO-8859-1");
byte[] b2 = str.getBytes("ISO-8859-1");
楼主为什么不试一下呢
String str = new String(b1,"ISO-8859-1");
byte[] b2 = str.getBytes("ISO-8859-1");
楼主为什么不试一下呢
___________________________________________________________________
我刚才粗略的试了一下,好像不幸;
那我再试试看
不过你的办法转化为String输出的是乱码,解析时不好控制
所以我还是会用它的办法,分给你给的少了一下,对不起了
public static void main(String[] args) throws Exception{
test("output\\1.gif");
//byte[]b = {97,98};
//System.out.println(new String(b));
//b = new String(b).getBytes();
//System.out.println(b[0]);
} public static void test(String path) throws Exception{ byte[] b1 = read(new File(path));
String str = new String(b1,"ISO-8859-1");
byte[] b2 = str.getBytes("ISO-8859-1");
write(new File("2.gif"), b1);
} public static byte[] read(File f) {
try {
FileInputStream in = new FileInputStream(f);
int length = in.available();
byte[] b = new byte[length];
in.read(b, 0, length);
in.close();
return b;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static void write(File f, byte[] b) {
try {
FileOutputStream out = new FileOutputStream(f);
out.write(b);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
所以我还是会用它的办法