IFS read Try to read the file (SJIS, UTF8, UTF16) created on IFS with the following Java code from Node.
IfsWrite.java
public class IfsWrite {
public static void main(String[] args) {
AS400 as400 = new AS400("192.168.X.XXX" ,"MYUSER" ,"MYPASS");
final String BR = System.getProperty("line.separator");
IFSFileOutputStream fileutf8;
IFSFileOutputStream fileutf16;
IFSFileOutputStream filesjis;
try {
String str1 = "Test string 1st line abc";
String str2 = "Test string 2nd line";
String str = str1 + BR + str2;
byte[] utf8 = str.getBytes("UTF-8");
byte[] utf16 = str.getBytes("UTF-16");
byte[] sjis = str.getBytes(Charset.forName("Shift_JIS"));
fileutf8 = new IFSFileOutputStream(as400, "/myIfsFolder/java-utf8.txt",IFSFileOutputStream.SHARE_ALL, true,1208);
fileutf8.write(utf8);
fileutf8.close();
fileutf16 = new IFSFileOutputStream(as400, "/myIfsFolder/java-utf16.txt",IFSFileOutputStream.SHARE_ALL, true,1200);
fileutf16.write(utf16);
fileutf16.close();
filesjis = new IFSFileOutputStream(as400, "/myIfsFolder/java-sjis.txt",IFSFileOutputStream.SHARE_ALL, true,932);
filesjis.write(sjis);
filesjis.close();
} catch (AS400SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Since iconv-lite is used for character code conversion, please install it before executing.
npm install iconv-lite
IfsRaed.JS
var jt400 = require("node-jt400");
var express = require("express");
var iconv = require('iconv-lite');
var app = express();
var pool = jt400.pool({ host: '192.168.X.XXX', user: 'MYUSER', password: 'MYPASS' });
var server = app.listen(8888, function () {
console.log("curl http://localhost:" + server.address().port + '/~');
});
app.get("/ifsReadSjis", function (req, res, next) {
const ifs = pool.ifs();
let ifsReadableStream = ifs.createReadStream("/myIfsFolder/java-sjis.txt").pipe(iconv.decodeStream("Shift_JIS"));
let buffer = "";
ifsReadableStream.on("data", (chunk) => {
buffer += chunk;
});
ifsReadableStream.on("end", function () {
res.send(buffer);
});
});
app.get("/ifsReadUtf8", function (req, res, next) {
const ifs = pool.ifs();
let ifsReadableStream = ifs.createReadStream("/myIfsFolder/java-utf8.txt").pipe(iconv.decodeStream("UTF-8"));
let buffer = "";
ifsReadableStream.on("data", (chunk) => {
buffer += chunk;
});
ifsReadableStream.on("end", function () {
res.send(buffer);
});
});
app.get("/ifsReadUtf16", function (req, res, next) {
const ifs = pool.ifs();
let ifsReadableStream = ifs.createReadStream("/myIfsFolder/java-utf16.txt").pipe(iconv.decodeStream("UTF-16"));
let buffer = "";
ifsReadableStream.on("data", (chunk) => {
buffer += chunk;
});
ifsReadableStream.on("end", function () {
res.send(buffer);
});
});
This is the execution result.
Recommended Posts