使用java的输入输出流将一个文本文件的内容按行读取
这段是什么意思FileReaderin=newFileReader(f1);BufferedReaderinTwo=newBufferedReader(in);FileW...
这段是什么意思FileReader in=new FileReader (f1);
BufferedReader inTwo=new BufferedReader(in);
FileWriter outOne=new FileWriter(f2);
BufferedWriter outTwo=new BufferedWriter(outOne);
import java.io.*;
public class EX1 {
public static void main(String[] args) {
File f1=new File("D:\\buhui.txt");
File f2=new File("buzhidao.txt");
try{
FileReader in=new FileReader (f1);
BufferedReader inTwo=new BufferedReader(in);
FileWriter outOne=new FileWriter(f2);
BufferedWriter outTwo=new BufferedWriter(outOne);
String s=null;
int i;
for(i=1;(s=inTwo.readLine())!=null;i++){
System.out.println(i+":"+s);
outTwo.write(i+":"+s);
outTwo.newLine();
}
in.close();
inTwo.close();
outTwo.flush();
outOne.close();
outTwo.close();
}
catch(Exception e){
System.out.println(e);}
}
}
排版
这段是什么意思FileReader in=new FileReader (f1);
BufferedReader inTwo=new BufferedReader(in);
FileWriter outOne=new FileWriter(f2);
BufferedWriter outTwo=new BufferedWriter(outOne); 展开
BufferedReader inTwo=new BufferedReader(in);
FileWriter outOne=new FileWriter(f2);
BufferedWriter outTwo=new BufferedWriter(outOne);
import java.io.*;
public class EX1 {
public static void main(String[] args) {
File f1=new File("D:\\buhui.txt");
File f2=new File("buzhidao.txt");
try{
FileReader in=new FileReader (f1);
BufferedReader inTwo=new BufferedReader(in);
FileWriter outOne=new FileWriter(f2);
BufferedWriter outTwo=new BufferedWriter(outOne);
String s=null;
int i;
for(i=1;(s=inTwo.readLine())!=null;i++){
System.out.println(i+":"+s);
outTwo.write(i+":"+s);
outTwo.newLine();
}
in.close();
inTwo.close();
outTwo.flush();
outOne.close();
outTwo.close();
}
catch(Exception e){
System.out.println(e);}
}
}
排版
这段是什么意思FileReader in=new FileReader (f1);
BufferedReader inTwo=new BufferedReader(in);
FileWriter outOne=new FileWriter(f2);
BufferedWriter outTwo=new BufferedWriter(outOne); 展开
1个回答
展开全部
import java.io.*;
public class FileLineNumber {
public static void main(String[] args) throws Exception {
appendLineNumber(new File("FileLineNumber.java"), new File("a.txt"));
}
public static void appendLineNumber(File from, File to) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(from));
StringBuilder sb = new StringBuilder();
String t;
int lineNumber = 1;
while((t = in.readLine()) != null) {
sb.append(lineNumber + " ");
sb.append(t);
sb.append(System.getProperty("line.separator"));
lineNumber++;
}
in.close();
BufferedWriter out = new BufferedWriter(new FileWriter(to));
out.write(sb.toString());
out.close();
}
}
请采纳答案,支持我一下。
public class FileLineNumber {
public static void main(String[] args) throws Exception {
appendLineNumber(new File("FileLineNumber.java"), new File("a.txt"));
}
public static void appendLineNumber(File from, File to) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(from));
StringBuilder sb = new StringBuilder();
String t;
int lineNumber = 1;
while((t = in.readLine()) != null) {
sb.append(lineNumber + " ");
sb.append(t);
sb.append(System.getProperty("line.separator"));
lineNumber++;
}
in.close();
BufferedWriter out = new BufferedWriter(new FileWriter(to));
out.write(sb.toString());
out.close();
}
}
请采纳答案,支持我一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询