Can';t正确地使用Java访问和修改文本文件
我在用Java访问文本文件时遇到问题 我创建了一个简单的示例来演示我的问题。我有一个简单的框架,有一个Can';t正确地使用Java访问和修改文本文件,java,java-io,Java,Java Io,我在用Java访问文本文件时遇到问题 我创建了一个简单的示例来演示我的问题。我有一个简单的框架,有一个JButton和一个JComboBox。这样做的目的是在每次按下按钮时更新组合框中显示的列表 程序如下: 使用默认值(空字符串数组)创建一个JComboBox。使用名为history.txt的文件中的详细信息更新组合框;对于每次单击按钮,请更改文本文件的详细信息并再次更新组合框 代码如下: package xx; import java.awt.BorderLayout; import jav
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);
}
xx包;
导入java.awt.BorderLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
公共类Xx扩展了JFrame{
私有字符串filePath=“C:\\Users\\gilbert\\Documents\\11111.xls”;
私用钮扣;
专用JComboBox组合框;
公共Xx(){
字符串disp[]={“”“”};
comboBox=新的JComboBox(disp);
comboBox.setEditable(false);
comboBox.setPrototypeDisplayValue(“”);
试一试{
updatembobox(getFileHistory());
}捕获(IOEX异常){
Logger.getLogger(Xx.class.getName()).log(Level.SEVERE,null,ex);
}
布尔isempty=false;
BufferedReader BR=null;
buttonOpen=新的JButton(“打开文件”);
buttonOpen.setEnabled(真);
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
试一试{
BufferedReader bO=新的BufferedReader(新文件读取器(新文件(“C:\\PinPrinter\\history.txt”));
int count=comboBox.getSelectedIndex()+1;
for(int i=0;i
}
在第一次运行时,程序将创建一个history.txt
文件。当updateFileHistory()
方法尝试执行时,程序会给出一个错误
在您尝试重新运行之前,请删除C:\PinPrinter
文件夹,否则程序将从一开始就失败,因为getFileHistory()
要求不保存history.txt
文件