Java 通过文件编写器打印所有值
我想打印用户将输入的所有字母,但问题是,我的程序只打印用户将输入的最后一个值,并且只有最后一个值记录在Java 通过文件编写器打印所有值,java,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(Stri
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){
}
}
}
}
}
import java.io.*;
公共类NewClass2{
公共静态void main(字符串args[])引发IOException{
BufferedReader buff=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“请输入逗号分隔的字母:”);
字符串str=buff.readLine();
对于(int i=0;i
只有在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();
}
BufferedWriter out;
试一试{
FileWriter fstream=新的FileWriter(“Ascii.txt”);
out=新的缓冲写入程序(fstream);
对于(int i=0;i
问题在于,对于要转储到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();
}
}
BufferedReader buff=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“请输入逗号分隔的字母:”);
字符串str=buff.readLine();
BufferedWriter out=null;
尝试
{
FileWriter fstream=新的FileWriter(“Ascii.txt”);
out=新的缓冲写入程序(fstream);
对于(int i=0;i
为了从输出中删除逗号,我建议使用:
/。。。
字符串[]splittedStr=str.split(“,”);
对于(int i=0;i0)
{
char c=splittedStr[i].charAt(0);
int j=c;
out.写入(c+“=”+j);
}
}
//...
如前所述,每次写入文件时都会关闭流。可能您正在寻找flush()
方法
作为补充,如果您使用的是Java 7,您可以使用try with resources(这样您就不必费心关闭它):
您可以使用
String.split(“,”
)来获取逗号之间数字的String[]
。您正在打开/关闭每个循环上的文件流。因此,程序完成后,输出中只显示最后一行。将打开/关闭代码移到循环之外。哦,谢谢您,先生,但是如何删除“Ascii.txt”中的逗号哦,谢谢,先生,但是如何删除“Ascii.txt”中的逗号呢?
//...
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);
}
}
//...
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.
}