• 2020101501


    Secondary Namenode莫名其妙无法启动的原因找到。是端口被Eclipse占用的缘故。

    同时,要求的文本编辑也编写完成。在设计的时候在编译原理的启发下做了个带有词法分析器+状态机(但是状态机对语法的分析有一些问题)的版本,模拟了一个简单的指令系统并且使得输入支持 代表换行、 代表制表符、\代表的等字符的转义。

    源代码如下:

      1 package konoha.pkg.p01;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.BufferedWriter;
      5 import java.io.IOException;
      6 import java.io.InputStream;
      7 import java.io.InputStreamReader;
      8 import java.io.OutputStreamWriter;
      9 import java.net.MalformedURLException;
     10 import java.net.URL;
     11 import java.util.Scanner;
     12 
     13 import org.apache.hadoop.conf.Configuration;
     14 import org.apache.hadoop.fs.FSDataOutputStream;
     15 import org.apache.hadoop.fs.FileSystem;
     16 import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
     17 import org.apache.hadoop.fs.Path;
     18 
     19 public class Type0TextEditor {
     20     private static Configuration conf;
     21     static {
     22         URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
     23     }
     24 
     25     public static void Config() {
     26         conf = new Configuration();
     27         conf.set("fs.defaultFS", "hdfs://localhost:9000");
     28         conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
     29     }
     30 
     31     public static boolean hasFile(String path) {
     32         try (FileSystem fs = FileSystem.get(conf)) {
     33             return fs.exists(new Path(path));
     34         } catch (IOException e) {
     35             e.printStackTrace();
     36             return false;
     37         }
     38     }
     39 
     40     public static String readFile(String filePath) {
     41         InputStream in = null;
     42         String rtn = "";
     43         try {
     44             in = new URL(filePath).openStream();
     45             BufferedReader br = new BufferedReader(new InputStreamReader(in));
     46             String tmp = null;
     47             boolean flag = true;
     48             while ((tmp = br.readLine()) != null) {
     49                 if (flag) {
     50                     flag = false;
     51                 } else {
     52                     rtn = rtn + "
    ";
     53                 }
     54                 rtn = rtn + tmp;
     55             }
     56         } catch (MalformedURLException e) {
     57             e.printStackTrace();
     58         } catch (IOException e) {
     59             e.printStackTrace();
     60         }
     61         return rtn;
     62     }
     63 
     64     public static void writeFile(String content, String filePath) {
     65         BufferedWriter bw = null;
     66         try {
     67             FileSystem fs = FileSystem.get(conf);
     68             FSDataOutputStream fout = fs.create(new Path(filePath));
     69             bw = new BufferedWriter(new OutputStreamWriter(fout, "UTF-8"));
     70             bw.write(content);
     71             bw.newLine();
     72             bw.flush();
     73         } catch (IOException e) {
     74             // TODO Auto-generated catch block
     75             e.printStackTrace();
     76         } finally {
     77             if (bw != null) {
     78                 try {
     79                     bw.close();
     80                 } catch (IOException e) {
     81                     // TODO Auto-generated catch block
     82                     e.printStackTrace();
     83                 }
     84             }
     85         }
     86     }
     87 
     88     public static void mkFile(String filePath) {
     89     }
     90 
     91     public static void main(String[] args) {
     92         Type0TextEditor.Config();
     93         String filePath = "hdfs://localhost:9000/user/test/0.txt";
     94         String content = "";
     95         Scanner ipt = new Scanner(System.in);
     96         String input = "";
     97         String inputPart = "";
     98         int operation = -1;
     99         boolean noExit = true;
    100         boolean commandFlag = false;
    101         boolean strFlag = false;
    102         while (noExit) {
    103             if (ipt.hasNextLine()) {
    104                 input = ipt.nextLine() + " ";
    105             }
    106             inputPart = "";
    107             operation = -1;
    108             commandFlag = false;
    109             strFlag = false;
    110             filePath = "";
    111             content = "";
    112             // 输入解析
    113             for (int i = 0; i < input.length(); i++) {
    114                 if (input.charAt(i) == '
    ') {
    115                     continue;
    116                 }
    117                 if (!strFlag) {
    118                     if (input.charAt(i) != '"') {
    119                         if (input.charAt(i) != ' ') {
    120                             inputPart = inputPart + input.charAt(i);
    121                         } else if (input.charAt(i) == ' ') {
    122                             if (!inputPart.equals("")) {
    123                                 if (inputPart.equals("open") && operation == -1) {
    124                                     operation = 0;
    125                                 } else if (inputPart.equals("new") && operation == -1) {
    126                                     operation = 1;
    127                                 } else if (inputPart.equals("insert") && operation == -1) {
    128                                     operation = 2;
    129                                 } else if (inputPart.equals("-start") && operation == 2) {
    130                                     operation = 3;
    131                                 } else if (inputPart.equals("-end") && operation == 2) {
    132                                     operation = 4;
    133                                 } else if (inputPart.equals("exit") && operation == -1) {
    134                                     operation = 5;
    135                                 } else {
    136                                     System.out.println("Syntax error![CODE:1]");
    137                                     operation = -1;
    138                                     break;
    139                                 }
    140                                 inputPart = "";
    141                             }
    142                         }
    143                     } else {// 遇到'"'
    144                         if (inputPart.equals("")) {
    145                             strFlag = true;
    146                         } else {
    147                             System.out.println("Syntax error![CODE:2]");
    148                             operation = -1;
    149                             break;
    150                         }
    151                     }
    152                 } else {// 字符串
    153                     if (commandFlag) {
    154                         if (input.charAt(i) == '\') {
    155                             inputPart = inputPart + "\";
    156                         } else if (input.charAt(i) == '"') {
    157                             inputPart = inputPart + """;
    158                         } else if (input.charAt(i) == 'n') {
    159                             inputPart = inputPart + "
    ";
    160                         } else if (input.charAt(i) == 't') {
    161                             inputPart = inputPart + "	";
    162                         } else {
    163                             inputPart = inputPart + "\" + input.charAt(i);
    164                         }
    165                         commandFlag = false;
    166                     } else {
    167                         if (input.charAt(i) == '\') {
    168                             commandFlag = true;
    169                         } else if (input.charAt(i) == '"') {
    170                             if (operation == 0 || operation == 1) {
    171                                 if (filePath.equals("")) {
    172                                     filePath = inputPart;
    173                                     inputPart = "";
    174                                 } else {
    175                                     System.out.println("Syntax error![CODE:3]");
    176                                     operation = -1;
    177                                     break;
    178                                 }
    179                             } else if (operation == 2 || operation == 3 || operation == 4) {
    180                                 if (filePath.equals("")) {
    181                                     filePath = inputPart;
    182                                     inputPart = "";
    183                                 } else if (content.equals("")) {
    184                                     content = inputPart;
    185                                     inputPart = "";
    186                                 } else {
    187                                     System.out.println("Syntax error![CODE:4]");
    188                                     operation = -1;
    189                                     break;
    190                                 }
    191                             } else {
    192                                 System.out.println("Syntax error![CODE:5]");
    193                                 operation = -1;
    194                                 break;
    195                             }
    196                             inputPart = "";
    197                             strFlag = false;
    198                         } else {
    199                             inputPart = inputPart + input.charAt(i);
    200                         }
    201                     }
    202                 }
    203             }
    204             // 解析完成
    205             System.out.println("[]" + filePath + "||" + content + "[]");
    206             if (operation == 0 && !filePath.equals("")) {
    207                 if (hasFile(filePath)) {
    208                     content = readFile(filePath);
    209                     System.out.println("[OPEN]File "" + filePath + "" is Below");
    210                     System.out.println(content);
    211                 } else {
    212                     System.out.println("[OPEN]No such file on " + filePath);
    213                 }
    214             } else if (operation == 1 && !filePath.equals("")) {
    215                 writeFile("", filePath);
    216                 System.out.println("[NEW]Created File on:" + filePath);
    217             } else if ((operation == 2 || operation == 4) && !filePath.equals("") && !content.equals("")) {
    218                 if (hasFile(filePath)) {
    219                     writeFile(readFile(filePath) + content, filePath);
    220                     System.out.println("[INSERT]Inserted content to end of " + filePath);
    221                 } else {
    222                     System.out.println("[INSERT]No such file on " + filePath);
    223                 }
    224             } else if (operation == 3 && !filePath.equals("") && !content.equals("")) {
    225                 if (hasFile(filePath)) {
    226                     writeFile(content + readFile(filePath), filePath);
    227                     System.out.println("[INSERT]Inserted content to start of " + filePath);
    228                 } else {
    229                     System.out.println("[INSERT]No such file on " + filePath);
    230                 }
    231             } else if (operation == 5) {
    232                 System.out.println("[EXIT]Exited Program");
    233                 break;
    234             } else {
    235                 if (operation == 0) {
    236                     System.out.println("[OPEN]Usage:
    open <FilePath>");
    237                 } else if (operation == 1) {
    238                     System.out.println("[NEW]Usage:
    new <FilePath>");
    239                 } else if (operation == 2 || operation == 3 || operation == 4) {
    240                     System.out.println("[INSERT]Usage:
    insert [-start|-end] <FilePath> <ContentText>");
    241                 } else {
    242                 }
    243             }
    244         }
    245 
    246         ipt.close();
    247     }
    248 }
    Type0TextEditor

  • 相关阅读:
    git init 后关联github仓库是发生错误:
    node mysql模块写入中文字符时的乱码问题
    Error: ER_BAD_FIELD_ERROR: Unknown column 'xxx' in 'where clause'
    安装MariaDB
    yum命令无效
    文档管理系统介绍
    RichTextBox文字处理控件属性介绍
    Android调用相册拍照控件实现系统控件缩放切割图片
    苹果5S指纹扫描识别传感器Touch ID有利于iPhone的安全性
    在服务器端将XML转换成HTML
  • 原文地址:https://www.cnblogs.com/minadukirinno/p/14199259.html
Copyright © 2020-2023  润新知