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

使用InputStreamReader读入,使用OutputStreamWriter写出,将一首诗按行重写?的更多相关文章

  1. python3 csv数据读入/写出

    这是读入 1 import csv 2 #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() 3 with open("XXX ...

  2. java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  3. PAT 1002. 写出这个数 (20)

    读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...

  4. PAT乙级 1002. 写出这个数 (20)

    1002. 写出这个数 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入一个自然数n,计算其各位数字 ...

  5. PAT 07-0 写出这个数

    用拼音输出一个数字的各位数字之和,这个或许比上面的标题恰当.这里我第一次用到了sprintf()(stdio.h)这个函数,我本来是要找itoa()(stdlib.h)函数来着,查资料一看,说这个函数 ...

  6. PAT (Basic Level) Practise:1002. 写出这个数

    [题目链接] 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各 ...

  7. PAT乙级真题1002. 写出这个数 (20)(解题)

    读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...

  8. PAT-乙级-1002. 写出这个数 (20)

    1002. 写出这个数 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入一个自然数n,计算其各位数字 ...

  9. PAT_1002 写出这个数

    宝宝不开心了.自从回家开始百练就上不去POJ也上不去,今天突然HDU也上不去了,PAT25分的题目都快更新完了.我就按顺序往下面更新了.回学校之后题目质量能高出不少= =. 问题描述: 读入一个自然数 ...

随机推荐

  1. 前端 HTML文档 详解

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. UIStoryboard跳转界面

    /**1.创建Storyboard,加载Storyboard的名字,这里是自己创建的Storyboard的名字*/ UIStoryboard *storyboard = [UIStoryboard s ...

  3. [py][mx]django静态文件目录配置

    使用TemplateView直接返回html from django.views.generic import TemplateView urlpatterns = [ path('',Templat ...

  4. [kx]宇宙-银河

    行星/恒星/卫星的区分 目前太阳系内有8颗行星,分别是:水星.金星.地球.火星.木星.土星.天王星.海王星. 参考 恒星是自发光,而行星(行星通常指自身不发光,其公转方向常与所绕恒星的自转方向相同.) ...

  5. mybatis中使用where in查询时的注意事项

    我使用的时候collection值为mapper的参数名如:int deleteRoleByUserIds(@Param("userIds") String[] userIds); ...

  6. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  7. boost--smart_ptr库

    C++没有类似Java.C#等语言的垃圾回收机制,内存管理是最为头痛的工作. new.delete以及指针的不恰当运用是C++中造成资源获取/释放问题的根源. 智能指针是解决这些问题的一种方案,boo ...

  8. Summary: Java中函数参数的传递

    函数调用参数传递类型(java)的用法介绍. java方法中传值和传引用的问题是个基本问题,但是也有很多人一时弄不清. (一)基本数据类型:传值,方法不会改变实参的值. public class Te ...

  9. JSON草稿

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. 数据仓库基础(十一)Informatica小技巧(2)

    本文转载自:http://www.cnblogs.com/evencao/p/3152384.html 1.User shortcuts:shortcuts能实现快捷方式的复用.快捷方式可以根据源的变 ...