[英]how to write/read from/to memory using java
Following java code is reading value from JSON
File ,using freemarker
template its storing JSON
value and write that new structure with JSON
key and value to text file and save to path mentioned ,and after that from mentioned path text file will be read and it will print that text file to TSC
printer.My concern is that I want to store that temporary reading/writing file to memory ,Can any one please help regarding this ,how to store that temporary file to memory ? 下面的Java代码从
JSON
File中读取值,使用freemarker
模板存储JSON
值,并将带有JSON
键和值的新结构写入文本文件并保存到上述路径,然后从提到的路径中读取文本文件,将那个文本文件打印到TSC
打印机。我担心的是,我想将该临时读/写文件存储到内存中,请问对此有何帮助,如何将该临时文件存储到内存中?
Java Code Java代码
public class JSONSimpleWritingToFileExample {
public static void main (String[] args){
// ************** Reading from JSON file **********
final String filePath = ("C:/Users//Desktop/333.json"); //JSON Path
FileReader reader = null;
try {
reader = new FileReader(filePath);
final JSONParser parser = new JSONParser();
final JSONObject json = (JSONObject) parser.parse(reader);
final JSONArray jsonUsers = (JSONArray) json.get("Booking");
final Iterator<?> it = jsonUsers.iterator();
while (it.hasNext())
{
final JSONObject jsonUser = (JSONObject) it.next();
final String bookSrc = (String) jsonUser.get("Key1");
final String custName = (String) jsonUser.get("Key2");
final String custNum = (String) jsonUser.get("Key3");
final String custPName = (String) jsonUser.get("Key4");
// ********* Reading From Template *************
Configuration cfg = new Configuration();
try {
//Load template from source folder
Template template = cfg.getTemplate("src/Test.ftl"); // Reading from Template path
// Build the data-model
Map<String, Object> data = new HashMap<String, Object>();
data.put("Key1", ""+Value1);
data.put("Key2", ""+Value2);
data.put("Key3", ""+Value3);
data.put("Key4", ""+Value4);
// Console output
Writer out = new OutputStreamWriter(System.out);
template.process(data, out);
out.flush();
// File output
Writer file = new FileWriter (new File("D:\\FTL_helloworld.txt")); // Writing text file path
template.process(data, file);
file.flush();
file.close();
// Reading Text file & Printing Logic
FileInputStream textStream;
textStream = new FileInputStream("D:/FTL_helloworld.txt");
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocAttributeSet das=new HashDocAttributeSet();
Doc mydoc = new SimpleDoc(textStream, flavor, das);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
@SuppressWarnings("unused")
PrinterJob pj = PrinterJob.getPrinterJob();
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
for (int i = 0; i < services.length; i++)
{
System.out.println(services[i].getName());
}
if(services.length == 0)
{
if(defaultService == null)
{
//no printer found
}
else {
//print using default
DocPrintJob job = defaultService.createPrintJob();
job.print(mydoc, aset);
}
}
else {
PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset);
if (service != null)
{
DocPrintJob job = service.createPrintJob();
job.print(mydoc, aset);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} } catch ( Exception e){
e.printStackTrace();
}
}
}
303 JSON file 303 JSON文件
{
"Booking": [ {
"Key1":"Value1",
"Key2":"Value2",
"Key3":"Value3",
"Key4":"Value4"
},
{
"Key1":"Value1",
"Key2":"Value2",
"Key3":"Value3",
"Key4":"Value4"
}]
}
Test.ftl 测试文件
Q799,B080+000
q831
rN
S4
D7
ZT
JF
OD,P
R24,0
N
X555,56,2,780,714
A771,73,1,1,2,1,N,"A {0}"
A742,70,1,1,2,2,N," {1}({31})"
A765,450,1,1,2,2,N,"${Value1}"
A706,86,1,2,1,1,N,"${Value2}"
A682,86,1,2,1,1,N,"${Value3}"
A658,86,1,2,1,1,N,"${Value4}"
P1
Instead of writing the data to a file and then reading it from a file, use a ByteArrayOutputStream
and a ByteArrayInputStream
(which means your intermediate storage mechanism is a byte array in memory). 与其将数据写入文件然后从文件中读取,
ByteArrayOutputStream
使用ByteArrayOutputStream
和ByteArrayInputStream
(这意味着您的中间存储机制是内存中的字节数组)。
FreeMarker's Template class uses a Writer
to write output. FreeMarker的Template类使用
Writer
编写输出。 Instead of using a FileWriter, try constructing an OutputStreamWriter: 而不是使用FileWriter,请尝试构造一个OutputStreamWriter:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(baos);
template.process(data, writer);
writer.close();
You can retrieve the data: 您可以检索数据:
byte[] savedData = baos.toByteArray();
Then read it back in: 然后读回:
ByteArrayInputStream bais = new ByteArrayInputStream(savedData);
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocAttributeSet das = new HashDocAttributeSet();
Doc mydoc = new SimpleDoc(bais, flavor, das);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.