導航:首頁 > 編程語言 > java文件二進制流

java文件二進制流

發布時間:2022-05-11 11:41:44

java中如何把圖片轉換成二進制流

Java中將圖片轉為二進制流只需要使用FileImageInputStream取得圖片文件,然後使用ByteArrayOutputStream 寫入到二進制流中即可,下面是詳細代碼:


//圖片到byte數組
publicbyte[]image2byte(Stringpath){
byte[]data=null;
FileImageInputStreaminput=null;
try{
input=newFileImageInputStream(newFile(path));
ByteArrayOutputStreamoutput=newByteArrayOutputStream();
byte[]buf=newbyte[1024];
intnumBytesRead=0;
while((numBytesRead=input.read(buf))!=-1){
output.write(buf,0,numBytesRead);
}
data=output.toByteArray();
output.close();
input.close();
}
catch(FileNotFoundExceptionex1){
ex1.printStackTrace();
}
catch(IOExceptionex1){
ex1.printStackTrace();
}
returndata;
}

另外,如果需要將byte[]存回圖片或轉為String,則:

//byte數組到圖片
publicvoidbyte2image(byte[]data,Stringpath){
if(data.length<3||path.equals(""))return;
try{
=newFileImageOutputStream(newFile(path));
imageOutput.write(data,0,data.length);
imageOutput.close();
System.out.println("MakePicturesuccess,Pleasefindimagein"+path);
}catch(Exceptionex){
System.out.println("Exception:"+ex);
ex.printStackTrace();
}
}
//byte數組到16進制字元串
publicStringbyte2string(byte[]data){
if(data==null||data.length<=1)return"0x";
if(data.length>200000)return"0x";
StringBuffersb=newStringBuffer();
intbuf[]=newint[data.length];
//byte數組轉化成十進制
for(intk=0;k<data.length;k++){
buf[k]=data[k]<0?(data[k]+256):(data[k]);
}
//十進制轉化成十六進制
for(intk=0;k<buf.length;k++){
if(buf[k]<16)sb.append("0"+Integer.toHexString(buf[k]));
elsesb.append(Integer.toHexString(buf[k]));
}
return"0x"+sb.toString().toUpperCase();
}

⑵ java怎麼實現讀取一個文件,拿到二進制流

Java讀取二進制文件,以位元組為單位進行讀取,還可讀取圖片、音樂文件、視頻文件等,
在Java中,提供了四種類來對文件進行操作,分別是InputStream OutputStream Reader Writer ,前兩種是對位元組流的操作,後兩種則是對字元流的操作。
示例代碼如下:
public static void readFileByBytes(String fileName){
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("一次讀一個");
// 一次讀一個位元組
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}

⑶ 請教,怎麼用JAVA來讀取二進制文件並輸出文件內容

Java讀取二進制文件,以位元組為單位進行讀取,還可讀取圖片、音樂文件、視頻文件等,
在Java中,提供了四種類來對文件進行操作,分別是InputStream OutputStream Reader Writer ,前兩種是對位元組流的操作,後兩種則是對字元流的操作。
示例代碼如下:
public static void readFileByBytes(String fileName){
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("一次讀一個");
// 一次讀一個位元組
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}

⑷ java里怎樣把文件轉換成二進制

轉換文件成為二進制數據並保存的Java代碼:

取出數據並還原文件到本地的java代碼:

[java]view plain//讀取資料庫二進制文件

publicvoidreaderJpg()throwsSQLException

{

connection=connectionManager.getconn();//自己連接自己的資料庫

StringsqlString="selectimagesfromsave_imagewhereid=4";//從資料庫中讀出要還原文件的二進制碼,這里我讀的是自己的資料庫id為4的文件

Filefile=newFile("E:\1.jpg");//本地生成的文件

if(!file.exists())

{

try{

file.createNewFile();

}catch(Exceptione){

e.printStackTrace();

}

}

try{

byte[]Buffer=newbyte[4096*5];

statement=connection.prepareStatement(sqlString);

resultSet=statement.executeQuery();

if(resultSet.next())

{

FileOutputStreamoutputStream=newFileOutputStream(file);

InputStreamiStream=resultSet.getBinaryStream("images");//去欄位用getBinaryStream()

intsize=0;

while((size=iStream.read(Buffer))!=-1)

{

System.out.println(size);

outputStream.write(Buffer,0,size);

}

}

}catch(Exceptione){

e.printStackTrace();

}

}

閱讀全文

與java文件二進制流相關的資料

熱點內容
JAVA特點是什麼 瀏覽:881
字幕網app怎麼找 瀏覽:528
奧迪a4l壓縮比 瀏覽:693
iis7伺服器怎麼安裝 瀏覽:975
孩子上學壓力太大怎麼解壓 瀏覽:236
javaurl格式 瀏覽:342
勻稱app怎麼用 瀏覽:61
pythonemail中文亂碼 瀏覽:442
android訪問html 瀏覽:88
做一個app或者網頁用什麼編程 瀏覽:776
一體機伺服器運行失敗怎麼辦 瀏覽:936
longjava比較大小 瀏覽:532
clojure編程pdf 瀏覽:22
一年級數學圈一圈的演算法 瀏覽:111
完整的python 瀏覽:42
c51單片機數字鍾 瀏覽:923
apk文件反編譯後安裝包損壞 瀏覽:816
app圖標分別是什麼 瀏覽:368
linux全選命令 瀏覽:365
怎麼用華碩app 瀏覽:963