使用InputStreamReader读入,使用OutputStreamWriter写出,将一首诗按行重写?
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写出,将一首诗按行重写?的更多相关文章
- python3 csv数据读入/写出
这是读入 1 import csv 2 #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() 3 with open("XXX ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- PAT 1002. 写出这个数 (20)
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...
- PAT乙级 1002. 写出这个数 (20)
1002. 写出这个数 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入一个自然数n,计算其各位数字 ...
- PAT 07-0 写出这个数
用拼音输出一个数字的各位数字之和,这个或许比上面的标题恰当.这里我第一次用到了sprintf()(stdio.h)这个函数,我本来是要找itoa()(stdlib.h)函数来着,查资料一看,说这个函数 ...
- PAT (Basic Level) Practise:1002. 写出这个数
[题目链接] 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各 ...
- PAT乙级真题1002. 写出这个数 (20)(解题)
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...
- PAT-乙级-1002. 写出这个数 (20)
1002. 写出这个数 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入一个自然数n,计算其各位数字 ...
- PAT_1002 写出这个数
宝宝不开心了.自从回家开始百练就上不去POJ也上不去,今天突然HDU也上不去了,PAT25分的题目都快更新完了.我就按顺序往下面更新了.回学校之后题目质量能高出不少= =. 问题描述: 读入一个自然数 ...
随机推荐
- 【Pyton】【小甲鱼】爬虫
一.什么是爬虫? 可以理解为一只蜘蛛,在不同的网页上爬来爬去,获取我们需要的资源 二.Python如何访问互联网 urllib(一个包)=url(网页地址)+lib() 第一部分:protocol:/ ...
- JVM学习笔记-内存管理
第一章 内存分配 1. 内存区域. 方法区和堆(线程共享),程序计数器 , VM栈 和 本地方法栈(线程隔离). 1) java虚拟机栈:线程私有.描写叙述的是java方法执行的内存模 ...
- 畅通工程续 (SPFA模板Floy模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1874 SPFA #include <iostream> #include <stdio.h&g ...
- 容器集成平台 rancher部署
下载rancher镜像 docker pull rancher/server:stable rancher/server:latest #开发版 rancher/server:stable #稳定版 ...
- 轮廓的查找、表达、绘制、特性及匹配(How to Use Contour? Find, Component, Construct, Features & Match)
http://www.cnblogs.com/xrwang/archive/2010/02/09/HowToUseContour.html 作者:王先荣 前言 轮廓是构成任何一个形状的边界或外形 ...
- 使用sys用户创建其他用户下的dblink
因为dblink的创建和删除只能是它的所属用户来操作,所以我们无法直接使用sys用户创建其他用户下的dblink,当遇到有这样的需求时,可以先建立该用户下存储过程,再通过调用这个存储过程来间接实现. ...
- thinkphp input
变量修饰符 input函数支持对变量使用修饰符功能,可以更好的过滤变量. 用法如下: input('变量类型.变量名/修饰符'); 或者 Request::instance()->变量类型('变 ...
- jquery 删除table行,该如何解决
query 删除table行< table > < tbody > < tr > < td > 这行原来就有 </ td > < ...
- #C++初学记录(ACM试题1)
A - Diverse Strings A string is called diverse if it contains consecutive (adjacent) letters of the ...
- Perl中的哈希(四)
Perl中的哈希数据结构.相比较于数组,这种数据结构对于数据查找和统计更加方便. 一个特殊的哈希,%ENV,表示当前terminal下,通过setenv设置的variable的键值. 键:环境变量名, ...