使用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分的题目都快更新完了.我就按顺序往下面更新了.回学校之后题目质量能高出不少= =. 问题描述: 读入一个自然数 ...
随机推荐
- mysql 内置功能 触发器 实验
#准备表命令表 CREATE TABLE cmd ( id INT PRIMARY KEY auto_increment, ), priv ), cmd ), sub_time datetime, # ...
- mysql5.6编译遇到错误
-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)CMake Error at cmake/readline ...
- 请用漂亮欢呼-------Day38
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/marSmile_tbo/article/details/31108557 周末,双休,疯了两天.敲了 ...
- 浙大 PAT 乙级 1001-1075 目录索引
1001. 害死人不偿命的(3n+1)猜想 1002. 写出这个数 1003. 我要通过! 1004. 成绩排名 1005. 继续(3n+1)猜想 1006. 换个格式输出整数 1007. 素数对猜想 ...
- [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- Linux系统——MySQL基础(二)
# MySQL数据库完全备份与恢复## 数据库备份的分类1. 从物理与逻辑的角度,备份可以分为物理备份和逻辑备份.(1)物理备份:对数据库操作系统的物理文件(数据文件.日志文件)的备份.物理备份又可分 ...
- linux locate
locate命令查找文件比find速度快很多,locate是在linux下实现快速查找文件的工具.相应的windows下有everything功能也很强大. [root@wuzhigang lib]# ...
- Java设计模式应用——策略模式
对于相同类型相同类型的输入输出,在不同场景下需要使用不同的逻辑处理,则可以使用策略模式. 比如排序算法有堆排序,快速排序,冒泡排序,选择排序等.为了保证排序效率,需要在不同场景下选择不同排序算法,这时 ...
- MySQL数据库----函数
函数 MySQL中提供了许多内置函数,例如: CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二字节字符集, LENG ...
- python之路----进程三
IPC--PIPE管道 #创建管道的类: Pipe([duplex]):在进程之间创建一条管道,并返回元组(conn1,conn2),其中conn1,conn2表示管道两端的连接对象,强调一点:必须在 ...