[英]why won't it read?
我想問一下我的程序有什么問題..我的程序是關於堆棧的,它沒有錯誤,但我的問題是,我們的要求是在堆棧中執行所有操作然后,即使你關閉程序,下一次你會打開它,以前的數據仍應顯示..我使用了文件編寫器和文件讀取器,但每次我關閉它,然后再次打開它,以前的數據不會出現..有什么建議嗎? 謝謝..這是我的節目..
import java.util.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class STACKS extends JFrame implements ActionListener
{
private JButton push,pop,peek,display,exit;
private JLabel Stack;
static int arr[],x=0,b=0,xy=0,c=0;
static String in="",INDEX="",d="",s="";
static String id[]={};
public STACKS()
{
super("STACKS MENU!^_^");
Container c=getContentPane();
c.setLayout(new FlowLayout());
Stack=new JLabel("STACKS MENU!^_^"); c.add(Stack);
push=new JButton("Push!^-^");
pop=new JButton("Pop!^-^");
peek=new JButton("Peek!^-^");
display=new JButton("Display!^-^");
exit=new JButton("Exit!^-^");
push.addActionListener(this); c.add(push);
pop.addActionListener(this); c.add(pop);
peek.addActionListener(this); c.add(peek);
display.addActionListener(this); c.add(display);
exit.addActionListener(this); c.add(exit);
setVisible(true);
setSize(150,250);
}
public static void saveMe() throws IOException
{
File data1=new File("Sample.txt"); //a file was created...
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(data1,true)));
for(int x=0;x<4;x++)
{
out.write(":"+arr[x]); xy=1; //here in this section, I put : in every elements inside the array..
}
out.close();
}
public static void readMe() throws IOException
{
Scanner txtFile=(new Scanner("Sample.txt"));
for(int y=0;x<id.length;y++)
{
s=txtFile.nextLine();
id=s.split(":");
arr[y]=Integer.parseInt(id[y]);
}
}
public void actionPerformed(ActionEvent a)
{
try
{
readMe(); //here is where the previous data will be read..
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"File not found! readme !^,^");
}
if (xy==0)
{
INDEX=JOptionPane.showInputDialog(null,"Enter LENGTH of the array!");
c=Integer.parseInt(INDEX); xy=1;
arr=new int[c];
}
if(a.getSource()==push)
{
in=JOptionPane.showInputDialog(null,"Enter integer to be pushed!");
b=Integer.parseInt(in);
arr[x]=b; x+=1;
if(x==c)
{
JOptionPane.showMessageDialog(null,"WARNING! The stacks are full, please pop something!^-^");
}
if(x>c)
{
JOptionPane.showMessageDialog(null,"Sorry, the stacks are full,please pop something first!^-^");
x-=1;
}
}
else if(a.getSource()==pop)
{
arr[x-1]=0;x-=1;
JOptionPane.showMessageDialog(null,"The value has been popped!^-^");
if(x==0)
{
JOptionPane.showMessageDialog(null,"The stacks are empty, push something!^-^");
}
}
else if(a.getSource()==peek)
{
JOptionPane.showMessageDialog(null,"The value is "+arr[x-1]+"! ^-^");
}
else if(a.getSource()==display)
{
for(int y=c-1;y>-1;y--)
{
d+="*** "+arr[y]+" ***\n";
}
JOptionPane.showMessageDialog(null,"The value inside the stacks are:\n"+d);
d="";
}
else if(a.getSource()==exit)
{
System.exit(0);
}
try
{
saveMe(); //here is where the file will be saved..
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"File not found!^,^");
}
}
public static void main(String args[])
{
STACKS pot=new STACKS();
pot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
實際上這里有很多問題。
就亞歷山大的答案而言,你會在每次推/彈調用后保存,因此正在調用saveMe()
。
但是在saveMe()
,你寫道:
在堆棧的第一個元素之前,所以讀回來將總是給出一個額外的空白元素。 在寫入之前檢查是否x!= 0 :
.
在readMe()
,您正在為id.length調用nextLine()
,這在兩個方面是不正確的:
x<id.length
作為for
循環的結束條件(無論如何x都是錯誤的,它應該在你的上下文中是y)。 使用txtFile.hasNext()
檢查是否還有更多要讀取的堆棧元素。 :
with textFile.useDelimiter(":")
,然后調用next()
。 還要記住,除非你更正saveMe()
,否則在開頭會有一個額外的元素是空白的。 同樣在readMe()
,根據Nicklamort的回答,您需要調用new Scanner(new File("Sample.txt"))
來打開實際文件。
新readMe()
示例
public static void readMe() throws IOException
{
Scanner txtFile=(new Scanner(new File("Sample.txt")));
int y = 0;
txtFile.useDelimiter(":");
while(txtFile.hasNext())
{
arr[y]=txtFile.nextInt();
y++;
}
}
新saveMe()
示例
public static void saveMe() throws IOException
{
File data1=new File("Sample.txt"); //a file was created...
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(data1,false))); // don't append, overwrite existing data since we are saving the entire stack each time
for(int x=0;x<arr.length;x++)
{
if(x != 0) { out.write(":"); } //here in this section, I put : in every elements inside the array..
out.write(arr[x]); xy=1;
}
out.flush(); // force writing to disk
out.close();
}
當您嘗試從文件中讀取時:
Scanner txtFile=(new Scanner("Sample.txt"));
我想你試圖解析文字字符串:“Sample.txt”
您希望將實際文件對象傳遞給Scanner構造函數:
Scanner txtFile = new Scanner(File source);
查看文檔
因為在調用saveMe()
之前調用System.exit
在終止程序之前必須調用FileWriter.close
,這樣緩沖區輸出將被刷新到磁盤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.