簡體   English   中英

用行尾Java替換txt文件中的連詞

[英]replace conjunctions from a txt file by end of line java

嘿,我需要代碼以行尾替換txt文件中所有出現的連詞。 我有一個保存在txt文件中的連詞列表。我希望將輸入文件和連詞文件都以數組形式存儲。然后使用for循環我想比較兩個數組。但這會帶來很多錯誤。更好的方法來做到這一點? 這是我嘗試做的,但是它在for循環中顯示錯誤

import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
public class Toarray
    {
    private static Object arrays;
    public static void main(String args[]) throws FileNotFoundException
     {
      String filename,path;
      System.out.println("select the input file");
      JFileChooser chooser = new JFileChooser();
      chooser.showOpenDialog(null);               
      File file1 = chooser.getSelectedFile();               
                chooser.showOpenDialog(null);           

                 filename = file1.getName();
                path= file1.getPath();

                Scanner sc;
                sc = new Scanner(new File(filename));
                List<String> lines = new ArrayList<String>();
                while (sc.hasNextLine()) {
                    lines.add(sc.nextLine());
                }

               String[] inp = lines.toArray(new String[0]);

               for (int index=0;index<=20;index++ ){
System.out.println(inp[index]);}

               String remove;
                remove="/Machintosh HD/Users/vaishnavi/Desktop/temp.txt";
                Scanner sc1;
                sc1 = new Scanner(new File(remove));
                List<String> con;
                con = new ArrayList<String>();
                while (sc1.hasNextLine()) {
                     lines.add(sc1.nextLine());
                }

               String[] conj = con.toArray(new String[0]);      
             }
     StriString oldtext;
         for(int i=0;i<=55;i++)

                 {
                  for(int j=0;j<=75;j++)
                         {
                        if(  inp[i].equals(conj[j]))
                        {
                             String newtext = oldtext.replaceAll(inp[i], ".");
                                FileWriter writer = null;   
                                 try {
                                   writer = new FileWriter(path);
                                    } catch (IOException ex) {
                                        Logger.getLogger(Toarray.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                 try {
                                     writer.write(newtext);
                                 } catch (IOException ex) {
                                     Logger.getLogger(Toarray.class.getName()).log(Level.SEVERE, null, ex);
                                 }
            }
        }
    }
}

請幫忙:)

要檢查數組是否包含某些內容,請嘗試以下操作:

if(Arrays.asList(inp).contains("something")){

    //Do something
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM