Java-Write in file

时间:2014-12-16 08:19:09

标签: java arrays joptionpane randomaccessfile

我希望从用户获取名称,姓氏和空间代码,并在写入文件后保存在一个数组中。我的代码没有编译器错误,但它不起作用。

public class WriteFile {

public static void main(String[] args){

    try {

        String array[][] = new String[100][2];
        for (int i = 0; i < array.length; i++) {
            RandomAccessFile raf=new RandomAccessFile("D://employee.txt","rw");
            String inputName=JOptionPane.showInputDialog("Please Insert First Name");
            array[i][0]=inputName;
            String inputLName=JOptionPane.showInputDialog("Please Insert Last Name");
            array[i][1]=inputLName;
            String inputMeliiC=JOptionPane.showInputDialog("Please Insert Melii Code");
            array[i][2]=inputMeliiC;

            raf.writeUTF(array[i][0]);
            raf.writeUTF(array[i][1]) ;
            raf.writeUTF(array[i][1]);

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }


}

3 个答案:

答案 0 :(得分:1)

你做错了很多事。

首先,为什么要在这里使用阵列?这是没有根据的。收集List

第二:.writeUTF()不会写文字。

第三:为什么要写输入?一次写下来。

第四:你根本没有关闭你的资源。

先请求输入,然后尝试写入文件。并且不要使用File,它已经过时了。使用它(假设Java 7 +):

final Path dst = Paths.get("d:\\employee.txt");

// Change open options if necessary
try (
    final BufferedWriter writer = Files.newBufferedWriter(dst,
        StandardCharsets.UTF_8,
        StandardOpenOption.CREATE, StandardOpenOption.APPEND);
) {
    // write your data
}

或者甚至更好,使用this。如果您按照我的建议收集了List中的所有员工数据,而不是数组,这就像以下一样简单:

Files.write(thePath, myList, StandardCharsets.UTF_8, yourOpenOptionsHere);

答案 1 :(得分:0)

几个问题:

  • 您将数组定义为new String[100][2]并使用它直到索引array[i][2]=inputMeliiC;当数组从0开始时,您应该将数组定义为新的String [100] [3]。您是否正在对阵列进行进一步处理?
  • 您没有编写inputMeliiC,而是复制两次执行raf.writeUTF(array[i][1]);的inputLName。你应该raf.writeUTF(array[i][2]);
  • 最重要的是你为什么写不工作,你需要清除缓冲区。所以你完成后应该raf.close();。确保UTF在您输入时不会以简单文本写入,并且您在两种读写模式下都打开文件。

答案 2 :(得分:0)

好的,您可以使用此修改后的代码:

public static void main(String[] args){
            RandomAccessFile raf = null;
    try {

        String array[][] = new String[2][3];
        raf=new RandomAccessFile("D:\\employee.txt","rw");
        for (int i = 0; i < array.length; i++) {
            String inputName=JOptionPane.showInputDialog("Please Insert First Name");
            array[i][0]=inputName;
            String inputLName=JOptionPane.showInputDialog("Please Insert Last Name");
            array[i][1]=inputLName;
            String inputMeliiC=JOptionPane.showInputDialog("Please Insert Melii Code");
            array[i][2]=inputMeliiC;

            raf.writeChars(array[i][0]);
            raf.writeChar(':');
            raf.writeChars(array[i][1]) ;
            raf.writeChar(':');
            raf.writeChars(array[i][2]);
            raf.writeChars("\n");

        }
        raf.seek(0);

        String str = raf.readLine();
        while(str != null ){
            System.out.println(str);
            String arr[] = str.split(":");
            System.out.println(Arrays.asList(arr));
            str = raf.readLine();
        }

        raf.close();
    } catch (FileNotFoundException e) {
                try {
                    raf.close();
                } catch (IOException ex) {
                    Logger.getLogger(Ideone.class.getName()).log(Level.SEVERE, null, ex);
                }
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
                try {
                    raf.close();
                } catch (IOException ex) {
                    Logger.getLogger(Ideone.class.getName()).log(Level.SEVERE, null, ex);
                }
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

}