0

我想打印用户输入的所有字母,但问题是,我的程序只打印用户输入的最后一个值,并且只有最后一个值记录在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){
       }
     }
    }
  }
  }
4

3 回答 3

2

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

    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);
        }
    }
    //...
于 2012-12-18T15:56:54.653 回答
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();
}
于 2012-12-18T15:56:11.750 回答
0

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

作为补充,如果您使用的是 Java 7,则可以将 try 与资源一起使用(因此您不必费心关闭它):

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.
}
于 2012-12-18T16:20:38.200 回答