Superscript characters are resulting in to junk characters in generated CSV file when using ICsvListWriter - csv

I am trying to write superscript characters in .csv file. I am using method write(List<?> columns)of org.supercsv.io.ICsvListWriter. In generated .csv file the superscript character is coming along with junk character before it.
List columns = new ArrayList();
String myString = "abcd1";
columns.add(myString.replaceAll("1", "¹"));
csvWriter.write(columns);
In the generated .csv file it is coming as
abcd¹
I also tried with unicode but it is not helping.
columns.add(myString.replaceAll("1", "\u00B9"));
Any suggestion here please?

Found a solution for this problem. Correction was needed in creating ICsvListWriter object. Previously I was having this code where 'response' is HttpServletResponse.
CsvPreference preference = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE).useEncoder(new DefaultCsvEncoder()).build();
ICsvListWriter csvWriter = new CsvListWriter(response.getWriter(), preference);
This code is enhanced to this:
ServletOutputStream output = response.getOutputStream();
output.write(new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF });
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"));
CsvPreference preference = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE).useEncoder(new DefaultCsvEncoder()).build();
ICsvListWriter csvWriter = new CsvListWriter(writer, preference);
This fixed the issue and all of the superscript characters are now coming properly in generated CSV file without any junk characters. No mater whether I use actual superscript characters or their Unicode, this fix works.

Related

How to replace special symbols in a binary?

I am trying to read a pdf from a URL, return it as a binary and replace some characters. This is working for plain text with the following code but if the pdf has any special symbols like Trademark, copyright etc then my webservice is unable to return the result. Can some one please help me how to achieve this. The output should definitely be a binary output :
String html="";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream in = new URL(jsonobj.getString("xBody")).openStream();
int reads = in.read();
while(reads != -1){
baos.write(reads);
reads = in.read();
}
html= baos.toString();
The method baos.toString() internally calls new String(buffer), which uses the default encoding (the encoding actually being used by your system, probably not UTF-8). Try to provide the encoding explicitly, as follows:
String html = new String(baos.toByteArray(), "UTF-8");

Importing CSV file in MVC and converting it in JSON using C#

I am importing .CSV file from an angular app into MVC and i am able to get the files like this
Int32 strLen, strRead;
System.IO.Stream stream = Request.InputStream;
strLen = Convert.ToInt32(stream.Length);
byte[] strArr = new byte[strLen];
strRead = stream.Read(strArr, 0, strLen);
here the files which is being imported is converted into byte[] because i am reading the file using
System.IO.Stream stream = Request.InputStream
Then i convert it into string like this
string a = System.Text.Encoding.UTF8.GetString(strArr);
and try to split the content and retrieve the data but it becomes very complex, i wonder if there is any alternate way for it. In a simple .CSV file like this
I get the result after converting the byte[] to string like this
and once i apply logic for splitting the string and retrieving the data, the logic gets very messy like this
Is there any efficinet way where i can convert the imported .CSV file to JSON
Save stream as text file in to the TEMP folder.
Use any parcer for working with CSV file. (Example FileHelpers)
Use any Json helper to convert it to the output format. (Example: newtonsoft)
You can use Cinchoo ETL - an open source library, to convert CSV to JSON easily.
using (var parser = new ChoCSVReader("IgnoreLineFile1.csv")
.WithField("PolicyNumber", 1)
.WithField("VinNumber", 2)
.Configure(c => c.IgnoreEmptyLine = true)
.Configure(c => c.ColumnCountStrict = true)
)
{
using (var writer = new ChoJSONWriter("ignoreLineFile1.json")
.WithField("PolicyNumber", fieldName: "Policy Number")
.WithField("VinNumber", fieldName: "Vin Number")
)
writer.Write(parser.Skip(1));
}
In above, you can pass stream to the reader and writer as well for your requirement.
Hope this will help.
Disclaimer: I'm the author of this library.

How to use Groovy JsonOutput.toJson with data encoded with UTF-8?

I have a file with UTF-8 encoding.
I write a groovy script to load a file with a JSON structure, modify it and save it:
def originPreviewFilePath = "./xxx.json"
//target the file
def originFile = new File(originPreviewFilePath)
//load the UTF8 data file as a JSON structure
def originPreview = new JsonSlurper().parse(originFile,'UTF-8')
//Here is my own code to modify originPreview
//Convert the structure to JSON Text
def resultPreviewJson = JsonOutput.toJson(originPreview)
//Beautify JSON Text (Indent)
def finalFileData = JsonOutput.prettyPrint(resultPreviewJson)
//save the JSONText
new File(resultPreviewFilePath).write(finalFileData, 'UTF-8')
The problem is that JsonOutput.toJson transforms UTF-8 data to UNICODE. I don't understand why JsonSlurper().parse can use UTF-8 but not JsonOutput.toJson?
How to have JsonOutput.toJson use UTF-8? I need to have the exact inverse of JsonSlurper().parse
In case anyone is still struggling with this, the solution is to disable unicode escaping:
new JsonGenerator.Options()
.disableUnicodeEscaping()
.build()
.toJson(object)
This worked for me in Groovy 3:
StringEscapeUtils.unescapeJavaScript(
JsonOutput.prettyPrint(resultPreviewJson)
)
I believe that the encoding is applied at the incorrect statement while reading itself.
Change below statements from :
def originFile = new File(originPreviewFilePath)
def originPreview = new JsonSlurper().parse(originFile,'UTF-8')
To:
def originFile = new File(originPreviewFilePath).getText('UTF-8')
def originPreview = new JsonSlurper().parseText(originFile)

How to read a text file with other encoding than UFT8 or UTF16 in WinRT?

If I read a textfile using FileIO.ReadTextAsync, ReadLinesAsync or a DataReader, I can only specify a member of the UnicodeEncoding enum for the encoding. This includes for some reason only Utf8, Utf16BE and Utf16LE. How can I read a text file with another encoding (like Windows-1252 or even regular Unicode (with 2 Bytes for all characters)) then?
This may be important if Windows Store Apps share text files with Desktop applications or read text files from the internet.
Hans' comment actually gave the answer to my question. Sample for Windows-1252:
string filePath = ...
StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);
IBuffer buffer = await FileIO.ReadBufferAsync(file);
byte[] fileData = buffer.ToArray();
Encoding encoding = Encoding.GetEncoding("Windows-1252");
string text = encoding.GetString(fileData, 0, fileData.Length);
#JürgenBayer buffer.ToArray() wasn't available for me.
So, instead of writing:
string text = await FileIO.ReadTextAsync(file);
I wrote:
IBuffer buffer = await FileIO.ReadBufferAsync(file);
byte[] fileData;
CryptographicBuffer.CopyToByteArray(buffer, out fileData);
Encoding encoding = Encoding.GetEncoding("Windows-1252");
string text = encoding.GetString(fileData, 0, fileData.Length);

Replacing string in html dynamically in Android

I am using "loadDataWithBaseUrl(...)" to load a html file, stored in assets, to Webview. that contains a string "Loading..." and a rotating GIF. String "Loading..." is hard coded, and it'll not be localized. How to replace that string dynamically, so that it can be localized?
Please help me to resolve this.
There are various solutions I could think of :
Load a different asset file according to the current language (get the current language using Locale.getDefault()), This way you can translate your HTML files independently.
Use place holders in your HTML file (for instance #loading_message#), then load the asset file in a String, replace all the occurences of the placeholder by the appropriate localised message (String.replaceAll("#loading_message#", getText(R.string.loading_message).toString())), finally load the processed HTML into the WebView using the loadData(String data, String mimeType, String encoding) function.
To load the asset file, you can do something like that:
File f = new File("file:///android_asset/my_file.html");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String eachLine = br.readLine();
while(eachLine != null) {
sb.append(eachLine);
sb.append("\n");
eachLine = br.readLine();
}
// sb.toString is your HTML file as a String
I had a similar problem when using the WebView to show help text that should be translated.
My solution was to add multiple translated HTML files in assets and loading them with:
webView.loadUrl("file:///android_asset/" + getResources().getString(R.string.help_file));
For more details go to: Language specific HTML Help in Android
String str = "Loading ..."
String newStr = str.substring("Loading ".length());
newStr = context.getResourceById(R.string.loading) + newStr;
I hope the code is sufficiently clear to understand the idea: extract the string without "Loading " and concatenate it with the localized version of "Loading" string