我已经有了这个基本程序,但它给出了一个奇怪的答案。当我运行该程序时,它给我:
{\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);
}
}
}
最佳答案
您是如何创建 mytext.txt 的?看起来您正在读取 RTF 文件而不是平面文本文件。尝试使用简单的文本编辑器重新创建 mytext.txt,然后重试。
关于java - Mac 版 Java 中的文件输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5239324/