我有一个作业,要求我从文本文件中读取代码。有人知道我的密码会有什么问题吗?
除了输出文本本身,它还输出额外的字符:
{\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf390{\fonttbl\f0\fswiss\fcharset0 Helvetica;}{\colortbl;\red255\green255\blue255;}\margl1440\margr1440\vieww10800\viewh8400\viewkind0\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\f0\fs24 \cf0 ana\nan\stop}
当它应该输出的时候:
分析
非数
停止播放
花盆
这是代码:
package anagram1;
import java.io.FileReader; //Access file systems and allows buffered reader
//to read
import java.io.BufferedReader; //Scanner
import java.util.Scanner;
public class Anagram1 {
public static void main(String[] args) throws Exception
{
String path;
Scanner prompt = new Scanner(System.in);
System.out.println("Enter file path.");
path = prompt.nextLine();
FileReader file = new FileReader(path); //here is where the file path is inserted
BufferedReader reader = new BufferedReader(file);
String text = ""; //stores what is inside file
String line = reader.readLine(); //keeps reading line after line of string given
while (line != null)
{
text += line;
line = reader.readLine();
}
System.out.println(text);
}
}谢谢你的帮助!
发布于 2014-02-14 07:30:38
编辑
您的文本文件似乎不仅包含了预期的四行代码,还包含了其他代码。
前内容
你确定你的文本文件只包含这四个字符串并且没有特殊的格式吗?您可能可以使用新创建的另一个文本文件进行检查。
发布于 2014-02-14 07:33:41
文件阅读器无法神奇地理解RTF格式,也不会神奇地删除所有格式标记。它从文件中读取字符,而不关心这些字符代表什么。
如果您想要这样做,就需要一个RTF文档解析器。或者您需要使您的文件成为纯文本文件,而不是RTF文档。
https://stackoverflow.com/questions/21773270
复制相似问题