Java扫描仪读取垃圾 [英] Java scanner reading garbage

查看:70
本文介绍了Java扫描仪读取垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java扫描仪读取文本文件.

I am reading a text file using java Scanner.

try {
            while(sc.hasNextLine()) {
                //Read input from file
                inputLine = sc.nextLine().toUpperCase();
                System.out.println(inputLine);
}

以上给出了以下输出,而我的文本文件仅包含"aabbcc". 如何避免扫描仪扫描垃圾? 谢谢.

The above gives below output while my text file only includes "aabbcc". How to avoid scanner from scanning the garbage? Thanks.

{\RTF1\ANSI\ANSICPG1252\COCOARTF1265\COCOASUBRTF210
{\FONTTBL\F0\FSWISS\FCHARSET0 HELVETICA;}
{\COLORTBL;\RED255\GREEN255\BLUE255;}
\PAPERW11900\PAPERH16840\MARGL1440\MARGR1440\VIEWW10800\VIEWH8400\VIEWKIND0
\PARD\TX566\TX1133\TX1700\TX2267\TX2834\TX3401\TX3968\TX4535\TX5102\TX5669\TX6236\TX6803\PARDIRNATURAL

\F0\FS24 \CF0 AABBCC}

推荐答案

您正在阅读RTF文档.如果您只想阅读文本,则可以尝试将其读取为字节数组,然后使用swings rtfeditorkit解析文本.

You are reading a RTF Document. If you want to read the text only you can try reading it into a byte array and parsing out the text using swings rtfeditorkit.

Path path = Paths.get("path/to/file");
byte[] data = Files.readAllBytes(path);

RTFEditorKit rtfParser = new RTFEditorKit();
Document document = rtfParser.createDefaultDocument();
rtfParser.read(new ByteArrayInputStream(data), document, 0);
String text = document.getText(0, document.getLength());

这篇关于Java扫描仪读取垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆