开发者

File input in Java for Mac

开发者 https://www.devze.com 2023-02-15 20:07 出处:网络
I\'ve got this basic program working but it\'s spitting out a weird answer. When I run the program it gives me:

I've got this basic program working but it's spitting out a weird answer. When I run the program it gives me:

{\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\pardir开发者_如何学编程natural
\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);

    }

}


}


How did you create mytext.txt? It looks like you're reading in an RTF file rather than a flat text file. Try recreating mytext.txt with a simple text editor and try it again.


It looks like your mytext.txt file was saved as rich text, instead of plain text, so it contains formatting information as well as the actual content.

Save the text file again as plain text and you should get the result you expect.

0

精彩评论

暂无评论...
验证码 换一张
取 消