package com.itrun.ssm.domain;
import java.io.*;
public class test {
public static void main(String args[]) {
try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw
/* 读入TXT文件 */
String pathname = "F:\\readAndWrite\\2-200.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
File filename = new File(pathname); // 要读取以上路径的input。txt文件
InputStreamReader reader = new InputStreamReader(
new FileInputStream(filename)); // 建立一个输入流对象reader
BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
String line1 = "";
String line2 = "";
String str1 = "";
String str2 = "";
int l1 ;
int l2 ;
line1 = br.readLine();
line2 = line1;
int index = 100;
File subFile = new File("F://readAndWrite//result//" + index + ".txt");
if (!subFile.exists()) {
subFile.createNewFile();
}
BufferedWriter out = new BufferedWriter(new FileWriter(subFile, true));
while (line2 != null) {
line1 = br.readLine();
if (line1 == null){
out.write(line2 + "\r\n");
//out.write(line2 + "\r\n");
out.close();
break;
}
str1 = line1.substring(line1.length()-1,line1.length());
str2 = line2.substring(line2.length()-1,line2.length());
l1 = Integer.parseInt(str1);
l2 = Integer.parseInt(str2);
if (l1 < l2 ) {
out.write(line2 + "\r\n");
line2 = line1;
out.close();
index += 1;
subFile = new File("F://readAndWrite//result//" + index + ".txt");
if (!subFile.exists()) {
subFile.createNewFile();
}
out = new BufferedWriter(new FileWriter(subFile));
//out.write(line1 + "\r\n"); // \r\n即为换行
out.write(line1 + "\r\n");
/*out.write(line1); // \r\n即为换行
out.flush(); // 把缓存区内容压入文件*/
// out.close(); // 最后记得关闭文件
}else {
//subFile = new File(".//result//" + index + ".txt");
/* if (!subFile.exists()) {
subFile.createNewFile();
}*/
//out = new BufferedWriter(new FileWriter(subFile, true));
out.write(line2 + "\r\n"); // \r\n即为换行
line2 = line1;
//out.write(line2 + "\r\n");
out.flush(); // 把缓存区内容压入文件
// 最后记得关闭文件
}
// line1 = br.readLine(); // 一次读入一行数据
// line2 = br.readLine();
if (line1 == null){
out.write(line1 + "\r\n");
//out.write(line2 + "\r\n");
out.close();
}
}
// /* 写入Txt文件 */
// File writename = new File(".\\result\\en\\output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件
// writename.createNewFile(); // 创建新文件
} catch (Exception e) {
e.printStackTrace();
}
}
}