Write data to file

M

moonhkt

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);
}
}
}
 
A

Arne Vajhøj

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
 
R

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top