简体   繁体   中英

replace conjunctions from a txt file by end of line java

hey i need the code to replace all the occurrence of conjunctions in a txt file by end of line. I have a list of conjunctions saved in a txt file.i want both the input file taken and the conjunctions file to be stored in an array form.Then using for loop i wanted compare both the arrays .but this gives many errors.is there a better way to do the same? this is what i tried doing, but it shows error in the for loop

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);
                                 }
            }
        }
    }
}

please do help :)

To check if array contains something or not try this:

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

    //Do something
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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