字符流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
String path="C:\\2021JAVA\\上机考试模拟题1\\上机考试模拟题1\\A卷(考生姓名)";
FileInputStream in=new FileInputStream(path+"\\DataA.txt");
int temp=in.read();// read()的理解:返回读入的一个字符,如果达到文件末尾,返回-1
int count=0;
while(temp!=-1)
{ if(temp==124)//符号|的ASCII码是124
count++;
temp=in.read();
}
in.close();
System.out.println(++count);
File f=new File(path+"\\"+count+".txt");
f.createNewFile();//新建n.txt文件
FileInputStream in1=new FileInputStream(path+"\\DataA.txt");
FileOutputStream out=new FileOutputStream(path+"\\"+count+".txt",true); //文件字节输出流,输出到文件的数据追加到文件尾
OutputStreamWriter ouw=new OutputStreamWriter(out);//文件字符输出流
ouw.write(""+count+"\r\n");
temp=in1.read();
while(temp!=-1)
{
if(temp!=124)
{
ouw.write(temp);
}
else
{
ouw.write(13);
}
temp=in1.read();
}
ouw.close();
out.close();
in1.close();
}
}
字节流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String path="C:\\2021JAVA\\上机考试模拟题1\\上机考试模拟题1\\A卷(考生姓名)";
FileInputStream in=new FileInputStream(path+"\\DataA.txt");
int temp=in.read();
int count=0;
while(temp!=-1)
{ if(temp==124)
count++;//符号|的ASCII码是124
temp=in.read();
}
in.close();
System.out.println(++count);
File f=new File(path+"\\"+count+".txt");
f.createNewFile();//新建一个n.txt文件
FileInputStream in1=new FileInputStream(path+"\\DataA.txt");
FileOutputStream out=new FileOutputStream(path+"\\"+count+".txt",true); //文件字节输出流,输出到文件的数据追加到文件尾
out.write((""+count+"\r\n").getBytes());//字节数组,存放了当前字符串中的所有字符
temp=in1.read();
while(temp!=-1)
{
if(temp!=124)
{
out.write(temp);
}
else
{
out.write(13);
}
temp=in1.read();
}
out.close();
in1.close();
}
}