无法正确访问和修改Java文本文件

时间:2014-05-20 20:38:06

标签: java java-io

我在使用Java访问文本文件时遇到问题。

我创建了一个简单的例子来演示我的问题。我有一个简单的框架,其中包含一个JButton和一个JComboBox。这个想法是每次按下按钮时更新组合框中显示的列表。

流程如下:

使用默认值(空字符串数组)创建JComboBox。使用名为history.txt的文件中的详细信息更新组合框;对于每个按钮单击,更改文本文件的详细信息并再次更新组合框。

以下是代码:

package xx;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;


public class Xx extends JFrame{


private String filePath = "C:\\Users\\gilbert\\Documents\\11111.xls";
private JButton buttonOpen;
private JComboBox comboBox;
public Xx(){

    String disp[] = {"",""};
    comboBox = new JComboBox(disp);
    comboBox.setEditable(false);
    comboBox.setPrototypeDisplayValue("                                                   ");
    try {
        updateComboBox(getFileHistory());
    } catch (IOException ex) {
        Logger.getLogger(Xx.class.getName()).log(Level.SEVERE, null, ex);
    }

    boolean isempty  =false;
    BufferedReader BR = null;
    buttonOpen = new JButton("Open File");
    buttonOpen.setEnabled(true);
    buttonOpen.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){

           try {
               BufferedReader bO = new BufferedReader(new FileReader(new File("C:\\PinPrinter\\history.txt")));
               int count = comboBox.getSelectedIndex() + 1;
               for(int i = 0; i < count; i++){
                   String readLine = bO.readLine();
               }
               filePath = bO.readLine();

               bO.close();
               updateFileHistory();
               updateComboBox(getFileHistory());
           } catch (FileNotFoundException ex) {
               Logger.getLogger(Xx.class.getName()).log(Level.SEVERE, null, ex);
           } catch (IOException ex) {
               Logger.getLogger(Xx.class.getName()).log(Level.SEVERE, null, ex);
           }


       } 
    });
    add(buttonOpen, BorderLayout.SOUTH);
    add(comboBox, BorderLayout.NORTH);
}
private  String[] getFileHistory() throws FileNotFoundException, IOException{

    int count;
    String tempList[] = null;
    File dir = new File("C:\\PinPrinter");
    if(!dir.isDirectory()){
        boolean made;
        made = dir.mkdir();
    }


    File fileH = new File("C:\\PinPrinter\\history.txt");
    boolean check;
    check = fileH.createNewFile();
    if(check){
        BufferedWriter  gfhw = new BufferedWriter(new FileWriter(fileH));
        gfhw.write("0");
        gfhw.newLine();
        tempList = new String[1];
        tempList[0] = "0";
        gfhw.close();
    }
    else{

        BufferedReader gfhr = new BufferedReader(new FileReader(fileH));
        count = Integer.parseInt(gfhr.readLine().substring(0, 1));
        tempList = new String[count + 1];
        tempList[0] = String.valueOf(count);
        for(int i = 1; i < count + 1; i++){
            tempList[i] = gfhr.readLine();
        }
        gfhr.close();
    }
    return tempList;
}



private void updateFileHistory() {

    String tempList[] = null;

    BufferedReader ufhR;
    BufferedWriter ufhW;
    CompleteTerminate:
    while(true){
        try {
            ufhR = new BufferedReader(new FileReader(new File("C:\\PinPrinter\\history.txt")));
            int count = Integer.parseInt(ufhR.readLine());
            if(count < 10){
                count++;    
            }
            tempList = new String[count + 1];
            tempList[0] = String.valueOf(count);
            tempList[1] = filePath;
            for(int i = 2; i < count; i++){
                tempList[i] = ufhR.readLine();
                if(tempList[2].equals(tempList[1])){
                    break CompleteTerminate;
                }
            }
            ufhR.close();

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Xx.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Xx.class.getName()).log(Level.SEVERE, null, ex);
        }


        try {
                ufhW = new BufferedWriter(new FileWriter(new File("C:\\PinPrinter\\history.txt")));
                for(String item:tempList){
                    ufhW.write(item);
                    ufhW.newLine();
                }
                ufhW.close();

        } catch (IOException ex) {
            Logger.getLogger(Xx.class.getName()).log(Level.SEVERE, null, ex);
        }

        }
}

private void updateComboBox(String[] tempList){
    comboBox.removeAllItems();
    int count = Integer.parseInt(tempList[0]);
    for(int i = 1; i < count + 1; i++){
        comboBox.addItem(String.valueOf(i)+ ": "+ tempList[i].subSequence(tempList[i].lastIndexOf("\\") + 1, tempList[i].lastIndexOf("s") + 1));
    }
}


public static void main(String[] args) {

    Xx cool = new Xx();
    cool.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    cool.setVisible(true);
}

}

首次运行时,程序将创建一个history.txt文件。当updateFileHistory()方法尝试执行时,程序会出错。

在您尝试重新运行之前删除C:\PinPrinter文件夹或程序将从一开始就失败,因为getFileHistory()要求history.txt文件不存在或不为空。

0 个答案:

没有答案