2

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?

upandgreen
  • 69
  • 5
  • 2
    You are reading a rich text file, not a plain text file. That's why you get all these extra lines. – DA. Jan 21 '19 at 10:21
  • 2
    `rtf` doesn't contain only text but also metainformations for instance related to formatting (like font, color, is it italic, bold etc.). If you want to only print 1 2 3 then it is easier to do so with `txt` file - you can create it with simpler editor like notepad. – Pshemo Jan 21 '19 at 10:22

1 Answers1

1

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138