UTF8.txt file to UTF8 xml

Posted on 2006-07-05
Last Modified: 2008-02-01
 I have a UTF8 file that I need to read and convert to xml (allso utf8).
The UTF file is in CZECH and I'm on an US system.
I have tree questions:
I'm reading the file like this:
            input = new BufferedReader( new FileReader(aFile) );
            String line = null;
            int i = 0;
            while (( line = input.readLine()) != null && i < 100){
                i ++;
                System.out.println("l:" + line);

1. When I print it out, I get lines like: Oznámení zadávacího řízenÃ. And that does not look correct. (I don't know Czech, but I'd ecpect som Czech letters.) ­ Any Idea what I do wrong?
2. I need to read the file line for line and I neet to remove "\t" "\n" and "  " from the start of the line. Is there any way I can print these "hidden" chars so I can see what the original file is using?
3. I have some text like this: "
  PD: 20060620
  ND: 121873-2006"
And I need to convert it to "<ti><country>UK</country><city>Cardiff</city><name>KOBO CESIE EEIG</name></ti>
Any ideas how I can split the text like this? Just to make things worse, sometimes the TI: text has several lines separatet with "\n" and serveral " " (banks or space).

Question by:kristian_gr
  • 4
  • 2
  • 2
  • +1
LVL 86

Expert Comment

ID: 17041076
You need to use a font that support Czech charcters


should be

line =
LVL 92

Expert Comment

ID: 17041080

            input = new BufferedReader( new InputStreamReader(new FileInputStream(aFile), "UTF8") );

you'll also need to have a font installed that support czech to display it
LVL 14

Expert Comment

ID: 17041086
System.out.println(...) uses a console or a command line, so don't expect to see there the foreign charaters, You could see them only in an awt or swing component like JTextArea and only in the font which has the glyphs defined for the language You are using. I would suggest You to convert the text file to xml and only then check whether the characters are recognizable.
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 86

Expert Comment

ID: 17041090
>>Just to make things worse,

If it's not too big, you'd be better off reading it all into one String

Author Comment

ID: 17041321
ok, this seams to work:  
            BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("c:/test.txt")), "UTF8"));
            BufferedReader input = new BufferedReader( new InputStreamReader(new FileInputStream(aFile), "UTF8") );
            String line = null;
            int i = 0;
            while (( line = input.readLine()) != null && i < 400){
                i ++;
//                line = line.trim();

If I open my test.txt in word it seams to have the right cahracters.
Using trim is not a good idea. The file is formated in a way that using 4 blanks/space in the start of a line, it indicates that it realy belongs to the line above.
I therfore thik I need to do if(! String line starts with 4 blanks){w.newline();}
But how can I test if a line starts with 4 blanks?  
LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 17041367
>>But how can I test if a line starts with 4 blanks?  

if (line.startsWith("    "))

Author Comment

ID: 17042044
Some days even the easiest things it hard. This is one of those.
tnx CEHJ.

And sinse I know you are good at regex'es, you probably have an idea about this:
Some of my lines starts with XX: as in two Uppercase chars, and a ":". If that occures I'd like to split the String at the first ":" into String[0] and String[1]. But the text in String[1] might allso contain serveral ":".
eks: String test = "PT: testString : has some more text";
String[0] result: PT
String[1] result: "testString : has some more text";
LVL 86

Expert Comment

ID: 17042081
if line.matches("^[A-Z]{2}:.+")) {
    String[]  tokens  = line.split(":", 2);
LVL 92

Expert Comment

ID: 17049149
how does that answer your original question?
the important thing was to specify the appropriate encoding.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
configure dependency in POM for new database 3 33
hibernate jars 4 45
String array comparison 4 38
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question