使用InputStreamReader读入,使用OutputStreamWriter写出,将一首诗按行重写?

简介: https://www.processon.com/view/link/5b1a3880e4b00490ac8f5f40改善后: (可将不管一行有几个字时的不规律的文本,按行倒写)package 换行诗;import java.

https://www.processon.com/view/link/5b1a3880e4b00490ac8f5f40

改善后: (可将不管一行有几个字时的不规律的文本,按行倒写)

package 换行诗;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class Poem{

    public static void main(String[] args) {

        InputStream is = null;
        OutputStream os = null;
        OutputStreamWriter osw = null;
        InputStreamReader isr = null;

        try {
            is = new FileInputStream("test1.txt");
            os = new FileOutputStream("test2.txt");
            isr = new InputStreamReader(is, "UTF-8");
            osw = new OutputStreamWriter(os, "UTF-8");

            ArrayList<String> one = getDataInt(isr);
            ArrayList<String> two = getList(one);

            for (String x : two) {
                System.out.print(x);
                osw.write(x);
            }

        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            {
                try {
                    if (osw != null)
                        osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    if (isr != null)
                        isr.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    if (os != null)
                        os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    if (is != null) {
                        is.close();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }

    ////////////////////////////////////////////////////////////

    public static ArrayList<String> getDataInt(InputStreamReader isr) {
        ArrayList<String> dataStrList = new ArrayList<>();
        int data = 0;
        String dataStr = "";
        char c;
        String chineseLine = "";
        try {
            while ((data = isr.read()) != -1) {
                dataStr += data + "";
                c = (char) data;
                chineseLine += c;
                if (dataStr.endsWith("1310")) {
                    dataStrList.add(chineseLine);
                    dataStr = "";
                    chineseLine = "";
                }
            } // while_END
            chineseLine += "\n";
            dataStrList.add(chineseLine);
            // 如果诗和词是一整行 或者 遇到最后一行没有回车换行表示,就存入

        } catch (

        IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (isr != null)
                    isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return dataStrList;
    }

    ///////////////////////////////////////////////////////////////

    public static ArrayList<String> getList(ArrayList<String> one) {
        int w;
        String poem = null;
        ArrayList<String> cutes = new ArrayList<>();

        int dataIndex = one.size() - 1;
        for (w = dataIndex; w >= 0; w--) {
            poem = one.get(w);
            cutes.add(poem);
        }

        return cutes;
    }

}

 

初版(只能针对五言绝句)

package 换行诗;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class Test9 implements Runnable {

    public static void main(String[] args) {
        Runnable r1 = new Test9();
        Thread t1 = new Thread(r1);// t1线程

        Test9 test = new Test9();
//        t1.start();
        InputStream is = null;
        OutputStream os = null;
        OutputStreamWriter osw = null;
        InputStreamReader isr = null;

        try {
            is = new FileInputStream("test1.txt");
            os = new FileOutputStream("test2.txt");
            isr = new InputStreamReader(is, "UTF-8");
            osw = new OutputStreamWriter(os, "UTF-8");

            ArrayList<Integer> one = test.getDataInt(isr);
            ArrayList<Integer> two = test.getList(one);
            final String ANTI_POEM = test.getAntiPoem(two);

            System.out.println(ANTI_POEM);
            osw.write(ANTI_POEM);

        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            {
                try {
                    if (osw != null)
                        osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    if (isr != null)
                        isr.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
                try {
                    if(os != null)
                        os.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
                
                try {
                    if(is != null) {
                        is.close();
                    }
                    
                }catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
        }
    }


    /////////////////////////////////////////////////////

    private String getAntiPoem(ArrayList<Integer> two) {
        String poems = "";
        // 换行计数
        int _1310 = 5;
        int _count = 0;
        int strAnd_n = 0;
        for (int e = 0; e < two.size(); e++) {
            poems += String.valueOf((char) ((int) (two.get(e))));
            if (poems.length() == _1310) {
                poems += "\n";// 假设为6
                _count++;
                if (_count == 1) {
                    strAnd_n = poems.length();
                }
                if (_count < 3) {
                    _1310 += strAnd_n;
                }
            }
        }
        return poems;
    }

    ////////////////////////////////////////////////////////////

    public ArrayList<Integer> getDataInt(InputStreamReader isr) {
        ArrayList<Integer> dataInt = new ArrayList<>();
        int data = 0;
        String dataStr = "";
        try {
            while ((data = isr.read()) != -1) {
                dataStr = data + "";
                if (dataStr.length() == 5) {
                    dataInt.add(data);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (isr != null)
                    isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return dataInt;
    }

    ///////////////////////////////////////////////////////////////

    public ArrayList<Integer> getList(ArrayList<Integer> dataInt) {
        int w;
        int index;

        int dataIndex = dataInt.size() - 1;
        ArrayList<Integer> cutes = new ArrayList<>();
        for (w = dataIndex; w >= 4; w--) {
            for (int l = 0; l < 5; l++) {
                index = (w - 4 + l);
                cutes.add(dataInt.get(index));
            }
            w -= 4;
        }
        return cutes;

    }

    InputStream inThread = null;
    OutputStream outThread = null;
    InputStreamReader isrThread = null;
    OutputStreamWriter oswThread = null;

    @Override
    public void run() {
        try {
            inThread = new FileInputStream("hello.txt");
            outThread = new FileOutputStream("hello1.txt");
            isrThread = new InputStreamReader(inThread, "utf-8");
            oswThread = new OutputStreamWriter(outThread, "utf-8");
            final String ANTI_POEM = getAntiPoem(getList(getDataInt(isrThread)));
            oswThread.write(ANTI_POEM);
            System.out.println(ANTI_POEM);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oswThread != null)
                    oswThread.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (isrThread != null)
                    isrThread.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (outThread != null)
                    outThread.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (inThread != null)
                    inThread.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}

 

将编程看作是一门艺术,而不单单是个技术。 敲打的英文字符是我的黑白琴键, 思维图纸画出的是我编写的五线谱。 当美妙的华章响起,现实通往二进制的大门即将被打开。
相关文章
|
easyexcel 索引
easyexcel读取合并单元格
easyexcel读取合并单元格
6236 1
easyexcel读取合并单元格
|
自然语言处理 JavaScript Java
Spring 实现 3 种异步流式接口,干掉接口超时烦恼
本文介绍了处理耗时接口的几种异步流式技术,包括 `ResponseBodyEmitter`、`SseEmitter` 和 `StreamingResponseBody`。这些工具可在执行耗时操作时不断向客户端响应处理结果,提升用户体验和系统性能。`ResponseBodyEmitter` 适用于动态生成内容场景,如文件上传进度;`SseEmitter` 用于实时消息推送,如状态更新;`StreamingResponseBody` 则适合大数据量传输,避免内存溢出。文中提供了具体示例和 GitHub 地址,帮助读者更好地理解和应用这些技术。
2245 121
|
设计模式 Java
Java“不能转换的类型”解决
在Java编程中,“不能转换的类型”错误通常出现在尝试将一个对象强制转换为不兼容的类型时。解决此问题的方法包括确保类型间存在继承关系、使用泛型或适当的设计模式来避免不安全的类型转换。
1520 7
|
安全 Java 数据库
SpringSecurity实现多种登录方式,如邮件验证码、电话号码登录
SpringSecurity实现多种登录方式,如邮件验证码、电话号码登录
2851 2
|
存储 安全 Java
深入理解Java中的ThreadLocal机制:原理、方法与使用场景解析
深入理解Java中的ThreadLocal机制:原理、方法与使用场景解析
473 2
|
SQL Java 关系型数据库
java 递归返回树形组织结构(附带树形菜单的搜索)
java 递归返回树形组织结构(附带树形菜单的搜索)
402 0
|
Oracle Java Linux
配置JDK环境变量的完整指南
配置JDK环境变量的完整指南
|
存储 网络协议 Java
【JDK21】详解虚拟线程
【JDK21】详解虚拟线程
766 0
|
缓存 Android开发
基于autojs7的番茄免费小说辅助
基于autojs7的番茄免费小说辅助
497 0