通过文件编写器打印所有值

时间:2012-12-18 15:48:58

标签: java

我想打印用户输入的所有字母,但事实是,我的程序只打印用户输入的最后一个值,只有最后一个值记录在Ascii.txt中。看起来应该是这样的

例如:用户输入A,B,c,C

我也想删除逗号,但我不能:(

“Ascii.txt”中的输出应为:

A = 65
B = 66
c = 99
C = 67
请不要嘲笑我,因为我还是一名学生并且很喜欢编程,非常感谢

import java.io.*;

public class NewClass2{
  public static void main(String args[]) throws IOException{



  BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));

  System.out.println("Please Enter  letters separated by comma: ");

  String str = buff.readLine();
  for ( int i = 0; i < str.length(); ++i )
  {
    char c = str.charAt(i);
    int j = (int) c;
    System.out.println(c +" = " + j);
    {
      try
      {
         FileWriter fstream = new FileWriter("Ascii.txt");
         BufferedWriter out = new BufferedWriter(fstream);

         out.write(c+"  =  "+j);
         out.close();

       }catch (Exception e){
       }
     }
    }
  }
  }

3 个答案:

答案 0 :(得分:2)

问题是您关闭并重新打开要转储到ASCII文件的每个字符的FileStream。因此,在写入字符之前,您的文件将被清空。只需在循环外移动流的创建和关闭。

    BufferedReader buff = new BufferedReader(new  InputStreamReader(System.in));
    System.out.println("Please Enter  letters separated by comma: ");

    String str = buff.readLine();
    BufferedWriter out = null;
    try
    {
        FileWriter fstream = new FileWriter("Ascii.txt");
        out = new BufferedWriter(fstream);
        for ( int i = 0; i < str.length(); ++i )
        {
            char c = str.charAt(i);
            int j = c;
            System.out.println(c + " = " + j);

            out.write(c + "  =  " + j);

        }
    }

    catch ( Exception e )
    {
        ;
    }
    finally
    {
        if ( out != null )
        {
            out.close();
        }
    }

为了从输出中删除逗号,我建议使用String.split()

    //...          
    String[] splittedStr = str.split(",");
    for ( int i = 0; i < splittedStr.length; i++ )
    {
        if ( splittedStr[i].length() > 0 )
        {
            char c = splittedStr[i].charAt(0);
            int j = c;

            out.write(c + "  =  " + j);
        }
    }
    //...

答案 1 :(得分:0)

您只能在str。

上循环后写入您的文件
BufferedWriter out;
try{
FileWriter fstream = new FileWriter("Ascii.txt");
out = new BufferedWriter(fstream);
for ( int i = 0; i < str.length(); ++i ){
  char c = str.charAt(i);
  int j = (int) c;
  System.out.println(c +" = " + j);
  out.write(c+"  =  "+j);
} catch ...{
...
} finally {
   if (out!=null) 
     out.close();
}

答案 2 :(得分:0)

正如所指出的,每次写入文件时都会关闭流。也许您正在寻找flush()方法。

为了补充,如果你使用Java 7,你可以使用try with resources(所以你不必费心关闭它):

BufferedReader buff = new BufferedReader(new  InputStreamReader(System.in));
System.out.println("Please Enter  letters separated by comma: ");

String str = buff.readLine();

try( BufferedWriter out = new BufferedWriter(new FileWriter("Ascii.txt"))){
   //Using split
   String[] splitted = str.split(",");
   for(String s : splitted){
      char c = s.charAt(0);
      int j = c;
      out.write(c + "  =  " + j);
   }
}catch(Exception E){
   //You really should catch specific exceptions here.
}