将修改后的用户输入附加到现有文本文件

时间:2017-03-05 07:22:50

标签: java

 package BIB;
 import java.io.*;
 import java.lang.*;
 public class fileParser{// start class file parser

       static Map<String, String> word ;
       Mapping_File mf = new Mapping_File();
       static Scanner scanner = new Scanner(System.in);

       public static void main(String[] args){

            String Readline = "WCRE";
            String Converted = null;
            word = mf.getWordsMap();

            if(word.containsKey(Readline)){
                   Converted = word.get(Readline);          
             }

            else if(word.isEmpty()){
                 Converted = "the map you are looking in is empty";
              }

         `  else 
            {

                 System.out.println( "Couldnot find the elaborated form.
                                     Please Type the elaborated form you
                                   would like to use for : "
                            + Readline + "'");
                 String query  = scanner.next();
                 Writer output;
                 output = new BufferedWriter(new       
                              FileWriter("HashMap.txt",  true));
                 String Input = Readline + "=" + query;
                 output.append(Input);
                 output.close();
                 Converted = "Your Input was added to the mapping file";

            }// end else
            System.out.println( Converted);


     }// end main method
  }// end class

我在这里所做的是创建了一个文件,该文件应该是一个单词的详细形式&#34; WCRE&#34;来自映射文件Mapping_File,它从文本文件中读取它。但是我的文本文件中没有包含WCRE这个词。

该程序应该向用户询问详细说明的表格,因为它在我所拥有的文本文件中不存在,而且它已成功完成。此外,输入的用户类型将与Words&#34; WCRE =&#34;一起写入文本文件中。在它之前。

但是,当我运行此代码时,会将类似的内容添加到文件末尾:

WCRE=ProceedingsWCRE=ofWCRE=20thWCRE=WorkingWCRE=ConferenceWCRE=onWCRE=ReverseWCRE=EngineeringWCRE=(WCRE)

我想要的是:

WCRE =第20届逆向工程工作会议论文集(WCRE)

其中&#34;第20届逆向工程工作会议论文集(WCRE)&#34;是用户输入。

映射文件构造遵循映射文件在从文本文件读取并创建映射

时正常工作
package BIB;
import java.io.*;
import java.util.*;
public class Mapping_File{

    static Map<String, String> words = new HashMap<>();


    public Mapping_File(){

        this.words = words;
        String filePath = "HashMap.txt";        
        String line;
        BufferedReader reader = null;
        try{

            reader =new BufferedReader(new FileReader(filePath));
            while((line = reader.readLine())!=null){
                String[] parts = line.split("=", 2);
                String key = parts[0];
                String value = parts[1];    
                words.put(key, value);

            }// end while
         }// end try block

         catch(Exception e){
               System.out.println("Couldnot find the requested file.");
           }// end catch block

         }// end constructor

        public Map<String, String> getWordsMap(){

             return words;
        }// end get method getWordsMap


}// end of class Mapping_file

我正在阅读的文本文件名为HashMap.txt: ICSE =国际软件工程会议论文集(ICSE)

IWSC =国际软件克隆研讨会(IWSC)会议记录

ASE =自动软件工程国际会议论文集(ASE)

ACSAC =年度计算机安全应用大会(ACSAC)会议记录

ICSE(NIER Track)=国际软件工程会议(ICSE)会议录,NIER Track

1 个答案:

答案 0 :(得分:0)

当我在主方法中声明Scanner时,程序在我的计算机上运行。扫描仪也用scanner.nextLine()替换了scanner.next(),因为我本来应该读一整行。