Topic: 为什么存储记录的文件为空呢? |
Print this page |
1.为什么存储记录的文件为空呢? | Copy to clipboard |
Posted by: zhjdenis Posted on: 2007-09-28 20:25 程序实现的***能是随机抽出一个数让你来猜,如果猜的次数最少将提示你输入姓名,并记入“record.txt”文件中,可是程序一切运行正常,记录姓名后我打开“record.txt”文件发现是空不知道为什么?还有就是我想问一下默认情况下如果不输入完整的路径名则“record.txt”文件应该存放在什么地方才能被程序读去其中的数据?谢谢!!我把JDK安装在C:\Program Files\JavA文件下,我用的ECLIPSE的输出工程目录为C:\Documents and Settings\Administrator\workspace package 游戏; import java.awt.GraphicsConfiguration; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Myguest extends JFrame implements ActionListener { JTextField tf=new JTextField(); JButton b1=new JButton("start"); JLabel j1=new JLabel(); int count; int n; int oldcount; boolean isend=false; public Myguest() throws HeadlessException { JPanel p=new JPanel(); b1.setActionCommand("start"); b1.addActionListener(this); tf.addActionListener(this); tf.setEnabled(false); p.add(b1); this.getContentPane().add(j1); this.getContentPane().add(p,"South"); this.getContentPane().add(tf, "North"); this.setSize(300, 300); this.setLocation(300, 100); this.setVisible(true); // TODO 自动生成构造函数存根 } public int getnumber() { int m=(int)(Math.random()*100+1); return m; } public void actionPerformed(ActionEvent e) { // TODO 自动生成方法存根 String s=e.getActionCommand(); if(s.equals("start")) { n=getnumber(); System.out.println(n); count=0; tf.requestFocus(); j1.setText("enter the number(1-100)"); b1.setEnabled(false); tf.setEnabled(true); oldcount=read(); } else { if(isend!=true) { count++; int guestnumber; String sn=tf.getText(); try { guestnumber=Integer.parseInt(sn); } catch(NumberFormatException e1) { j1.setText("please enter the number"); return; } if(guestnumber<n) { j1.setText("the number your type is small"); return; } else { if(guestnumber>n) { j1.setText("the number your type is big"); return; } else { j1.setText("you win"); j1.setText("you guest "+count); tf.setText(""); isend=true; b1.setEnabled(true); if(count<oldcount) { j1.setText("你打破记录,请输入你的姓名"); } return; } } } else { String name=tf.getText(); write(count,name); j1.setText("your name has been record"); tf.setText(""); b1.setEnabled(true); } } } int read() { int count=100; File f1=new File("record.txt"); try { FileReader fin=new FileReader(f1); BufferedReader bufin=new BufferedReader(fin); String s=bufin.readLine(); count=Integer.parseInt(s); bufin.close(); fin.close(); } catch(java.io.FileNotFoundException e){} catch(IOException e){} return count; } void write(int result,String name) { File f1=new File("record.txt"); try { FileWriter fout=new FileWriter(f1); PrintWriter fprint=new PrintWriter(fout); fprint.println(result); fprint.println(name); String dir=f1.getAbsolutePath(); System.out.println(dir); } catch(java.io.FileNotFoundException e){} catch(IOException e){} } /** * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 new Myguest(); } } |
2.Re:为什么存储记录的文件为空呢? [Re: zhjdenis] | Copy to clipboard |
Posted by: xuxiaolei Posted on: 2007-10-09 11:53 void write(int result,String name)方法中,fout和fprint两个流没有被关闭,所以文件是空的 如果不输入完整的路径名则“record.txt”文件应该存放在什么地方才能被程序读去其中的数据?通过下面的方法打印出record.txt的绝对路径 File file = new File("record.txt"); System.out.println(file.getAbsolutePath()); |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |