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 (FileNotFoundException fe)
{
System.out.println("File Not Found");
}
catch (IOException ie)
{
System.out.println("IO Exception");
}
}
}如果执行两次程序,你们想想会有什么结果呢,!就会出现两个HEAD变量的信息,应该怎么避免,还有我应该怎么解决空值问题
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");
}
}
}如果执行两次程序,你们想想会有什么结果呢,!就会出现两个HEAD变量的信息,应该怎么避免,还有我应该怎么解决空值问题
import java.io.BufferedWriter;
import java.io.File;
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) {
// 每次需要输入的数量
final int COUNT = 2;
// 标题
final String HEAD = "Name,ID,DS,DB";
// 文件名
final String FILENAME = "f:/student.txt";
String record = "";
File dir = new File(FILENAME);
if (!dir.exists()) {
record = HEAD + "\r\n";
}
try {
BufferedWriter fo = new BufferedWriter(new FileWriter(FILENAME, true));
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(HEAD);
for (int i = 0; i < COUNT; i++) {
System.out.printf("请输入第[%d]个数据:", i+1);
String info = reader.readLine();
// 对输入的数据进行检验
if(!info.matches(".+,\\d+,\\d+,\\d+")){
System.out.println(" ** 不符合规范,请重新输入**");
i--;
}else{
record += info + "\r\n";
}
}
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");
}
}
}