package s;
import java.io.*;public class persons {
public static void main(String[] args)
{
try
{
String head = "Name,ID,DS,DB";
byte[] c = new byte[40];
c = head.getBytes();
System.out.println(c);
FileOutputStream fo = new FileOutputStream("c:\\student.txt");
byte [] name = new byte[40];
byte [] id = new byte[4];
byte [] ds = new byte[3];
byte [] db = new byte[3];
System.in.read(name);
System.in.read(id);
System.in.read(ds);
System.in.read(db);
String sName = new String(name);
String sId = new String(id);
String sDs = new String(ds);
String sDb = new String(db);
String record = "\n"+sName+","+sId+","+sDs+","+sDb;
c = record.getBytes();
fo.write(c);
fo.close();
}
catch(FileNotFoundException fe)
{
System.out.println("File Not Found");
}
catch(IOException ie)
{
System.out.println("IO Exception");
}
}
}
执行程序没有错.但是我输入数据以后,输入的信息没有保存到文本文件里面,
请高手帮我看看哪里有问题,
import java.io.*;public class persons {
public static void main(String[] args)
{
try
{
String head = "Name,ID,DS,DB";
byte[] c = new byte[40];
c = head.getBytes();
System.out.println(c);
FileOutputStream fo = new FileOutputStream("c:\\student.txt");
byte [] name = new byte[40];
byte [] id = new byte[4];
byte [] ds = new byte[3];
byte [] db = new byte[3];
System.in.read(name);
System.in.read(id);
System.in.read(ds);
System.in.read(db);
String sName = new String(name);
String sId = new String(id);
String sDs = new String(ds);
String sDb = new String(db);
String record = "\n"+sName+","+sId+","+sDs+","+sDb;
c = record.getBytes();
fo.write(c);
fo.close();
}
catch(FileNotFoundException fe)
{
System.out.println("File Not Found");
}
catch(IOException ie)
{
System.out.println("IO Exception");
}
}
}
执行程序没有错.但是我输入数据以后,输入的信息没有保存到文本文件里面,
请高手帮我看看哪里有问题,
System.out.println(new String(c));
public static void main(String[] args) {
try {
String head = "Name,ID,DS,DB";
byte[] c = new byte[40];
c = head.getBytes();
System.out.println(c); BufferedWriter fo = new BufferedWriter(new FileWriter("d:\\student.txt"));
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();
}
String record = "";
for(String s : info) {
record += s;
}
fo.write(record);
fo.close();
} catch (FileNotFoundException fe) {
System.out.println("File Not Found");
} catch (IOException ie) {
System.out.println("IO Exception");
}
}
}
String record = "";
for(String s : info) { //这里出错,我改为FOREACH后也出错
record += s;
}
提示:Syntax error on token "String", ? expect after this token
是什么原因,我用的eclipse
他的办法是对的,读取输入的字符的时候,不能使用byte数组,因为有编码问题。
那应该怎么弄,?
我测试成功~~格式
不队 我的 JDK1.6
如果要改成jdk1.4的方式
for(String s : info) {
record += s;
}
改成
for(int i=0; i<info.length; i++ ) {
record += info[i];
}
就行了
System.out.println(c); 改成System.out.println(new String(c));
才能正确显示HEAD,因为第一个C只是输出的一个地址,!
现在我程序调对了,但是还是不能正常写入文本,我还是JKD1.6+ECLISPE
这句
for(int i=0; i<info.length; i++ ) {
record += info[i];
}
改成
for(int i=0; i<info.length; i++ ) {
record += info[i] + ",";
}
record = record.substring( 0,record.length()-1 );