软件体系结构之kwic本地实现

java代码:

import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.StringTokenizer;
public class bbb {
}
class Pipe {
    private Scanner pipeReader;
    private PrintWriter pipeWriter;
    Pipe(){
        PipedWriter pipedWriter = new PipedWriter();
        PipedReader pipedReader = new PipedReader();
        try {
            pipedWriter.connect(pipedReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        pipeReader = new Scanner(pipedReader);
        pipeWriter = new PrintWriter(pipedWriter);
    }
    public String readerLine(){
        return pipeReader.nextLine();
    }
    public boolean hashNextLine(){
        return pipeReader.hasNext();
    }
    public void writerLine(String strLine){
        pipeWriter.println(strLine);
    }
    public void closeReader(){
        pipeReader.close();
    }
    public void closeWriter(){
        pipeWriter.close();
    }
}
abstract class Filter {
    protected Pipe input;
    protected Pipe output;

    public Filter(Pipe input, Pipe output) {
        this.input = input;
        this.output = output;
    }
    protected abstract void transform() throws IOException;
}
class Input extends Filter {
    private File file;
    public Input(File file,Pipe output) {
        super(null, output);
        this.file = file;
    }

    @Override
    protected void transform() throws IOException {
        BufferedReader inputFile = null;
        try {
            inputFile = new BufferedReader(new FileReader(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String line;
        try {
            while ((line = inputFile.readLine()) != null) {
                output.writerLine(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        output.closeWriter();
    }
}
class Shift extends Filter {

    public Shift(Pipe input, Pipe output) {
        super(input, output);
    }

    @Override
    protected void transform() throws IOException {
        //获取每个单词,存入tokens
        while (input.hashNextLine()) {
            StringTokenizer token = new StringTokenizer(input.readerLine());
            ArrayList<String> tokens = new ArrayList<String>();
            int i = 0;
            //循环添加单词
            int count = token.countTokens();
            while (i < count) {
                tokens.add(token.nextToken());
                i++;
            }

            //display(tokens);
            //切割各个单词,不断改变起始值和利用loop实现位移。
            for (i = 0; i < count; i++) {
                StringBuffer lineBuffer = new StringBuffer();
                int index = i;
                for (int f = 0; f < count; f++) {
                    //从头继续位移
                    if (index >= count)
                        index = 0;
                    //存入StringBuffer
                    lineBuffer.append(tokens.get(index));
                    lineBuffer.append(" ");
                    index++;
                }
                String tmp = lineBuffer.toString();
                output.writerLine(tmp);
            }
        }
        input.closeReader();
        output.closeWriter();
    }
}
class Alphabetizer extends Filter {
    private ArrayList<String> kwicList = new ArrayList<>();
    public Alphabetizer(Pipe input, Pipe output) {
        super(input, output);
    }

    @Override
    protected void transform() throws IOException {
        while (input.hashNextLine()){
            kwicList.add(input.readerLine());
        }
        Collections.sort(this.kwicList, new AlphabetizerComparator());
        for (String line:kwicList){
            output.writerLine(line);
        }
        input.closeReader();
        output.closeWriter();
    }
    private class AlphabetizerComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            if (o1 == null && o2 == null) {
                throw new NullPointerException();
            }
            int compareValue = 0;
            char o1c = o1.toLowerCase().charAt(0); //忽略大小写
            char o2c = o2.toLowerCase().charAt(0); //忽略大小写
            compareValue = o1c - o2c;
            return compareValue;

        }

    }
}
class Output extends Filter {
    private File file;
    public Output(File file,Pipe input) {
        super(input, null);
        this.file = file;
    }

    @Override
    protected void transform() throws IOException {
        BufferedWriter outputFile =null;
        String line;
        try {
            outputFile = new BufferedWriter(new FileWriter(file));
            while (input.hashNextLine()) {
                outputFile.write(input.readerLine()+"\n");
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                if (outputFile!=null) {
                    outputFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        input.closeReader();
    }
}
class Main {
    public static void main(String[] args) throws IOException {
        File inFile = new File("E:\\ttt\\input.txt");
        File outFile = new File("E:\\ttt\\output.txt");
        Pipe pipe1 = new Pipe();
        Pipe pipe2 = new Pipe();
        Pipe pipe3 = new Pipe();
        Input input = new Input(inFile, pipe1);
        Shift shift = new Shift(pipe1, pipe2);
        Alphabetizer alphabetizer  = new Alphabetizer(pipe2, pipe3);
        Output output = new Output(outFile,pipe3);
        input.transform();
        shift.transform();
        alphabetizer.transform();
        output.transform();

    }
}

html代码

<!DOCTYPE html>
<html>
<head>
    <title>李可——202131060523</title>
    <script>
        function saveText() {
            // 获取输入框中的内容
            var inputText = document.getElementById("input").value;

            // 创建一个Blob对象,用于保存文本内容
            var blob = new Blob([inputText], { type: "text/plain" });

            // 创建一个链接,并设置下载属性
            var a = document.createElement("a");
            a.href = URL.createObjectURL(blob);
            a.download = "input.txt";

            // 模拟点击下载链接
            a.click();
        }

        function displayText() {
            // 读取本地文件
            var file = new XMLHttpRequest();
            file.open("GET", "E:\\ttt\\output.txt", true);
            file.onreadystatechange = function() {
                if (file.readyState === 4 && file.status === 200) {
                    // 获取文件内容
                    var outputText = file.responseText;

                    // 将文件内容显示在文本框中
                    document.getElementById("output").value = outputText;
                }
            }
            file.send();
        }

        function displayText() {
            var fileInput = document.getElementById("fileInput");
            var textOutput = document.getElementById("textOutput");

            // 检查浏览器是否支持FileReader
            if (typeof FileReader !== "undefined") {
                var reader = new FileReader();

                // 当文件加载完成时触发
                reader.onload = function(e) {
                    textOutput.value = e.target.result;
                };

                // 读取本地文件
                reader.readAsText(fileInput.files[0]);
            } else {
                textOutput.value = "抱歉,你的浏览器不支持FileReader。";
            }
        }
    </script>
</head>
<body>
    <label for="input">输入框:</label>
    <br>
    <textarea id="input" name="input" rows="6" cols="50"></textarea>
    <br>
    <button onclick="saveText()">提交</button>
    <br>
    <input type="file" id="fileInput">
    <br>
    <textarea id="textOutput" rows="6" cols="50"></textarea>
    <br>
    <button onclick="displayText()">显示</button>
    <br>
    <img src="D:\桌面\desktop\学科\软件体系结构\1.png"alt="图片">
    
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值