0

i just want fileoutputstream like filewriter at same style

like this

//filewriter code
        try {
        File file = ('some file link');
        fw=new FileWriter(file,true);
        fw.write("2017-06-08");
        fw.write("#");//separator
        fw.write("ABCD");
        fw.write("#");//separator
        fw.write("\r\n");
        fw.flush();
    } 

is answer at file :: 2017-06-08#ABCD#

but i want filelock, so use fos&channel.

try {   
        //date = string "2017-06-08", menu= string"ABCD"
        fos = new FileOutputStream('file_addr',true);
        channel = fos.getChannel();
        lock=channel.tryLock();
        //if (lock==null){ //<- not yet work
        //  channel.close();
        //  throw new Exception();
        //}

        ByteBuffer buf = ByteBuffer.allocate(512);

        for (char ch : date.toCharArray())
            buf.putChar(ch);


            buf.putChar('#');

        for (char ch : menu.toCharArray())
            buf.putChar(ch);



            buf.putChar('\r');

            buf.putChar('\n');
            buf.rewind();
        channel.write(buf);


        //if (lock!=null){
        //  lock.release();
        //  channel.close();
        //}

but isn't work. is output is '2 0 1 7 - 0 6 - 0 8'(and crushed)

how can write like this '2017-06-08#ABCD#' using fos & filechannel?

LocketGoma
  • 75
  • 1
  • 2
  • 11
  • It the problem that you are writing using the UTF-16 encoding of the Unicode character set and/or don't know how to verify that? See [ByteBuffer.putChar(char)](https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#putChar-char-). How do you conclude the output is "2 0 1 7 - 0 6 - 0 8"? – Tom Blodget Jun 07 '17 at 17:06
  • i checked written file. in that file. written string is "2 0 1 7 -", not "2017-" + i will check your links. thx – LocketGoma Jun 07 '17 at 17:10
  • When a text file is written it is with a specific character encoding. When you read it, you must use that same encoding. (Editors and people like to guess. Debuggers like to assume something simple. They are all wrong unless they are right.) – Tom Blodget Jun 07 '17 at 17:54

1 Answers1

0

The documentation for ByteBuffer.putChar() explains the problem you are having.

Writes two bytes containing the given char value, in the current byte order, into this buffer at the current position, and then increments the position by two.

In other words, putChar() writes a UTF-16 character (so that it can be read using getChar() ). This is probably not what you want.

If your characters are all in ascii, you can write them as single bytes simply by casting them. buf.put((byte)ch) should give you what you expected.

David Narum
  • 312
  • 2
  • 3
  • actually i write Unicode text. : < (because write string isn't alphabet) – LocketGoma Jun 07 '17 at 16:34
  • @LocketGoma No, you are not writing Unicode in your original example. By using `FileWriter(File, boolean)` the person who is running your program is using their default character set and encoding at the time they start the program. – Tom Blodget Jun 07 '17 at 16:44
  • @Tom Blodget that is my mistake... 'ABCD' position string is unicode set string. i'm sorry. – LocketGoma Jun 07 '17 at 17:00
  • Also Unicode can be UTF-8, UTF-16, UTF-32 etc. you need to know which one. If you have a choice, choose UTF-8, but java uses UTF-16 internally. This matters because UTF-8 is 1 byte usually and UTF-16 is 2 bytes usually – Novaterata Jun 07 '17 at 18:02
  • 1
    If you wish to write Unicode text using ByteBuffer, you will have to use the `put(byte[])` method. Do `put(menu.getBytes(CHAR_SET));` using the character set you want (e.g. `private static final CHAR_SET = Charset.forName("UTF-8");`) – David Narum Jun 09 '17 at 10:26