帮我看看这个简单的问题..!
import java.io.*;
public class temp
{
public static void main(String [] args)
{
try
{
String head = "Name,ID,DS,DB ";
byte[] c = new byte[40];
c = head.getBytes();
System.out.println(new String(c));
String record = "\n ";
BufferedWriter fo = new BufferedWriter(new FileWriter( "c:\\student.txt ",true));
String[] info = new String[4];
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < info.length; i++)
{
info[i] = reader.readLine();
record += info[i];
}
System.out.println(record);
fo.write(record);
fo.close();
}
catch (
FileNotFoundException fe)
{
System.out.println( "File Not Found ");
}
catch (
IOException ie)
{
System.out.println( "IO Exception ");
}
}
}
我想让程序实现输出字符保存到文本文件里
其格式为
Name,ID,DS,DB
a 001 90 90
b 002 90 96
当我输入两组数据以后,程序执行结果与预期结果不同,我搞了半天也没搞懂为什么,
a 001 90 90b 002 99 99null
nullnullnullnull
上面为程序执行,为什么会是这种结果
------解决方案--------------------修改如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import
java.io.FileNotFoundException;
import java.io.FileWriter;
import
java.io.IOException;
import java.io.InputStreamReader;
// 注意类名要大写
public class Temp
{
public static void main(String [] args)
{
try
{
String head = "Name,ID,DS,DB ";
byte[] c = new byte[40];
c = head.getBytes();
System.out.println(new String(c));
String record = " ";
// 判断文件是否存在
File dir = new File( "c:\\student.txt ");
if(dir.exists())
{
// 是否有内容
FileInputStream infile = new FileInputStream(dir);
if(infile.read() < 0)
{
record = "Name ID DS DB " + "\r\n ";
}
infile.close();
}
BufferedWriter fo = new BufferedWriter(new FileWriter( "c:\\student.txt ",true));
String[] info = new String[4];
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < info.length; i++)
{
info[i] = reader.readLine();
record += info[i] + "\t\n ";
}
System.out.println(record);
fo.write(record);
// 换行
fo.write( "\r\n ");
fo.close();
}
catch (File
NotFoundException fe)
{
System.out.println( "File Not Found ");
}
catch (IOException ie)