日期:2014-05-20  浏览次数:20782 次

求帮助怎么样用printStream类在e.txt中写上一首唐诗呢且是从键盘输入的
求助我只会用printWriter的不符合题目要求

------解决方案--------------------
探讨

使用 java.util.Scanner 读入
使用 java.io.PrintStream 输出

PrintStream out = new PrintStream("e.txt", "GBK");
Scanner scanner = new Scanner(System.io);
while(scanner.hasNextLine()){
out.println(sca……

------解决方案--------------------
还有是System.in 不是System.io
------解决方案--------------------
Java code

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Ansewer29
{
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        File file = new File("d:" + File.separator + "e.txt");
        FileOutputStream fout = null;
        try
        {
            fout = new FileOutputStream(file);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        PrintStream ps = null;
        try
        {
            ps = new PrintStream(fout, true, "GBK");
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        Scanner sc = new Scanner(System.in);
        String str = null;
        System.out.println("请输入古诗,按'q'退出!");

        while (sc.hasNextLine())
        {
            str = sc.nextLine();
            if (str.equals("q"))
            {
                System.out.println("退出,Bye");
                break;
            }
            ps.println(str);
        }
        try
        {
            fout.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        ps.close();
    }
}