文件的输入输出搭配
import java.io.*;
1:字符串读-行读 ,写;
BufferedReader 与 FileReader
PrintWriter 与 FileWriter
例1:复制
BufferedReader in = new BufferedReader(new FileReader("D://test.txt"));
PrintWriter out =new PringWriter(FileWriter(new File("D://test11.txt"));
String s;
//给原文件,增加行标;
int counter=0;
while((s=in.readLine())!=null){
out.println(counter++ + s+"\n");
}
统计奇偶数
int even=0;
int odd=0;
while((s=in.readLine())!=null){
String[] str=s.split(" ");
while(int i=0;i<str.length;i++){
number=Integer.parseInt(str[i]);
if(number%2==0) even++;
else odd++;
}
}
2 单个字符
BufferedInputStream 与 FileInputStream
PrintStream 与 FileOutputStream
时间: 2024-11-07 16:52:29