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);
}
}
}
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\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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是如何创建 mytext.txt 的?看起来您正在读取 RTF 文件而不是平面文本文件。尝试使用简单的文本编辑器重新创建 mytext.txt,然后重试。
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.
您的
mytext.txt
文件似乎保存为富文本而不是纯文本,因此它包含格式信息以及实际内容。再次将文本文件保存为纯文本,您应该会得到您期望的结果。
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.