From: moonhkt on
Hi All
Do you know how to write data to file as below format

first data is readable , second data is byte value


for (int i = 192 ; i < 223 ; i++) {
for ( int j = 129 ; i <= 223 , j++) {
output to file i ,j ;
output to byte of i , byte f j;
}
}

I does not know how to update below file.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class build_binary {
public static void main(String[] args) {

String phrase = new String(new byte [] {
(byte) 0xe5, (byte) 0x87, (byte) 0x8c
});
File aFile = new File("test.txt");

FileOutputStream file = null;
try {
file = new FileOutputStream(aFile, true);
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
}
FileChannel outChannel = file.getChannel();
ByteBuffer buf = ByteBuffer.allocate(phrase.length());
byte[] bytes = phrase.getBytes();
buf.put(bytes);
buf.flip();

try {
outChannel.write(buf);
file.close();
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
}
From: Arne Vajhøj on
On 01-02-2010 03:42, moonhkt wrote:
> Do you know how to write data to file as below format
>
> first data is readable , second data is byte value
>
>
> for (int i = 192 ; i< 223 ; i++) {
> for ( int j = 129 ; i<= 223 , j++) {
> output to file i ,j ;
> output to byte of i , byte f j;
> }
> }
>
> I does not know how to update below file.

> ByteBuffer buf = ByteBuffer.allocate(phrase.length());
> byte[] bytes = phrase.getBytes();
> buf.put(bytes);
> buf.flip();

buf.put(bytes);

need to ve replaced with two loops that calls putInt and put on buf
with the necessary arguments.

Arne
From: Roedy Green on
On Mon, 1 Feb 2010 00:42:44 -0800 (PST), moonhkt <moonhkt(a)gmail.com>
wrote, quoted or indirectly quoted someone who said :

>Do you know how to write data to file as below format
>
>first data is readable , second data is byte value

see http://mindprod.com/applet/fileio.html

It will generate you code for almost anything reasonable.

You don't mix human-readable and binary in Java.

Pick one or the other.
--
Roedy Green Canadian Mind Products
http://mindprod.com

You can�t have great software without a great team, and most software teams behave like dysfunctional families.
~ Jim McCarthy
 | 
Pages: 1
Prev: Code and Creation 01324
Next: Jave ME and BT