import java.io.*;
import java.util.Vector;
public class TextCut {
static PrintWriter stdErr = new PrintWriter(System.out);
static PrintWriter out;
static BufferedReader inf;
//static String appPath;
static Vector lines;
public static void main(String[] args) throws IOException {
if (args.length == 0) {
stdErr.println( "this application is use to change the text files to shorter lines, please input file name(s)");
stdErr.flush();
return;
}
int i;
// if ((i = args[0].lastIndexOf('\\')) != -1) {
// appPath = args[0].substring( 0, i ); //get path
// }
lines = new Vector();
for (i = 0; i < args.length; i++) {
try {
run(args[i]);
}
catch(FileNotFoundException e) {
stdErr.println("cannpt open input file:\n" + args[i]);
stdErr.flush();
}
catch(Exception e) {
stdErr.println("unkonw exception for input file:\n" + args[i]
+ "\ndetal reason:\n" + e.toString());
e.printStackTrace();
stdErr.flush();
out.close();
}
}
}
static void run(String arg) throws Exception{
inf = new BufferedReader(new FileReader(arg));
//load file to lines
String line;
line = inf.readLine();
while (line != null) {
lines.add(line);
line = inf.readLine();
}
inf.close();
//backupfile
int i;
String name = arg;
if ((i = arg.lastIndexOf('\\')) != -1) {
name = arg.substring(i + 1); //remove path in name
}
out = new PrintWriter(new FileWriter("backup\\" + name));
int k = lines.size();
for (i = 0; i < k; i++) {
out.println(lines.get(i));
}
out.close();
//open the input file to change
out = new PrintWriter(new FileWriter(arg));
final int COUNT_OF_LINE = 40;
long count = lines.size();
int start, end, endOfNewLine;
for (i = 0; i < count; i++) {
line = (String)lines.get(i);
if (line.length() <= COUNT_OF_LINE)
out.println(line);
else {
start = 0;
end = line.length();
//start is the next char to be outed
while (start < end) {
//get the end index of a new line
endOfNewLine = checkChars( line, start + COUNT_OF_LINE - 1 );
if(endOfNewLine < end) {
out.println( line.substring( start, endOfNewLine + 1 ));
}
else {
out.println( line.substring(start));
break;
}
start = endOfNewLine + 1;
}
}
}
out.close();
}
static boolean isAlp(char c) {
return ( c >= 65 && c <= 90 || c >= 97 && c <= 122);
}
//check some chars, they should not be left to next line
static int checkChars( String str, int end ) {
int length = str.length();
//make end point to the last letter of an English word if it now point to a letter in the word
if(end < length && isAlp(str.charAt(end))) { //donot arpart a word
while(++end < length && isAlp(str.charAt(end)));
end--;
}
//these chars should always be put in the end of a line
if (end + 1 < length) {
char temp = str.charAt(end + 1);
if (temp == '¡£' || temp == '£©' || temp == '£¬' || temp == ',' || temp == '.' || temp == ' ') {
end += 1;
}
}
return end;
}
}//class define end
没有合适的资源?快使用搜索试试~ 我知道了~
实用与学习软件系列(23) textcut 文件换行工具(含源码)
共5个文件
cpp:3个
java:2个
需积分: 9 10 下载量 64 浏览量
2008-10-14
00:11:44
上传
评论
收藏 5KB RAR 举报
温馨提示
对文件自动分行,直接修改输入文件,而非查看时换行。 可供学习如何在c语言下识别汉字等中文字符。
资源推荐
资源详情
资源评论
收起资源包目录
textCut.rar (5个子文件)
textCut
main.cpp 4KB
java&c++
main.cpp 2KB
TextCut.java 3KB
TextCut.java 3KB
isStartCharOfCN.cpp 618B
共 5 条
- 1
资源评论
wangfeiyao1983
- 粉丝: 3
- 资源: 39
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功