Display Rich Text Format (.rtf) File [message #1717574] |
Mon, 14 December 2015 23:54 |
Arthur Chan Messages: 44 Registered: September 2015 |
Member |
|
|
Hi, I have a .rtf-file containing simplified Chinese characters. It was created in a Windows-10 env.
I am running eclipse Mars.1 in Windows-7 env. My Firefox and MS Word and WordPad can display the original .rtf-file, but eclipse is not.
My InputStreamReader and OuputStreamWriter are configured to read UTF-8, seems like it is not UTF-8
Is this an issue with eclipse or is it my coding, or an issue with UTF-8
This is what pops up in the console:
1 {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fprq2\fcharset134 SimSun;}}
2 {\*\generator Riched20 6.3.9600}{\*\mmathPr\mnaryLim0\mdispDef1\mwrapIndent1440 }\viewkind4\uc1
3 \pard\nowidctlpar\sa200\sl276\slmult1\b\f0\fs40\lang2052\'ce\'d2\'cf\'eb\'c2\'f2\'d7\'d4\'d0\'d0\'b3\'b5\par
4 \'b5\'d8\'cc\'fa\par
5 \'b8\'df\'cc\'fa\par
6 \'b9\'ab\'b9\'b2\'c6\'fb\'b3\'b5\'d5\'be\'d4\'da\'c4\'c7\'a3\'bf\par
7 \'bc\'fb\'bc\'d2\'c8\'cb\par
8 \par
etc etc
And this is the code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ReadWriteUTF8Files {
public static void main(String[] args) throws IOException{
// Inbound files deposited in this directory...
Path inPath = Paths.get("C:", "Users", "ARTHUR", "eclipse", "textFiles", "Îĵµ.rtf");
String strInPath = inPath.toString();
// Processed files to be archived to this directory...
Date todaysDate = new Date();
DateFormat df7 = new SimpleDateFormat("yyyyMMMdd-HHmmss");
String strDateTime = df7.format(todaysDate);
final String strArchive = ("C:/Users/ARTHUR/eclipse/ArchivedFiles/Îĵµ" + strDateTime + ".rtf");
File inFile = new File(strInPath);
Boolean canRead = inFile.canRead();
String aLine = null;
if (canRead) {
// FileReader reads from this text file in UTF-8 encoding
Reader readUTF8 = new InputStreamReader(new FileInputStream(strInPath), "UTF-8");
BufferedReader br = new BufferedReader(readUTF8);
LineNumberReader lnr = new LineNumberReader(br);
// writes to this file in default encoding
Writer writeUTF8 = new OutputStreamWriter(new FileOutputStream(strArchive), "UTF-8");
BufferedWriter bw = new BufferedWriter(writeUTF8);
//
while ((aLine = lnr.readLine()) != null) {
int lineNo = lnr.getLineNumber();
bw.write(lineNo + "\t" + aLine + "\r\n"); // If "\r\n" does not work (might be a file from Unix or Mac), then try bw.newLine() or bw.newLine(\r\n);
bw.newLine();
System.out.println(lineNo + "\t" + aLine);
}
// close the outside object lnr, no need to close inside objects
lnr.close();
// if you don't close bw, it won't flush to file and you get an empty file
bw.close();
} else {
System.out.println("File path does not exist " + strInPath);
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.09413 seconds