如何使用line.contains&获取我的文本文件的所有单词?排列

时间:2017-12-17 02:16:03

标签: java

我的问题是,当我使用这段代码时,我只得到包含我所要求的单词的最后一行文字,我怎样才能获得包含我要求的特定单词的所有行,然后将它们正确地存储到jTextArea中?

    try  {
       BufferedReader br = new BufferedReader(new FileReader("file.txt"));
       String line;

       while((line = br.readLine()) !=null) {
           if(line.contains("Win")){
               jTextArea1.setText(line);
           }
       }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
} else {
    jTextArea1.setText("sup");
} 

2 个答案:

答案 0 :(得分:0)

一种方法是将String保存到List

enterFullScreenMode(withOptions:)

最后几行将使用新行标记连接ArrayList中的每个单词以加入单词。

答案 1 :(得分:0)

问题是你正在使用方法setText()。每次调用setText()时,都会替换您的文本区域。您要做的是追加,因此您需要致电:#%% from tkinter import * def adminLogin(): global AnameEL global ApwordEL # More globals :D global ArootA AnameL = Label(f2, text='Username: ') # More labels ApwordL = Label(f2, text='Password: ') # ^ AnameL.grid(row=1, sticky=W) ApwordL.grid(row=2, sticky=W) AnameEL = Entry(f2) # The entry input ApwordEL = Entry(f2, show='*') AnameEL.grid(row=1, column=1) ApwordEL.grid(row=2, column=1) AloginB = Button(f2, text='Login', command=CheckAdmin) # This makes the login button, which will go to the CheckLogin def. AloginB.grid(columnspan=2, sticky=W) def CheckAdmin(): if AnameEL.get() == "test" and ApwordEL.get() == "123" : # Checks to see if you entered the correct data. f2.destroy() #r = Tk() # Opens new window loginL = Label(f3, text = 'Success!!!') loginC = Button(f3, text='Add new login', command=Signup) loginL.grid(row = 2, column=0, sticky=W) loginC.grid(columnspan=2, sticky=E) #r.mainloop() else: f2.destroy() #r = Tk() # Opens new window loginL2 = Label(f3, text = 'Error!!') ribl = Label(f3, text='\n[!] Invalid Login') loginL2.grid(row = 2, column=0, sticky=W) ribl.grid(row = 3, column=0, sticky=W) #.mainloop() def Signup(): # This is the signup definition, global pwordE # These globals just make the variables global to the entire script, meaning any definition can use them global nameE global roots f3.destroy() #r = Tk() # Opens new window loginL3 = Label(f4, text = 'Signup!!!') # This renames the title of said window to 'signup' loginL3.grid(row = 0, column=50, sticky=W) intruction = Label(f4, text='Please Enter new Credidentials\n') # This puts a label, so just a piece of text saying 'please enter blah' intruction.grid(row=2, column=0, sticky=E) # This just puts it in the window, on row 0, col 0. If you want to learn more look up a tkinter tutorial :) nameL = Label(f4, text='New Username: ') # This just does the same as above, instead with the text new username. pwordL = Label(f4, text='New Password: ') # ^^ nameL.grid(row=3, column=0, sticky=W) # Same thing as the instruction var just on different rows. :) Tkinter is like that. pwordL.grid(row=4, column=0, sticky=W) # ^^ nameE = Entry(f4) # This now puts a text box waiting for input. pwordE = Entry(f4, show='*') # Same as above, yet 'show="*"' What this does is replace the text with *, like a password box :D nameE.grid(row=3, column=1) # You know what this does now :D pwordE.grid(row=4, column=1) # ^^ signupButton = Button(f4, text='Signup', command=FSSignup) # This creates the button with the text 'signup', when you click it, the command 'fssignup' will run. which is the def signupButton.grid(columnspan=2, sticky=W) roots.mainloop() # This just makes the window keep open, we will destroy it soon ArootA = Tk() # This now makes a new window. ArootA.geometry('1280x720') ArootA.title('Admin login') # This makes the window title 'login' f1 = Frame(width=200, height=200, background="#D3D3D3") f2 = Frame(ArootA, width=400, height=200) f2.pack(fill='both', expand=True, padx=0, pady=0, side = TOP) #f2.place(in_=f1, anchor="c", relx=.5, rely=.5) f3 = Frame(ArootA, width=550, height=450) f3.pack(fill='both', expand=True, padx=0, pady=0, side = TOP) f4 = Frame(ArootA, width=550, height=450) f4.pack(fill='both', expand=True, padx=0, pady=0, side = TOP) adminLogin() ArootA.mainloop()