代码之家  ›  专栏  ›  技术社区  ›  Crystal

在Java中编辑文件时跟踪标点、间距

  •  0
  • Crystal  · 技术社区  · 15 年前

    /** Removes consecutive duplicate words from text files.  
    It accepts only one argument, that argument being a text file 
    or a directory.  It finds all text files in the directory and 
    its subdirectories and moves duplicate words from those files 
    as well.  It replaces the original file. */
    
    import java.io.*;
    import java.util.*;
    
    public class RemoveDuplicates {
    
        public static void main(String[] args) {
    
    
            if (args.length != 1) {
                System.out.println("Program accepts one command-line argument.  Exiting!");
                System.exit(1);
            }
            File f = new File(args[0]);
            if (!f.exists()) {
                System.out.println("Does not exist!");
            }
    
            else if (f.isDirectory()) {
                System.out.println("is directory");
    
            }
            else if (f.isFile()) {
                System.out.println("is file");
                String fileName = f.toString();
                RemoveDuplicates dup = new RemoveDuplicates(f);
                dup.showTextFile();
                List<String> noDuplicates = dup.doDeleteDuplicates();
                showTextFile(noDuplicates);
                //writeOutputFile(fileName, noDuplicates);
            }
            else {
                System.out.println("Shouldn't happen");
            }   
        }
    
        /** Reads in each line of the passed in .txt file into the lineOfWords array. */
        public RemoveDuplicates(File fin) {
            lineOfWords = new ArrayList<String>();
            try {
                BufferedReader in = new BufferedReader(new FileReader(fin));
                for (String s = null; (s = in.readLine()) != null; ) {
                    lineOfWords.add(s);
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void showTextFile() {
            for (String s : lineOfWords) {
                System.out.println(s);
            }
        }
    
        public static void showTextFile(List<String> list) {
            for (String s : list) {
                System.out.print(s);
            }
        }
    
        public List<String> doDeleteDuplicates() {
            List<String> noDup = new ArrayList<String>(); // List to be returned without duplicates
            // go through each line and split each word into end string array
            for (String s : lineOfWords) {
                String endString[] = s.split("[\\s+\\p{Punct}]");
                // add each word to the arraylist
                for (String word : endString) {
                    noDup.add(word);
                }
            }
            for (int i = 0; i < noDup.size() - 1; i++) {
                if (noDup.get(i).toUpperCase().equals(noDup.get(i + 1).toUpperCase())) {
                    System.out.println("Removing: " + noDup.get(i+1));
                    noDup.remove(i + 1);
                    i--;
                }
            }
            return noDup;
        }
    
        public static void writeOutputFile(String fileName, List<String> newData) {
            try {
                PrintWriter outputFile = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
                for (String str : newData) {
                    outputFile.print(str + " ");
                }
                outputFile.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private List<String> lineOfWords;
    }
    

    例如.txt:

    Hello hello this is a test test in order
    order to see if it deletes duplicates Duplicates words.
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   limc    15 年前

        Pattern p = Pattern.compile("(\\w+) \\1");
        String line = "Hello hello this is a test test in order\norder to see if it deletes duplicates Duplicates words.";
    
        Matcher m = p.matcher(line.toUpperCase());
    
        StringBuilder sb = new StringBuilder(1000);
        int idx = 0;
    
        while (m.find()) {
            sb.append(line.substring(idx, m.end(1)));
            idx = m.end();
        }
        sb.append(line.substring(idx));
    
        System.out.println(sb.toString());
    

    这是你的答案output:-

    Hello this a test in order
    order to see if it deletes duplicates words.