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");
}
}
}
执行程序没有错.但是我输入数据以后,输入的信息没有保存到文本文件里面,
请高手帮我看看哪里有问题,

解决方案 »

  1.   

    String record = "\n"+sName.trim()+","+sId.trim()+","+sDs.trim()+","+sDb.trim();
      

  2.   

    String record = "\r\n"+sName.trim()+","+sId.trim()+","+sDs.trim()+","+sDb.trim();
    System.out.println(new String(c));
      

  3.   

    不是这样,我是要把字符连起来,放到TXT文件里面,!
      

  4.   

    import java.io.*;public class Demo {
    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");
    }
    }
    }
      

  5.   

    我执行tommy___2005() 给的程序也一样报错
    String record = "";
    for(String s : info) {    //这里出错,我改为FOREACH后也出错
    record += s;
    }
    提示:Syntax error on token "String", ? expect after this token
    是什么原因,我用的eclipse
      

  6.   

    他给你的代码是适用于jdk1.5以后的,你可以根据情况改一下。
    他的办法是对的,读取输入的字符的时候,不能使用byte数组,因为有编码问题。
      

  7.   

    我的JKD是1.6的.没有错啊,
    那应该怎么弄,?
      

  8.   

    我的JKD是1.6的啊,那应该怎么改啊?我不知道
      

  9.   

    请确定 文件 有没有 建立 如果 没有 请自己建立 或者 用函数 还有路径是否正确
    我测试成功~~格式
    不队 我的 JDK1.6
      

  10.   

    tommy___2005已经修改的很好了
    如果要改成jdk1.4的方式
    for(String s : info) {
    record += s;
    }
    改成
    for(int i=0; i<info.length; i++ ) {
    record += info[i];
    }
    就行了
      

  11.   

    我后面就是用的YOUTHON的方法,但是还是不行,另外有个地方要改,不然一样错
    System.out.println(c);  改成System.out.println(new String(c));
    才能正确显示HEAD,因为第一个C只是输出的一个地址,!
    现在我程序调对了,但是还是不能正常写入文本,我还是JKD1.6+ECLISPE
      

  12.   

    直接用System.out.println( head );就好了,不要转来转去的
    这句
    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 );