我有这个基本程序工作,但它吐出了一个奇怪的答案。当我运行它,它给了我:用于Mac的Java文件输入
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0
Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
\f0\fs24
\cf0
CIS
260
is
cool.\
Let's
put
another
sentence
in
here.\
Programming
is
problem
driven.}
*
import java.util.Scanner;
import java.io.*;
public class FileIO
{
public static void main(String[] args)
{
File Fred = new File(System.getProperty("user.home"), "mytext.txt");
try
{
Scanner input = new Scanner(Fred);
while (input.hasNext())
{
System.out.println(input.next());
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
同意。我完全按照你在我的Mac上所写的那样尝试了这个源代码,它给了我预期的输出。我甚至用不同的编码(mac,unix,windows)保存了文件,但它被正确读取。问题一定是mytext.txt保存为RTF。 – Mirkules 2011-03-08 22:57:41
谢谢。我没有意识到,即使我将它作为一个.txt文件读取,但并未将其转换为一个。 – Mary 2011-03-08 23:05:00