Java练习九

第十五章

1.编写程序,实现读取文件时出现一个表示读取进度的进度条。

public class exercise  {

	public static void main(String[] args) {
		byte b[] = new byte[2];
		File file=new File("exercise.txt");
		try {
			FileWriter out=new FileWriter(file);
			String tp=new String("天行健君子以自强不息");
			out.write(tp);
			out.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		try {
			FileInputStream in=new FileInputStream(file);
			ProgressMonitorInputStream inn=new ProgressMonitorInputStream(null,"读取中",in);
			//String tp=new String("天行健君子以自强不息");
			//char tp1[]=new char[1024];
			//int tp2=in.read(tp1);
			//in.close();
			//System.out.println("信息:"+new String(tp1,0,tp2));
			while(inn.read(b)!=-1) {
				String s=new String(b);
				   System.out.print(s);
				   Thread.sleep(1000);				
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		}
}

2.使用字符输入输出流读取文件

public class exercise  {

	public static void main(String[] args) {
		byte b[] = new byte[2];
		File file=new File("exercise.txt");
		try {
			FileWriter out=new FileWriter(file);
			String tp=new String("天行健君子以自强不息");
			out.write(tp);
			out.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		try {
			FileReader in=new FileReader(file);
			//ProgressMonitorInputStream inn=new ProgressMonitorInputStream(null,"读取中",in);
			char tp1[]=new char[1024];
			int tp2=in.read(tp1);
			in.close();
			System.out.println("信息:"+new String(tp1,0,tp2));
		}catch(Exception e) {
			e.printStackTrace();
		}
		}
}

3.编写程序,实现当用户输入姓名和密码时,将每一个姓名和密码加在文件中,如果用户输入done,结束程序。

public class exercise extends JFrame  {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static JTextField jt1=new JTextField();
	static JPasswordField jt2=new JPasswordField();
	static JPanel jp1=new JPanel();
	static JPanel jp2=new JPanel();
    static JLabel jl1=new JLabel("用户名");
    static JLabel jl2=new JLabel("密码");
     File file=new File("exercise1.txt");
     File file1=new File("exercise2.txt");
    public exercise() {

    	jt1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String tp=new String(jt1.getText());
				try {
					FileWriter fw=new FileWriter(file);
					fw.write(tp);
					fw.close();
					if(tp.trim().equals("done")) {
						fw.close();
					}
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
    		
    	});
    	jt2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String tp1=new String(jt2.getPassword());
				try {
					FileWriter fw=new FileWriter(file1);
					fw.write(tp1);
					fw.close();
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
    		
    	});
    }
	public static void main(String[] args) {
		JFrame jf=new JFrame("111");
		jf.setVisible(true);
		jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
		jf.setSize(500, 300);
		Container co=jf.getContentPane();
		co.setLayout(new GridLayout(1,2,10,10));
    	jp1=new JPanel(new GridLayout(2,1,5,5));
    	jp2=new JPanel(new GridLayout(2,1,5,5));
    	jp1.add(jl1);
    	jp1.add(jt1);
    	jp2.add(jl2);
    	jp2.add(jt2);
		co.add(jp1);
		co.add(jp2);
		new exercise();
		//File file=new File("exercise1.txt");
/*		try {
			FileWriter fw=new FileWriter(file);
			String tp=new String(jt1.getText());
			while(true) {
			fw.write(tp);
			if(tp.trim().equals("done")) {
				break;
			}
			}
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		}
}

你可能感兴趣的:(Java练习九)