Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 缓冲读取/保存_Java_Inputstream_Bufferedreader - Fatal编程技术网

Java 缓冲读取/保存

Java 缓冲读取/保存,java,inputstream,bufferedreader,Java,Inputstream,Bufferedreader,我有一个程序,要求你的名字和第二个名字。我使用OutputStream将名字保存在工作区中存储的文件中。我使用BufferedReader读取文件,但我正在尝试获取该文件,因此,如果此人在JOptionPane上单击“是”。yes_NO_对话框,它将使用文件中的名称! 我试着做了一个if声明,上面说if JOptionPane。。。然后是text.setText(savedName),但结果是“Welcome null” 无需在标题中添加[需要帮助],如果您要求我们,您当然需要帮助:)哦,对不起

我有一个程序,要求你的名字和第二个名字。我使用OutputStream将名字保存在工作区中存储的文件中。我使用BufferedReader读取文件,但我正在尝试获取该文件,因此,如果此人在JOptionPane上单击“是”。yes_NO_对话框,它将使用文件中的名称! 我试着做了一个if声明,上面说if JOptionPane。。。然后是text.setText(savedName),但结果是“Welcome null”


无需在标题中添加
[需要帮助]
,如果您要求我们,您当然需要帮助:)哦,对不起:/我的问题可以理解吗,或者我应该更改措辞吗?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class BingoHelper extends JFrame implements WindowListener, ActionListener{
JTextField text = new JTextField();

//JLabel bg = new JLabel("helo");

private JButton b; {
        b = new JButton("Click to enter name");
        }

JPanel pnlButton = new JPanel();

public static String fn;
public static String sn;

public static int n;

File f = new File("test.txt");

public void actionPerformed (ActionEvent e){

    Object[] yesNo = {"Yes",
                      "No",};
    n = JOptionPane.showOptionDialog(null,"Would you like to use previously entered data?","Welcome Back?",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,  null, yesNo,yesNo[1]);

    if (n == JOptionPane.NO_OPTION){    
        for(fn=JOptionPane.showInputDialog("What is your first name?");!fn.matches("[a-zA-Z]+");fn.isEmpty()){
            JOptionPane.showMessageDialog(null, "Alphabet characters only.");
            fn=JOptionPane.showInputDialog("What is your first name?");
        }
        for(sn=JOptionPane.showInputDialog("What is your second name?");!sn.matches("[a-zA-Z]+");sn.isEmpty()){
            JOptionPane.showMessageDialog(null, "Alphabet characters only.");
            sn=JOptionPane.showInputDialog("What is your second name?");
        }

    }
    //JOptionPane.showMessageDialog(null, "Welcome " + fn + " " + sn + ".", "", JOptionPane.INFORMATION_MESSAGE);
    text.setText("Welcome " + fn + " " + sn + ".");
    b.setVisible(false);
    b.setEnabled(false);
    text.setVisible(true);
    text.setBounds(140,0,220,20);
    text.setHorizontalAlignment(JLabel.CENTER);
    text.setEditable(false);
    text.setBackground(Color.YELLOW);
    pnlButton.setBackground(Color.DARK_GRAY);
    writeToFile();
    //bg.setVisible(true);
}

private void writeToFile() {

    String nameToWrite = fn;
    OutputStream outStream = null;
    try {
        outStream = new FileOutputStream(f);
        outStream.write(nameToWrite.getBytes());
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
        String savedName = br.readLine();

        //System.out.println(savedName);
        br.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } finally {
        if (null != outStream) {
            try {
                outStream.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
}