Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

Write byte array to file in java

I want to write byte array to txt file. Firstly I want to see that data in eclipse console and I see that data are true. But when I want to write this data into txt file the problem is manifest itself. Txt file is coming with errors.

My code is as follows.

FileOutputStream fos;
try {
    fos = new FileOutputStream("/usr/text110.txt");
    fos.write(imageInByte);
    fos.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

My txt file is as follows. How can I save my byte array to file properly. Please help me.

0-0R~9DB6C9D9DDEBF1FAFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAEFEFE4E0E2DAD9D7D0CEC4C1C0B6B5B1AEB0A1A2A0999D9995989295938E929195858686868A8A898C888C848180y}~{||||zzxxwzyusoklnppjqy818E8F94919799A0B2C4D2E0E9EAE7ECE9E9E7E3E3D9DCD3CFD0C7C7C6C2üBDB4B1ACA1A09E97968E848483888589znlx{86868C908D9290909490959194929091898B8483ungiba[Xcbfbc^SRSSXUKILTVbes|8C959DAAB4B6B4C5D2E3ECF2FAFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF4E8E0DED9D2D8D6D4CCCBC8C6C5C9?BCBFBEC1BCC0BEC0BDBCBEB9B7AEA8A8A7A5AAA8AEADB2B2B1B5B0B2ADAFACA8A8A2A5A19F978D8D91949C9CA3A2A7A4A3A19C9E9B9A9E9A9C968D8B9099A2A3A5A6AAACB2B9BCBEC3C2C6C1?B9BDB8BDB9BBB3AB979A9AA1A0A5A2A4A29F9E9698919089828181838386868280vwtyx}}8082838584868385}808083~82}|}|{80}828187868B898B868580xsyy|80trrv8386939FA5B1ACAEB6BCBFC9C9C9C9CDD6D4CFCDD8DBE4E2E8E9E8EDE9EFEAEEEDE8EAE2E4E2DEE0D9DBD2D3CBC9?AA9A9B96989F9FA6A3A7A0A2998D858B8B908E939290938F918E918F908A8A828480828181zwla_dmqzy81~8386828885868A8586~zz81838680t|}u{85858F8E939A9AA09C938D9392999397999399969A9A959691898D898D8B8A8D858988858A84888282yuqgb^UUQQQJJC?1217387>jiowrv~8180888C8A929592999C999FA19DA3A1A2A7A1A5A9A4A9ABA6ACAAACB1ABB0B1ABB0AAABAFA9ACABA9ADA6A9A6A5A79FA1A09B9E9698979192898B878383yzqonfibcaZVJ=.)##!#)($'% -8:FMOX]Yagcjohmqknphlkejiflhjnijmdegadbc_b_VZ]VXRHGAEBLKRSRVQSNQQMNEGIDIDJLJOGJEA=5-))*.+.''..16.*3;:EIKTYU_fbjrou|y|8380828884858B86888C868A8E888D8F8A90908E928E90948E91938C90908D90898C8C8589828285~828083~828182{}|vyqrmih^^TTJF:45.5226.1-0.+.'"!'+4<9BJGMTPT[[V]_X_VYZUYUTYQU[UZ_Y]aZ^UWTNOFIMIMQLPOCD?96'!(+15/4/1+-'#!! &%),)( #!',(185=D?ENNPY[Xafahnhotorwquyruyrwysyyu|xz~y|80z~80z~~{~wz|twvswqutquprtnrplpimlgiacc^TVTQRGJH@@550*)!& " "$+-687>C?FLFKRPPUUMQQJLGGICHJFLPKQUNSVMQRJMKHJADHAFIDGHAC?9:68369/35110..))#" " #-/,5<8@FAGOKOVYV]ae^be^bd^ab[]]b]faeidgkdhjchhbfac_`XVWYRWWTXRVXQURQUMPPIMFHIFGA<:;44./625>8;CEAGH?CC:<53626638748647340+.-/#),'+)(!# !

")&'0229>;BGAGMIKQRNSUPTVORQLOIJNIMQHNRLRVPUXPTVPTTPTQOQGJLBE??B;@A@FAAE?BC;?9=@9<96:311*.''(! %!$'!'-05029:59<458200+%!#(&-4/5;439:8:70/(+("'#&,(+)(*! "!"#%$!

$#)#&.-,4:8>C=@E@=BB=AB>>:992686<@A@>A?=@57911+("))/)&&.*+&&(!&%%#!"#"#

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
FileOutputStream fos = FileOutputStream("path/to/the/file/to/write/in");
fos.write(theByteArray);
fos.close();

Will write the byte array in byte form.

FileOutputStream fos = FileOutputStream("path/to/the/file/to/write/in");
for (byte b : bytes) {
    fos.write(String.format("%02X ", b).getBytes());
}
fos.write(theByteArray);
fos.close();

Will write byte array in human readable form.

Source: Java code To convert byte to Hexadecimal http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...