IFS read Essayez de lire les fichiers (SJIS, UTF8, UTF16) créés sur IFS avec le code Java suivant de 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 1ère ligne abc";
				String str2 = "Test string 2ème ligne";
				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();
			}
	}
}
Puisque iconv-lite est utilisé pour la conversion de code de caractère, veuillez l'installer avant de l'exécuter.
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);
  });
});
C'est le résultat de l'exécution.
 
        Recommended Posts