zoukankan      html  css  js  c++  java
  • Java 诗词纵向转换字符流输出

    第一次用cnblogs希望大家海涵。

    实现类

      1 package com.xs.test;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.BufferedWriter;
      5 import java.io.FileNotFoundException;
      6 import java.io.FileReader;
      7 import java.io.FileWriter;
      8 import java.io.IOException;
      9 
     10 public class SortChar {
     11     private String srcPath; // 存储源文件
     12     private String destPath;// 存储生成的目标文件
     13     private FileWriter fw = null; // 定义文件写入流
     14     private FileReader fr = null; // 定义文件读取流
     15     private BufferedReader br = null;// 定义缓冲读取流
     16     private BufferedWriter bw = null;// 定义缓冲写入流
     17 
     18     /**
     19      * 
     20      * 描述:读取文件
     21      * 
     22      * @date 2012-4-23
     23      * @author 小四
     24      * @return 读取到的字符串
     25      * @throws IOException
     26      */
     27     public String readerFile() throws IOException {
     28         try {
     29             fr = new FileReader(srcPath);
     30             br = new BufferedReader(fr);
     31             String result = "";
     32             String line = null;
     33             while ((line = br.readLine()) != null) {
     34                 result += line;
     35             }
     36             return result;
     37         } catch (FileNotFoundException e) {
     38             // TODO Auto-generated catch block
     39             return null;
     40         } finally {
     41             try {
     42                 if (fr != null) {
     43                     fr.close();
     44                 } else if (br != null) {
     45                     br.close();
     46                 }
     47             } catch (IOException e) {
     48                 // TODO Auto-generated catch block
     49                 e.printStackTrace();
     50             }
     51         }
     52     }
     53 
     54     /**
     55      * 
     56      * 描述:将传入的字符串进行诗的纵向排列
     57      * 
     58      * @date 2012-4-23
     59      * @author 小四
     60      * @param string
     61      *            传入的诗
     62      * @return 返回排列后的诗的字符串
     63      */
     64     public String sortString(String string) {
     65 
     66         int column = 0;// 记录新列数
     67         int row = 0;// 记录新行数
     68         row = string.indexOf(",");// 根据获得的第一个逗号来判断其新的行数
     69         char[] chars = string.toCharArray();
     70         for (char c : chars) {// 根据判断总的逗号数,还判断新的列数
     71             if (c == ',') {
     72                 column++;
     73             }
     74         }
     75 
     76         // 生成新的二维数组
     77         char[][] sChars = new char[row + 1][column + 1];
     78         StringBuilder sb = new StringBuilder();// 存放重新排序后的数组
     79         for (int i = 0; i <= row; i++) {
     80             for (int j = 0; j <= column; j++) {
     81                 sChars[i][j] = chars[j * 6 + i];// 双层循环改变诗的结构
     82             }
     83         }
     84 
     85         // 循环遍历改变结构后的数组,传给SB来生成新的字符串,方便直接调用writerFile方法输出到文件
     86         for (int i = 0; i <= row; i++) {
     87             for (int j = 0; j <= column; j++) {
     88                 sb.append(sChars[i][j] + "\t");//格式化追加
     89             }
     90             sb.append("\n");//追加换行
     91         }
     92         // System.out.println(sb.toString());
     93         return sb.toString();
     94     }
     95 
     96     /**
     97      * 
     98      * 描述:输出到文件的字符流
     99      * 
    100      * @date 2012-4-23
    101      * @author 小四
    102      */
    103     public void writerFile() {
    104         try {
    105             fw = new FileWriter(destPath);
    106             bw = new BufferedWriter(fw);
    107 
    108             bw.write(sortString(readerFile()));
    109             bw.flush();
    110 
    111         } catch (IOException e) {
    112             // TODO Auto-generated catch block
    113             e.printStackTrace();
    114         } finally {
    115             try {
    116                 if (fw != null) {
    117                     fw.close();
    118                 } else if (bw != null) {
    119                     bw.close();
    120                 }
    121             } catch (IOException e) {
    122                 // TODO: handle exception
    123                 e.printStackTrace();
    124             }
    125         }
    126     }
    127 
    128     /**
    129      * @param srcPath
    130      *            获取源文件路径
    131      * @param destPath
    132      *            生成目标文件路径
    133      */
    134     public SortChar(String srcPath, String destPath) {
    135         this.srcPath = srcPath;
    136         this.destPath = destPath;
    137     }
    138 
    139     /**
    140      * @return the srcPath
    141      */
    142     public String getSrcPath() {
    143         return srcPath;
    144     }
    145 
    146     /**
    147      * @param srcPath
    148      *            the srcPath to set
    149      */
    150     public void setSrcPath(String srcPath) {
    151         this.srcPath = srcPath;
    152     }
    153 
    154     /**
    155      * @return the destPath
    156      */
    157     public String getDestPath() {
    158         return destPath;
    159     }
    160 
    161     /**
    162      * @param destPath
    163      *            the destPath to set
    164      */
    165     public void setDestPath(String destPath) {
    166         this.destPath = destPath;
    167     }
    168 
    169 }

    main测试

     1 package com.xs.test;
     2 
     3 public class SortCharDemo {
     4 
     5     /**
     6      * 描述:
     7      * 
     8      * @date 2012-4-23
     9      * @author 小四
    10      * @param args
    11      */
    12     public static void main(String[] args) {
    13         // TODO Auto-generated method stub
    14         SortChar sortChar = new SortChar("诗.txt", "shi.txt");
    15         sortChar.writerFile();
    16         System.out.println("写入成功。");
    17     }
    18 
    19 }
  • 相关阅读:
    中国剩余定理(crt)和扩展中国剩余定理(excrt)
    数论集合
    gcd(欧几里得算法)与exgcd(扩展欧几里得算法)
    青蛙的约会
    【杭电多校第七场】A + B = C
    【XDOJ】小W的塔防
    备战省赛组队训练赛第十四场(UPC)
    2019.4.27浙江省赛
    备战省赛组队训练赛第六场(UPC)
    备战省赛组队训练赛第七场(UPC)
  • 原文地址:https://www.cnblogs.com/tonycody/p/2467318.html
Copyright © 2011-2022 走看看