使用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分的题目都快更新完了.我就按顺序往下面更新了.回学校之后题目质量能高出不少= =. 问题描述: 读入一个自然数 ...
随机推荐
- Redis 启动警告错误解决
启动错误 1.WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. T ...
- js_常见特效——点击切换_背景颜色_收起与展开
<script src="../js/jquery-1.8.3.min.js"></script><script type="text/ja ...
- Andrew Ng-ML-第八章-正则化
1.过度拟合overfitting 过度拟合,因为有太多的特征+过少的训练数据,学习到的假设可能很适应训练集,但是不能泛化到新的样例.即泛化generalize能力差. 解决办法: 1.手动/使用选择 ...
- OpenCV中PCA实现人脸降维
前言: PCA是大家经常用来减少数据集的维数,同时保留数据集中对方差贡献最大的特征来达到简化数据集的目的.本文通过使用PCA来提取人脸中的特征脸这个例子,来熟悉下在oepncv中怎样使用PCA这个类. ...
- lambda函数和map函数
lambda函数,简化了函数定义的书写形式,使代码更为简洁,但是使用自定义函数的定义方式更为直观,易理解 g = lambda x:x+1 #上面的lambda表达式相当于下面的自定义函数 def g ...
- uva11354 LCA+最小生成树+dp
源自大白书 题意 有n座城市通过m条双向道路相连,每条道路都有一个危险系数.你的任务是回答若干个询问,每个询问包含一个起点s和一个终点t,要求找到一条从s到t的路,使得途径所有的边的大最大危险系数最小 ...
- pandas练习(一)------ 了解数据
探索Chipotle快餐数据 (相关数据见github) 步骤1 导入pandas库 import pandas as pd 步骤2 导入数据集 path1 = "./data/chipot ...
- OS X 下动态库的引用
foo.c #include <stdio.h> void foo(void) { printf("foo.\n"); } main.c #include <st ...
- Python: 字典列表: itemgetter 函数: 根据某个或某几个字典字段来排序列表
问题:根据某个或某几个字典字段来排序Python列表 answer: 通过使用operator 模块的itemgetter 函数,可以非常容易的排序这样的数据结构 eg: rows = [ {'fna ...
- Java设计模式应用——桥接模式
性能管理系统中,数据产生后需要经过采集,汇聚,入库三个流程,用户才能查询使用. 采集可以是snmp采集,也可以是ems采集:汇聚可以使storm汇聚,也可以是spark汇聚:入库可以是hdfs入库,也 ...