Java Reading from file. Bad result

M R :

I want the program to read a file which has a text "1 2 3".

I'm doing it with code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.FileHandler;

public class ReadingFromFile {
    public static void main(String[] args) throws FileNotFoundException {
        String separator = File.separator;
        String path = separator + "Users" + separator + "aa" + separator + "Desktop" + separator + "test.rtf";


        File file = new File(path);

        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }

        scanner.close();
    }
}

But getting a result as:

    {\rtf1\ansi\ansicpg1251\cocoartf1671\cocoasubrtf200
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0

\f0\fs24 \cf0 1 2 3}

Process finished with exit code 0

How I can get a more clear result? (just 1 2 3) IntelliJ IDEA doesn't show any errors. Where is a mistake?

Joop Eggen :

The text is in fact rich text, RTF.

Old java swing can read HTML and also RTF. One then has a StyledDocument with fonts and styles, from which to extract the plain text.

Path path = Paths.get("/Users/aa/Desktop/test.rtf";
try (Reader reader = Files.newBufferedReader(path)) {
    JEditorPane pane = new JEditorPane();
    pane.setContentType("text/rtf");
    EditorKit kit = pane.getEditorKitForContentType("text/rtf");
    Document doc = pane.getDocument();
    kit.read(reader, doc, 0);
    String text = doc.getText(0, doc.getLength());
    System.out.println();
}

It probably is possible to do this without the swing component JEditorPane, using a StyledDocument from the EditorKit for RTFs.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161083&siteId=1