| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2177 人关注过本帖
标题:求助大神,,我这样修改TXT文档中的一行就会把整个文档清除数据,该怎么正确 ...
只看楼主 加入收藏
张贤明
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2018-3-3
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:5 
求助大神,,我这样修改TXT文档中的一行就会把整个文档清除数据,该怎么正确写那块的代码呢,新手自学,希望大神点拨下
import java.awt.*;
import java.awt.event.*;
import *;
import *;
import java.util.*;
import javax.swing.*;
public class Image extends JFrame{

    public void Myframe(){
        final JFrame kj=new JFrame();
        //setLayout(null);
        kj.setLayout(null);
        //kj.setSize(300, 200);
        kj.setBounds(0,0,300,200);
        //setBounds(0,0,300,200);
        kj.setVisible(true);
        final Container con=kj.getContentPane();
        
        final File file=new File("G:/password.txt");              //创建配置文件
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        
      
        
        final JLabel zh=new JLabel("账号");
        zh.setBounds(0, 20, 100, 21);
        con.add(zh);
        
        final JTextField zhangh=new JTextField();
        zhangh.setBounds(30,20,100,20);   
        zhangh.setBackground(Color.yellow);
        con.add(zhangh);
        zhangh.setVisible(true);
        
        final JButton zhuche=new JButton("注册账号");      
        zhuche.setBounds(150, 20, 100,21);
        con.add(zhuche);
        
        zhuche.addActionListener(new  ActionListener(){                     //注册按钮事件
            public void actionPerformed( ActionEvent e){
                final pass arr[]=new pass[5];
                JFrame ZC=new JFrame("注册框");
                ZC.setLayout(null);
                ZC.setBounds(0,0,300,300);
                final Container con1=ZC.getContentPane();
               
                final JLabel zczh=new JLabel("注册账号");
                zczh.setBounds(0, 20, 100, 21);
                con1.add(zczh);
               
                final JTextField zczhangh=new JTextField();
                zczhangh.setBounds(60,20,100,20);   
                zczhangh.setBackground(Color.yellow);
                con1.add(zczhangh);
                zczhangh.setVisible(true);
               
                final JLabel zcjl=new JLabel("密码");
                zcjl.setBounds(0, 40, 100, 21);
                con1.add(zcjl);
               
                final JTextField zcmima=new JTextField("",20);
                zcmima.setBounds(60,40,100,20);   
                zcmima.setBackground(Color.yellow);
                con1.add(zcmima);
                zcmima.setVisible(true);
               
                final JButton zcau=new JButton("注册");
                zcau.setBounds(40, 70, 60,20);
                con1.add(zcau);
               
                ZC.setVisible(true);
               
                zcau.addActionListener(new ActionListener(){                     //注册框 注册按钮事件
                    public void actionPerformed(ActionEvent e){
                        String ip=zczhangh.getText()+"\t"+"||"+"\t"+zcmima.getText();
                        ip = ip+"\r\n";
                        try{
                            //FileOutputStream out = new FileOutputStream(file,true);  
                           // out.write(ip.getBytes());  
                            FileWriter out=new FileWriter(file,true);      //true 很关键
                            out.write(ip);  
                            out.close();
                            }catch(Exception e1){
                                e1.printStackTrace();
                                }
                        final JLabel zcview=new JLabel("注册成功");                                   
                        zcview.setBounds(150, 70, 100,21);
                       zcview.setForeground(Color.red);
                        con1.add(zcview);                       
                    }
                    });                    
            }
            
          });
        
        final JLabel jl=new JLabel("密码");
        jl.setBounds(0, 40, 100, 21);
        con.add(jl);
        
        final JTextField mima=new JTextField();
        mima.setBounds(30,40,100,20);   
        mima.setBackground(Color.yellow);
        con.add(mima);
        mima.setVisible(true);
        
        final JButton shouch=new JButton("修改密码");      
        shouch.setBounds(150, 40, 100,21);
        con.add(shouch);
        
        
        shouch.addActionListener(new  ActionListener(){                     //找回密码按钮事件
            public void actionPerformed( ActionEvent e){
                JFrame ZH=new JFrame("找回密码");
                ZH.setLayout(null);
                ZH.setBounds(0,0,300,300);
                ZH.setVisible(true);
                Container con2=ZH.getContentPane();
               
                final JLabel xgzh=new JLabel("账号");
                xgzh.setBounds(0, 20, 100, 21);
                con2.add(xgzh);
               
                final JTextField xgzhangh=new JTextField();
                xgzhangh.setBounds(60,20,100,20);   
                xgzhangh.setBackground(Color.yellow);
                con2.add(xgzhangh);
                xgzhangh.setVisible(true);
               
                final JLabel xgjl=new JLabel("原始密码");
                xgjl.setBounds(0, 40, 100, 21);
                con2.add(xgjl);
               
                final JTextField xgmima=new JTextField("",20);
                xgmima.setBounds(60,40,100,20);   
                xgmima.setBackground(Color.yellow);
                con2.add(xgmima);
                xgmima.setVisible(true);
               
                final JLabel xg=new JLabel("修改密码");
                xg.setBounds(0, 60, 100, 21);
                con2.add(xg);
               
                final JTextField xgtext=new JTextField("",20);
                xgtext.setBounds(60,60,100,20);   
                xgtext.setBackground(Color.yellow);
                con2.add(xgtext);
                xgtext.setVisible(true);
               
               
               
                final JButton xgau=new JButton("确认修改");
                xgau.setBounds(40, 90, 90,20);
                con2.add(xgau);
               
                final JLabel xgview=new JLabel();                                    //创建错误提示框
                xgview.setBounds(150, 90, 100,21);
                xgview.setForeground(Color.red);
                con2.add(xgview);
               
                xgau.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                    
                    try {
                        final FileReader in=new FileReader(file);
                         BufferedReader br=new BufferedReader(in);            //每行的对象
                        String line=null;
                         while ((line=br.readLine())!=null){
                             if((xgzhangh.getText()+"\t"+"||"+"\t"+xgmima.getText()).equals(line)){
                                 FileWriter out=new FileWriter(file);
                                 BufferedWriter jf=new BufferedWriter(out);
                                 line=xgzhangh.getText()+"\t"+"||"+"\t"+xgtext.getText();
                                 jf.write(line);                                 
                                 jf.newLine();
                                 out.flush();
                                 out.close();
                                 //line=xgzhangh.getText()+"\t"+"||"+"\t"+xgtext.getText();
                                 xgview.setText("修改密码成功");
                                 return;
                             }
                             else{    xgview.setText("账号或密码错误修改失败");}
                         }
                         in.close();
                         br.close();
                         }
                    catch (Exception e1) {                            //注意点
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    }
                    });
            }
            
          });
        
        
        final JButton au=new JButton("登入");
        au.setBounds(40, 70, 60,20);
        con.add(au);
        //au.setVisible(true);
        
        final JLabel view=new JLabel();                                    //创建错误提示框
        view.setBounds(150, 70, 100,21);
        view.setForeground(Color.red);
        con.add(view);
        
      
        au.addActionListener(new ActionListener(){                     //确定按钮事件
            public void actionPerformed(ActionEvent e){
               
                try {
                    final FileReader in=new FileReader(file);
                     BufferedReader br=new BufferedReader(in);            //每行的对象
                    String line=null;
                     while ((line=br.readLine())!=null) {  
                        //System.out.print(line);
                         if((zhangh.getText()+"\t"+"||"+"\t"+mima.getText()).equals(line)){
                                //view.setText("密码正确");
                                kj.setVisible(false);
                                JFrame qj=new JFrame("QQ2017");
                                qj.setLayout(null);
                                qj.setBounds(0,0,200,500);
                                Container con=qj.getContentPane();
                                qj.setVisible(true);
                                return;
                                }
                                
                         else{    view.setText("账号或密码错误");}
                                    
                                 
                        }
                     
                        br.close();  
                        in.close();
                }
                catch (Exception e1) {                            //注意点
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
               
               }
        });
        }
   
    public static void main(String args[]){
        Image main=new Image();
        main.Myframe();
   
    }
}
搜索更多相关主题的帖子: new out con add line 
2018-03-05 23:44
张贤明
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2018-3-3
收藏
得分:0 
回复 楼主 张贤明
主要是“修改密码”这个按钮的事件,,先谢谢各位啦
2018-03-05 23:45
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:10 
回复 2楼 张贤明
final pass arr[] = new pass[5];
pass是什么类型?

假如人生没有梦想,和咸鱼有什么区别!
2018-03-06 08:48
张贤明
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2018-3-3
收藏
得分:0 
回复 3楼 疯狂的小a
这个我没用上直接删除就是了,
2018-03-06 09:29
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:0 
程序代码:
package com.xiaoa.demo;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import import import import import import import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Image extends JFrame {

    public void Myframe() {
        final JFrame kj = new JFrame();
        // setLayout(null);
        kj.setLayout(null);
        // kj.setSize(300, 200);
        kj.setBounds(0, 0, 300, 200);
        // setBounds(0,0,300,200);
        kj.setVisible(true);
        final Container con = kj.getContentPane();

        final File file = new File("D:/password.txt"); // 创建配置文件
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        final JLabel zh = new JLabel("账号");
        zh.setBounds(0, 20, 100, 21);
        con.add(zh);

        final JTextField zhangh = new JTextField();
        zhangh.setBounds(30, 20, 100, 20);
        zhangh.setBackground(Color.yellow);
        con.add(zhangh);
        zhangh.setVisible(true);

        final JButton zhuche = new JButton("注册账号");
        zhuche.setBounds(150, 20, 100, 21);
        con.add(zhuche);

        zhuche.addActionListener(new ActionListener() { // 注册按钮事件
            public void actionPerformed(ActionEvent e) {
                JFrame ZC = new JFrame("注册框");
                ZC.setLayout(null);
                ZC.setBounds(0, 0, 300, 300);
                final Container con1 = ZC.getContentPane();

                final JLabel zczh = new JLabel("注册账号");
                zczh.setBounds(0, 20, 100, 21);
                con1.add(zczh);

                final JTextField zczhangh = new JTextField();
                zczhangh.setBounds(60, 20, 100, 20);
                zczhangh.setBackground(Color.yellow);
                con1.add(zczhangh);
                zczhangh.setVisible(true);

                final JLabel zcjl = new JLabel("密码");
                zcjl.setBounds(0, 40, 100, 21);
                con1.add(zcjl);

                final JTextField zcmima = new JTextField("", 20);
                zcmima.setBounds(60, 40, 100, 20);
                zcmima.setBackground(Color.yellow);
                con1.add(zcmima);
                zcmima.setVisible(true);

                final JButton zcau = new JButton("注册");
                zcau.setBounds(40, 70, 60, 20);
                con1.add(zcau);

                ZC.setVisible(true);

                zcau.addActionListener(new ActionListener() { // 注册框 注册按钮事件
                    public void actionPerformed(ActionEvent e) {
                        String ip = zczhangh.getText() + "\t" + "||" + "\t" + zcmima.getText();
                        ip = ip + "\r\n";
                        try {
                            // FileOutputStream out = new FileOutputStream(file,true);
                            // out.write(ip.getBytes());
                            FileWriter out = new FileWriter(file, true); // true 很关键
                            out.write(ip);
                            out.close();
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        final JLabel zcview = new JLabel("注册成功");
                        zcview.setBounds(150, 70, 100, 21);
                        zcview.setForeground(Color.red);
                        con1.add(zcview);
                    }
                });
            }

        });

        final JLabel jl = new JLabel("密码");
        jl.setBounds(0, 40, 100, 21);
        con.add(jl);

        final JTextField mima = new JTextField();
        mima.setBounds(30, 40, 100, 20);
        mima.setBackground(Color.yellow);
        con.add(mima);
        mima.setVisible(true);

        final JButton shouch = new JButton("修改密码");
        shouch.setBounds(150, 40, 100, 21);
        con.add(shouch);

        shouch.addActionListener(new ActionListener() { // 找回密码按钮事件
            public void actionPerformed(ActionEvent e) {
                JFrame ZH = new JFrame("找回密码");
                ZH.setLayout(null);
                ZH.setBounds(0, 0, 300, 300);
                ZH.setVisible(true);
                Container con2 = ZH.getContentPane();

                final JLabel xgzh = new JLabel("账号");
                xgzh.setBounds(0, 20, 100, 21);
                con2.add(xgzh);

                final JTextField xgzhangh = new JTextField();
                xgzhangh.setBounds(60, 20, 100, 20);
                xgzhangh.setBackground(Color.yellow);
                con2.add(xgzhangh);
                xgzhangh.setVisible(true);

                final JLabel xgjl = new JLabel("原始密码");
                xgjl.setBounds(0, 40, 100, 21);
                con2.add(xgjl);

                final JTextField xgmima = new JTextField("", 20);
                xgmima.setBounds(60, 40, 100, 20);
                xgmima.setBackground(Color.yellow);
                con2.add(xgmima);
                xgmima.setVisible(true);

                final JLabel xg = new JLabel("修改密码");
                xg.setBounds(0, 60, 100, 21);
                con2.add(xg);

                final JTextField xgtext = new JTextField("", 20);
                xgtext.setBounds(60, 60, 100, 20);
                xgtext.setBackground(Color.yellow);
                con2.add(xgtext);
                xgtext.setVisible(true);

                final JButton xgau = new JButton("确认修改");
                xgau.setBounds(40, 90, 90, 20);
                con2.add(xgau);

                final JLabel xgview = new JLabel(); // 创建错误提示框
                xgview.setBounds(150, 90, 100, 21);
                xgview.setForeground(Color.red);
                con2.add(xgview);

                xgau.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {

                        try {
                            final FileReader in = new FileReader(file);
                            BufferedReader br = new BufferedReader(in); // 每行的对象
                            String line = null;
                            
                            
                            //创建一个集合,存数据
                            ArrayList<String> list = new ArrayList<String>();
                            while((line=br.readLine())!=null) {
                                list.add(line);
                            }
                            
                            //遍历集合,找用户名,密码对应的行
                            int index=0;
                            for(int i=0;i<list.size();i++) {
                                if ((xgzhangh.getText() + "\t" + "||" + "\t" + xgmima.getText()).equals(list.get(i))) {
                                    index=i;
                                    line = xgzhangh.getText() + "\t" + "||" + "\t" + xgtext.getText();
                                    list.set(i, line);
                                }
                                if(i==list.size()) {
                                    xgview.setText("账号或密码错误修改失败");
                                    
                                }
                            }
                            //将list集合写到文件
                            System.out.println(list);
                            FileWriter out = new FileWriter(file);
                            BufferedWriter jf = new BufferedWriter(out);
                            for(int i=0;i<list.size();i++) {
                                
                                jf.write(list.get(i));
                                if(i==index) {
                                    xgview.setText("修改密码成功");
                                }
                                jf.newLine();
                                jf.flush();
                                
                            }
                            
                            jf.close();
                            in.close();
                            br.close();
                        } catch (Exception e1) { // 注意点
                            e1.printStackTrace();
                        }
                    }
                });
            }

        });

        final JButton au = new JButton("登入");
        au.setBounds(40, 70, 60, 20);
        con.add(au);
        // au.setVisible(true);

        final JLabel view = new JLabel(); // 创建错误提示框
        view.setBounds(150, 70, 100, 21);
        view.setForeground(Color.red);
        con.add(view);

        au.addActionListener(new ActionListener() { // 确定按钮事件
            public void actionPerformed(ActionEvent e) {

                try {
                    final FileReader in = new FileReader(file);
                    BufferedReader br = new BufferedReader(in); // 每行的对象
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        System.out.print(line);
                        if ((zhangh.getText() + "\t" + "||" + "\t" + mima.getText()).equals(line)) {
                            // view.setText("密码正确");
                            kj.setVisible(false);
                            JFrame qj = new JFrame("QQ2017");
                            qj.setLayout(null);
                            qj.setBounds(0, 0, 200, 500);
                            Container con = qj.getContentPane();
                            qj.setVisible(true);
                            return;
                        }else {
                            view.setText("账号或密码错误");
                        }
                    }
                    br.close();
                    in.close();
                } catch (Exception e1) { // 注意点
                    e1.printStackTrace();
                }

            }
        });
    }

    public static void main(String args[]) {
        Image main = new Image();
        main.Myframe();

    }
}

假如人生没有梦想,和咸鱼有什么区别!
2018-03-06 11:39
张贤明
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2018-3-3
收藏
得分:0 
回复 5楼 疯狂的小a
谢谢大神,,基本上也明白其中的意思,感谢
2018-03-06 20:17
快速回复:求助大神,,我这样修改TXT文档中的一行就会把整个文档清除数据,该怎 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058074 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved