码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-06-09 00:53:55      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:val   fileinput   RoCE   eof   null   har   taint   dex   rri   

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

今天浪费好多时间在这个编程题,还没做完,再完美点是将多线程开启后随机选取一个文本文件的诗词进行重写. 或者是多个线程从网络上搜索相关字眼进行摘录重写查找关键字.

虽然不懂什么叫爬虫,但可能这个东东也可能能办到吧?因为看到InputStreamReader的in属性可以使用URL. 

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();
            }

        }

    }

}

 

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

标签:val   fileinput   RoCE   eof   null   har   taint   dex   rri   

原文地址:https://www.cnblogs.com/ukzq/p/9158049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!