Files in the z / OS environment (IBM mainframe environment) are called data sets. Data sets are slightly different from Linux, Unix, and Windows files (*** 1 **). The features of the dataset and the implementation sample of dataset I / O processing in Java language are shown below.
** * 1 ** In the z / OS environment, there is also a Unix environment called USS in addition to the environment operated by TSO and JCL. Files in the USS environment can be accessed using the standard Java API, just like files in Linux, Unix, and Windows.
The dataset has the following characteristics:
Data sets with the above characteristics cannot implement I / O processing with the Java standard API (java.io package or java.nio package). Then, what to do is to use ** Java Batch Launcher and Toolkit for z / OS (hereinafter referred to as JZOS) **. JZOS is a toolkit that supports the development of Java applications that run in the z / OS environment and is included in IBM Java for z / OS (see the reference link [JZOS Batch Launcher and Toolkit](https:: for an overview of JZOS). //www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.80.doc/zsecurity-component/jzos.html See "Reference link: JZOS Batch Launcher and Toolkit").
The sample Java application that implements the input processing from the dataset and the sample Java application that implements the output processing to the dataset are shown below (*** 2 **).
** * 2 ** There are various types of datasets, but in the following, samples are shown focusing on the sequential organization dataset, which is similar to Windows files and Unix files and has the simplest structure. ..
The following sample program (DSRead.java) reads records from the dataset 'USER01.TEST.DATA'
and writes them to standard output. It is assumed that the data is encoded in ʻUTF-8`.
DSRead.java
package sample;
import com.ibm.jzos.RecordReader;
import com.ibm.jzos.ZFile;
public class DSRead {
public void main(String args[]) throws Exception {
String ddName = ZFile.allocDummyDDName();
String dsName = "USER01.TEST.DATA";
RecordReader reader = null;
int x = 0;
try {
ZFile.bpxwdyn("alloc fi("+ddName+") da("+dsName+") shr reuse");
reader = RecordReader.newReaderForDD(ddName);
byte[] recordBuf = new byte[reader.getLrecl()];
while ((reader.read(recordBuf)) >= 0) {
String rec = new String(recordBuf, "UTF-8");
System.out.println(rec);
x++;
}
System.out.println("record count : "+x);
} catch(Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
ZFile.bpxwdyn("free fi("+ddName+")");
}
}
}
** This is the point! **
--Dataset allocation and free is done with the ZFile # bpxwdyn
method.
--This method BPXWDYN For dynamic allocation (dynamically allocating datasets programmatically). Call a command interface called ibm.zos.v2r2.bpxb600/wdyn.htm (reference link: BPXWDYN)). Specify the command string (" alloc ... "
, " free ... "
) you want to execute as an argument.
--Specify the DD name for the alloc command (*** 3 **) and the free command. The DD name is a logical name that identifies the dataset in your program. The ZFile # allocDummyDDName
method is generating a unique DD name.
--The process of reading a data record from a file is done with RecordReader
.
--An instance of RecordReader
is obtained by calling the factory method RecordReader # newReaderForDD
with the DD name mentioned above. For details on the API, refer to the reference link [JZOS API (Javadoc)](http://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.api.80.doc/com.ibm. See .jzos / index.html "Reference link: JZOS API (Javadoc)").
** * 3 ** ʻalloc is an abbreviation for ʻallocate
. See the reference link Examples of allocate command for specific examples of the allocate command.
See "Reference link: Example of allocate command").
The following sample program (DSWrite.java) writes records in overwrite mode to the already existing dataset'USER01.TEST.DATA'. The dataset'USER01.TEST.DATA' has a fixed length and a logical record length of 80 bytes, and outputs a 3-line record created by the getTestData
method. The data shall be encoded in UTF-8 and output.
DSWrite.java
package sample;
import com.ibm.jzos.RecordWriter;
import com.ibm.jzos.ZFile;
public class DSWrite {
public void main(String args[]) throws Exception {
String ddName = ZFile.allocDummyDDName();
String dsName = "USER01.TEST.DATA";
RecordWriter writer = null;
int x = 0;
try {
ZFile.bpxwdyn("alloc fi("+ddName+") da("+dsName+") old reuse");
writer = RecordWriter.newWriterForDD(ddName);
int lrecl = writer.getLrecl();
String records[] = getTestData();
for (String record : records) {
writer.write(record.getBytes("UTF-8"), 0, lrecl);
x++;
}
System.out.println("record count : "+x);
} catch(Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
ZFile.bpxwdyn("free fi("+ddName+")");
}
}
private String[] getTestData() {
String[] records = new String[3];
records[0] = String.format("%-80s", "AAAAAAAAAAAAAAAAAAAA");
records[1] = String.format("%-80s", "BBBBBBBBBBBBBBBBBBBB");
records[2] = String.format("%-80s", "CCCCCCCCCCCCCCCCCCCC");
return records;
}
}
** This is the point! **
--The data set allocation and free are almost the same as the above sample DSRead.java
.
--The only difference is that the DISP parameter of the alloc command specifies old instead of shr. You can access the dataset exclusively by specifying old.
--The process of writing data records to a file is done with RecordWriter
.
--An instance of RecordWrite
is obtained by calling the factory method RecordWriter # newWriterForDD
with the DD name. For details on the API, refer to the reference link [JZOS API (Javadoc)](http://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.api.80.doc/com.ibm. .jzos / index.html See "JZOS API (Javadoc)").
JZOS Batch Launcher and Toolkit https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.80.doc/zsecurity-component/jzos.html
JZOS API (Javadoc) http://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.zsecurity.api.80.doc/com.ibm.jzos/index.html
BPXWDYN https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.2.0/com.ibm.zos.v2r2.bpxb600/wdyn.htm
** Example of allocate command ** http://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.ikjc500/ikj2l2_ALLOCATE_command_examples.htm
Recommended Posts