您的位置:首页 > 编程语言 > Java开发

java处理txt文件,并生成文件

2017-01-19 09:52 489 查看
@Test
public void transfer() throws IOException{
String tx = null;
FileInputStream fis = new FileInputStream("DDDSA2005.TXT");
FileOutputStream fos = new FileOutputStream("newDDDSA2005.CSV");
InputStreamReader read = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(read);
String line = System.getProperty("line.separator");
while ((tx = br.readLine()) != null) {
String newtx =
tx.substring(0, 19) + "|"
+ tx.substring(21, 29) + "|"
+ tx.substring(31, 39) + "|"
+ tx.substring(39, 43) + "|"
+ tx.substring(43, 57) + "|"
+ tx.substring(58, 71) + "|"
+ tx.substring(71, 76) + "|"
+ tx.substring(76, 77) + "|"
+ tx.substring(77, 78) + "|"
+ tx.substring(78, 79) + "|"
+ tx.substring(79, 80) + "|"
+ tx.substring(82, 97) + "|"
+ tx.substring(99, 114) + "|"
+ tx.substring(115, 124) + "|"
+ tx.substring(126, 132) + "|"
+ tx.substring(132, 138) + "|"
+ tx.substring(138, 144) + "|"
+ tx.substring(144, 154) + "|"
+ tx.substring(154, 164) + "|"
+ tx.substring(166, 172) + "|"
+ tx.substring(172, 173);

fos.write(newtx.getBytes());
fos.write(line.getBytes());
}

实现按行处理,每行内容按格式加|分隔符。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: