求一个修改txt文档内容的java源码

N0010M3S2000N0020T1N0030G0X38N0040Z3N0050G1Z0F300N0060X24.5F350N0070G1X24.1Z-7F350N00... N0010M3S2000
N0020T1
N0030G0X38
N0040Z3
N0050G1Z0F300
N0060X24.5F350
N0070G1X24.1Z-7F350
N0075X24Z-9F250
N0080Z-18F230
N0090U-0.5
N0100G0Z20
N0105M30

求能把上述txt文本每一行以N开头+4位数字删除,每一行后面加; 最后一行加上%的java源码,更改后如下文本并另存为新的文件
M3S2000;
T1;
G0X38;
Z3;
G1Z0F300;
X24.5F350;
G1X24.1Z-7F350;
X24Z-9F250;
Z-18F230;
U-0.5;
G0Z20;
M30;
%
展开
 我来答
匿名用户
2019-06-21
展开全部
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new FileReader(new File("test.txt")));
writer = new BufferedWriter(new FileWriter(new File("test1.txt")));
String line;
while ((line = reader.readLine()) != null) {
line = line.substring(5) + ";";
writer.write(line);
writer.newLine();
}
writer.write("%");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2019-06-22
展开全部
public static void main(String[] args) {
try {
File file = new File("C:\\old.txt");
File rs = new File("C:\\new.txt");
rs.createNewFile();

if (file.isFile() && file.exists()) {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),"utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
FileWriter fileWriter = new FileWriter(rs);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String line;
while ((line=bufferedReader.readLine()) != null) {
System.out.println(line.substring(5));
bufferedWriter.write(line.substring(5)+";\r\n");
}
bufferedWriter.write("%");
bufferedWriter.flush();

} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式