简体   繁体   English

Java修改来自Jtextfiields的文本文件内容作为输入

[英]Java Modify text file content from Jtextfiields as input

Currently I get stuck in the modification parts. 目前,我陷入了修改部分。 I having a "booklist.txt": 我有一个“ booklist.txt”:

aaa-bbb-ccc-ddd AAA BBB-CCC,DDD

qqq-www-eee-rrr QQQ-WWW-EEE-RRR

    String q=oldID.getText();
    String w=newID.getText();
    String a=Author.getText();
    String r=title.getText();
    String t=quantity.getText();
            try{
    File f= new File("BookList.txt");
    FileReader fr=new FileReader(f);
    BufferedReader br=new BufferedReader(fr);
    FileWriter fw=new FileWriter(f);
    String line=br.readLine();
    String oldtext="";
    String newtext="";
    String[] tem=new String[4];

    while(line!=null)
    {
    oldtext +=line+System.getProperty("line Seperator");
    tem=line.split("-");
    if(tem[0].equals(q)){
            newtext = oldtext.replaceAll(line,w+"-"+e+"-"+r+"-"+t);
            }     
    }
    fw.write(newtext);        
    fw.close();
    br.close();
            }catch(IOException Ex){Ex.printStackTrace();}

What i want is get input from the Jtexfields and replace the strings in the text file. 我想要的是从Jtexfields获取输入并替换文本文件中的字符串。

Example: if the id that I type is qqq(tem[0]) then the while loop will read all the line in text file.If the qqq that i type is found, modify the other strings which is www, eee, and rrr. 例如:如果我输入的ID是qqq(tem [0]),则while循环将读取文本文件中的所有行。如果找到我输入的qqq,则修改其他字符串,如www,eee和rrr 。 The input to replace these strings are from the Jtextfields (String q,w,e,r,t) 替换这些字符串的输入来自Jtextfields(字符串q,w,e,r,t)

in order to write in a file use FileWriter ! 为了写入文件,请使用FileWriter

here is what should you do 这是你应该怎么做

FileWriter writer=new FileWriter(path);
writer.writeln(newText);
writer.flush();
writer.close();

this will create a file even if the file path doesn't exist. 即使文件路径不存在,这也会创建一个文件。 Be aware this will remove all content in the file and the content will be replaced. 请注意,这将删除文件中的所有内容,并且内容将被替换。 However if you don't want to replace the content add a parameter to FileWriter object to be like this 但是,如果您不想替换内容,则向FileWriter对象添加一个参数,就像这样

new FileWriter(path,true);

this will append the current content with the new content you want to add 这会将当前内容添加到您要添加的新内容中

UPDATE UPDATE

I'm not sure if that what is you need First thing you have to read the whole file the second step you need to put the whole lines into ArrayList due arrayList is dynamic array 我不确定是否需要什么第一件事是必须读取整个文件,第二步是将整行放入ArrayList中,因为arrayList是动态数组

finally do your compare 终于做你的比较

Scanner scanner=new Scanner(new File(path));
ArrayList<String>list=new ArrayList<String>();
while(scanner.hasNextLine()){list.add(scanner.nextLine())}

finally for example you want to replace something let us assume QQQ as you mention 最后例如,您要替换某些内容,让我们假设您提到的QQQ

if(list.contains("QQQ"))
{
list.replace("QQ",myNewText)>0
}else list.add(myNewText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM