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
上面为程序执行,为什么会是这种结果
{
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
上面为程序执行,为什么会是这种结果
解决方案 »
- 1000分:请教一个JNA 调用 DLL的参数传递
- 老生常谈,JAVA传参数到底是值传递还是引用?
- List转化二维数组
- 关于各种数值类型的疑难
- 关于jdom怎么解析XML流
- 我好像找到jdk正则表达式的bug了,大家来鉴定一下。。。。。。。。
- 类似于JBuilder中的代码补齐,用Swing怎么做?
- 新手求助,一个hello word
- 菜鸟关于抽象函数和接口的问题
- Resin中bin目录下的httpd.exe , srun.exe , standalone.exe 分别是什么意思?
- 一个文件在被其他程序写的时候,java中其File对象的canWrite属性是否为false?
- ~~~~~~~~高分。JAVA调用VC++写的DLL。如何使VC++的函数使用JAVA的String类型的字符串!
所以你看到的数据都连起来了你输入的是两行数据,所以info[2],info[3]都是null,print后就是“null”
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 (FileNotFoundException fe)
{
System.out.println("File Not Found");
}
catch (IOException ie)
{
System.out.println("IO Exception");
}
}}
搞不懂你那一堆null是怎么出来的?敲个回车也该是个空呀