Sujet : Re: New VSI post on Youtube
De : arne (at) *nospam* vajhoej.dk (Arne Vajhøj)
Groupes : comp.os.vmsDate : 20. Aug 2024, 20:10:51
Autres entêtes
Organisation : SunSITE.dk - Supporting Open source
Message-ID : <66c4ea3b$0$715$14726298@news.sunsite.dk>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13
User-Agent : Mozilla Thunderbird
On 8/20/2024 2:37 PM, Arne Vajhøj wrote:
Because InputStream/OutputStream and Reader/Writer are fundamentally
different.
The first transfer between an in memory sequence of bytes
(-128..127 values) to an external form of same sequence of
bytes (-128..127 values).
The second transfer between an in memory sequence of
sequence of chars aka Unicode code points (0..65536 values)
usually in the form of String objects to an external form
of sequence of bytes (-128..127) values often of a different
length according to some encoding (UTF-8 is most common but
there are hundreds of possibilities: UTF-16, UTF-32, ISO-8859-x,
ASCII, EBCDIC, various non-latin).
Demo:
$ type OSDemo.java
import java.io.*;
public class OSDemo {
public static void main(String[] args) throws Exception {
try(OutputStream os = new FileOutputStream("os.dat")) {
os.write(1);
os.write(2);
os.write(3);
}
}
}
$ javac OSDemo.java
$ java OSDemo
$ dump os.dat
Dump of file DKA0:[arne]os.dat;2 on 20-AUG-2024 15:05:50.22
File ID (52122,13,0) End of file block 1 / Allocated 16
Virtual block number 1 (00000001), 512 (0200) bytes
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00030201 ................................ 000000
$ type WrtDemo.java
import java.io.*;
public class WrtDemo {
public static void main(String[] args) throws Exception {
try(PrintWriter pw = new PrintWriter("pw_" + args[0].toLowerCase() + ".txt", args[0])) {
pw.printf("%d %d %d\n", 1, 2, 3);
}
}
}
$ javac WrtDemo.java
$ java WrtDemo "UTF-8"
$ dump pw_utf-8.txt
Dump of file DKA0:[arne]pw_utf-8.txt;2 on 20-AUG-2024 15:06:00.26
File ID (52124,20,0) End of file block 1 / Allocated 16
Virtual block number 1 (00000001), 512 (0200) bytes
00000000 00000000 00000000 00000000 00000000 00000000 00000A33 20322031 1 2 3........................... 000000
$ java WrtDemo "UTF-16"
$ dump pw_utf-16.txt
Dump of file DKA0:[arne]pw_utf-16.txt;2 on 20-AUG-2024 15:06:02.38
File ID (52126,11,0) End of file block 1 / Allocated 16
Virtual block number 1 (00000001), 512 (0200) bytes
00000000 00000000 00000000 00000000 00000A00 33002000 32002000 3100FFFE þ..1. .2. .3.................... 000000
Arne