Java 程序 读入程序自身的*.java文件给每一行添加行号,并写入另一个文件。
1个回答
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class Demo
{
public static void main(String[] args)
{
File file = new File("D:\\demo1.java");
try {
BufferedReader br = new BufferedReader(new FileReader("D:\\demo.java"));
String temp = null;
StringBuffer sb = new StringBuffer();
int i = 1;
while((temp = br.readLine()) != null )
{
temp = i + " " + temp;
i++;
sb.append(temp + "\r\n");
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class Demo
{
public static void main(String[] args)
{
File file = new File("D:\\demo1.java");
try {
BufferedReader br = new BufferedReader(new FileReader("D:\\demo.java"));
String temp = null;
StringBuffer sb = new StringBuffer();
int i = 1;
while((temp = br.readLine()) != null )
{
temp = i + " " + temp;
i++;
sb.append(temp + "\r\n");
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询