Discussion:
How to write TDI java script to create new file with UTF-8 encoding
(too old to reply)
Kanon Yingwanna
2017-02-13 15:22:29 UTC
Permalink
Raw Message
Could you please advsie, How to write TDI java script to create new file with UTF-8 encoding? Thank you
John Dell'Oso
2017-02-14 00:20:23 UTC
Permalink
Raw Message
This is probably not the "prettiest way" to perhaps do this, but the TDI UserFunctions class (associated with the system object) has a writeln method with the following signature:

public void writeln(Writer w, String str)

So you could do something like

var str = "A line of text encoded as UTF-8";
system.writeln(fw, str);

The "trick" I suppose is setting up the Writer object - you'll probably need wrap some Java objects to be able to specify the encoding and do something like:

var outFile = new java.io File("c:\\dir\\outfile.txt");

var fw = new java.io BufferedWriter(new java.io OutputStreamWriter(
new java.io FileOutputStream(outFile), "UTF8"));

But there are far smarter TDI folks out there than me who may come up with something a bit more cleaner.

Anyway, give it a shot and see how it goes.

Cheers,
JD
John Dell'Oso
2017-02-14 00:24:09 UTC
Permalink
Raw Message
Post by John Dell'Oso
public void writeln(Writer w, String str)
So you could do something like
var str = "A line of text encoded as UTF-8";
system.writeln(fw, str);
var outFile = new java.io File("c:\\dir\\outfile.txt");
var fw = new java.io BufferedWriter(new java.io OutputStreamWriter(
new java.io FileOutputStream(outFile), "UTF8"));
But there are far smarter TDI folks out there than me who may come up with something a bit more cleaner.
Anyway, give it a shot and see how it goes.
Cheers,
JD
Sorry the code for setting up the Writer object should be:

var outFile = new java.io.File("c:\\dir\\outfile.txt");

var fw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
new java.io.FileOutputStream(outFile), "UTF8"));

JD
Kanon Yingwanna
2017-02-14 04:44:04 UTC
Permalink
Raw Message
Post by John Dell'Oso
Post by John Dell'Oso
public void writeln(Writer w, String str)
So you could do something like
var str = "A line of text encoded as UTF-8";
system.writeln(fw, str);
var outFile = new java.io File("c:\\dir\\outfile.txt");
var fw = new java.io BufferedWriter(new java.io OutputStreamWriter(
new java.io FileOutputStream(outFile), "UTF8"));
But there are far smarter TDI folks out there than me who may come up with something a bit more cleaner.
Anyway, give it a shot and see how it goes.
Cheers,
JD
var outFile = new java.io.File("c:\\dir\\outfile.txt");
var fw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
new java.io.FileOutputStream(outFile), "UTF8"));
JD
Thank you so much JD, It's working! Could you please also advise how to close the file?
Kanon Yingwanna
2017-02-14 04:49:35 UTC
Permalink
Raw Message
Post by John Dell'Oso
Post by John Dell'Oso
public void writeln(Writer w, String str)
So you could do something like
var str = "A line of text encoded as UTF-8";
system.writeln(fw, str);
var outFile = new java.io File("c:\\dir\\outfile.txt");
var fw = new java.io BufferedWriter(new java.io OutputStreamWriter(
new java.io FileOutputStream(outFile), "UTF8"));
But there are far smarter TDI folks out there than me who may come up with something a bit more cleaner.
Anyway, give it a shot and see how it goes.
Cheers,
JD
var outFile = new java.io.File("c:\\dir\\outfile.txt");
var fw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
new java.io.FileOutputStream(outFile), "UTF8"));
JD
Thank you so much JD, It's working! Could you please also advise how to close the file? I couldn't delete it
Kanon Yingwanna
2017-02-14 05:48:50 UTC
Permalink
Raw Message
Post by John Dell'Oso
Post by John Dell'Oso
public void writeln(Writer w, String str)
So you could do something like
var str = "A line of text encoded as UTF-8";
system.writeln(fw, str);
var outFile = new java.io File("c:\\dir\\outfile.txt");
var fw = new java.io BufferedWriter(new java.io OutputStreamWriter(
new java.io FileOutputStream(outFile), "UTF8"));
But there are far smarter TDI folks out there than me who may come up with something a bit more cleaner.
Anyway, give it a shot and see how it goes.
Cheers,
JD
var outFile = new java.io.File("c:\\dir\\outfile.txt");
var fw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
new java.io.FileOutputStream(outFile), "UTF8"));
JD
Thank you so much JD, It's working!!.
Kanon Yingwanna
2017-02-14 06:07:08 UTC
Permalink
Raw Message
Post by John Dell'Oso
Post by John Dell'Oso
public void writeln(Writer w, String str)
So you could do something like
var str = "A line of text encoded as UTF-8";
system.writeln(fw, str);
var outFile = new java.io File("c:\\dir\\outfile.txt");
var fw = new java.io BufferedWriter(new java.io OutputStreamWriter(
new java.io FileOutputStream(outFile), "UTF8"));
But there are far smarter TDI folks out there than me who may come up with something a bit more cleaner.
Anyway, give it a shot and see how it goes.
Cheers,
JD
var outFile = new java.io.File("c:\\dir\\outfile.txt");
var fw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
new java.io.FileOutputStream(outFile), "UTF8"));
JD
It's working!! JD, Thank you so much.
Kanon Yingwanna
2017-03-08 07:44:22 UTC
Permalink
Raw Message
Post by John Dell'Oso
Post by John Dell'Oso
public void writeln(Writer w, String str)
So you could do something like
var str = "A line of text encoded as UTF-8";
system.writeln(fw, str);
var outFile = new java.io File("c:\\dir\\outfile.txt");
var fw = new java.io BufferedWriter(new java.io OutputStreamWriter(
new java.io FileOutputStream(outFile), "UTF8"));
But there are far smarter TDI folks out there than me who may come up with something a bit more cleaner.
Anyway, give it a shot and see how it goes.
Cheers,
JD
var outFile = new java.io.File("c:\\dir\\outfile.txt");
var fw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
new java.io.FileOutputStream(outFile), "UTF8"));
JD
It can also define as below.

var fos = new java.io.FileOutputStream("D:\\output.txt");
var fileWriter = new java.io.OutputStreamWriter(fos, "TIS-620")
system.writeln(fileWriter, "some text")
fileWriter.close()

Loading...